【发布时间】:2015-01-02 05:46:28
【问题描述】:
我正在使用 zend 2 身份验证。现在有一种情况,用户可以使用用户名和密码或电子邮件和密码登录。是否可以在 zend 2 中同时提供用户名和电子邮件作为身份。否则我该如何管理这种情况?
这是我当前的工作代码。这里使用电子邮件作为身份和密码作为凭据。
在 Module.php 中
public function getServiceConfig()
{
return array(
'factories' => array(
'AuthService' => function($sm) {
$dbTableAuthAdapter = new DbTableAuthAdapter(
$sm->get('Zend\Db\Adapter\Adapter'),
'users',
'email',
'password'
);
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
return $authService;
},
)
);
}
在控制器中,
$this->getAuthService()->getAdapter()->setIdentity($oRequest->getPost('username'))->setCredential(md5($oRequest->getPost('password')));
$select = $this->getAuthService()->getAdapter()->getDbSelect();
$select->where('is_active = 1');
$oResult = $this->getAuthService()->authenticate();
// Authentication ends here
if ($oResult->isValid()) {
// code after authentication
}
有什么想法吗?谢谢。
【问题讨论】:
-
这肯定需要一个自定义的身份验证适配器类,可能扩展
Zend\Authentication\Adapter\DbTable\AbstractAdapter。您可以将“标识列”修改为有效列名的数组,并在authenticateCreateSelect中构建选择查询时使用这些名称。
标签: php authentication zend-framework2