2014-09-30 4 views
-2

Я хочу войти с именем пользователя или адресом электронной почты. Я могу войти только с именем пользователя на данный момент. Как я могу сделать это с ZF2? (И я использую doctirine)ZF2 AuthenticationService

Спасибо

$authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); 
// Do the same you did for the ordinar Zend AuthService 
$adapter = $authService->getAdapter(); 
$adapter->setIdentityValue($post['username']); //$data['usr_name'] 
$adapter->setCredentialValue($post['password']); // $data['usr_password'] 
$authResult = $authService->authenticate(); 

if ($authResult->isValid()) { 
    $identity = $authResult->getIdentity(); 

    $authService->getStorage()->write($identity); 
    $time = 1209600; // 14 days 1209600/3600 = 336 hours => 336/24 = 14 days 

    if ($post['rememberme']) { 
     $sessionManager = new \Zend\Session\SessionManager(); 
     $sessionManager->rememberMe($time); 
    } 
} 
+0

1. формат вопроса лучше 2. повысить уровень голосов – tasmaniski

ответ

0

Вот как это реализовано в модуле 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; 
    } 
0

Если у вас есть этот код в ваш module.config, вы можете изменить identity_property и credential_property, для входа с почтой/nameUser/userName/etc ...

'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; 
        } 
       }, 
      ), 
     ), 
    ), 
Смежные вопросы