Authentication ZF2身份验证服务
我想用用户名或电子邮件地址登录。我现在可以用用户名登录。如何使用ZF2执行此操作?(我用的是博士药) 多谢各位Authentication ZF2身份验证服务,authentication,doctrine-orm,zend-framework2,Authentication,Doctrine Orm,Zend Framework2,我想用用户名或电子邮件地址登录。我现在可以用用户名登录。如何使用ZF2执行此操作?(我用的是博士药) 多谢各位 $authService=$this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); //执行与ordinar Zend AuthService相同的操作 $adapter=$authService->getAdapter(); $adapter->setIdentityValue($pos
$authService=$this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');
//执行与ordinar Zend AuthService相同的操作
$adapter=$authService->getAdapter();
$adapter->setIdentityValue($post['username'])//$数据['usr_名称']
$adapter->setCredentialValue($post['password']);//$数据['usr_密码']
$authResult=$authService->authenticate();
如果($authResult->isValid()){
$identity=$authResult->getIdentity();
$authService->getStorage()->write($identity);
$time=1209600;//14天1209600/3600=336小时=>336/24=14天
如果($post['rememberme'])){
$sessionManager=new\Zend\Session\sessionManager();
$sessionManager->rememberMe($time);
}
}
以下是如何在zfcUser模块中实现此功能:
$userObject = null;
// Cycle through the configured identity sources and test each
$fields = $this->getOptions()->getAuthIdentityFields();
while (!is_object($userObject) && count($fields) > 0) {
$mode = array_shift($fields);
switch ($mode) {
case 'username':
$userObject = $this->getMapper()->findByUsername($identity);
break;
case 'email':
$userObject = $this->getMapper()->findByEmail($identity);
break;
}
}
if (!$userObject) {
$e->setCode(AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND)
->setMessages(array('A record with the supplied identity could not be found.'));
$this->setSatisfied(false);
return false;
}
如果您的module.config中有此代码,您可以修改标识属性和凭证属性,以便使用mail/nameUser/userName/等登录
'doctrine' => array(
// 1) for Aithentication
'authentication' => array( // this part is for the Auth adapter from DoctrineModule/Authentication
'orm_default' => array(
'object_manager' => 'Doctrine\ORM\EntityManager',
// object_repository can be used instead of the object_manager key
'identity_class' => 'Application\Entity\Users', //'Application\Entity\User',
'identity_property' => 'usrName', // 'username', // 'email',
'credential_property' => 'usrPassword', // 'password',
'credential_callable' => function($user, $passwordGiven) { // not only User
if ($user->getUsrPassword() == md5('aFGQ475SDsdfsaf2342' . $passwordGiven . $user->getUsrPasswordSalt()) &&
$user->getUsrActive() == 1) {
return true;
}
else {
return false;
}
},
),
),
),
1.格式问题2。提高投票率