/** * @param AdapterChainEvent $e * @return bool */ public function authenticate(AuthEvent $e) { if ($this->isSatisfied()) { $storage = $this->getStorage()->read(); $e->setIdentity($storage['identity'])->setCode(AuthenticationResult::SUCCESS)->setMessages(array('Authentication successful.')); return true; } $identity = $e->getRequest()->getPost()->get('identity'); $credential = $e->getRequest()->getPost()->get('credential'); $credential = $this->preProcessCredential($credential); $userObject = null; $fields = $this->getUserOptions()->getAuthIdentityFields(); while (!is_object($userObject) && count($fields) > 0) { $mode = array_shift($fields); switch ($mode) { case 'username': $userObject = $this->getUserMapper()->findByUsername($identity); break; case 'email': $userObject = $this->getUserMapper()->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; } if ($this->getUserOptions()->isEnableUserStatus()) { if (!in_array($userObject->getStatus(), $this->getUserOptions()->getAllowedLoginStatus())) { $e->setCode(AuthenticationResult::FAILURE_UNCATEGORIZED)->setMessages(array('A record with the supplied identity is not active.')); $this->setSatisfied(false); return false; } } $bcrypt = new Bcrypt(); $bcrypt->setCost($this->getUserOptions()->getPasswordCost()); if (!$bcrypt->verify($credential, $userObject->getPassword())) { $e->setCode(AuthenticationResult::FAILURE_CREDENTIAL_INVALID)->setMessages(array('Supplied credential is invalid.')); $this->setSatisfied(false); return false; } $session = new SessionContainer($this->getStorage()->getNameSpace()); $session->getManager()->regenerateId(); $e->setIdentity($userObject->getId()); $this->updateUserPasswordHash($userObject, $credential, $bcrypt); $this->setSatisfied(true); $storage = $this->getStorage()->read(); $storage['identity'] = $e->getIdentity(); $this->getStorage()->write($storage); $e->setCode(AuthenticationResult::SUCCESS)->setMessages(array('Authentication successful.')); return true; }
/** * @param Event $e * @return AdapterChain */ public function setEvent(Event $e) { if (!$e instanceof AdapterChainEvent) { $eventParams = $e->getParams(); $e = new AdapterChainEvent(); $e->setParams($eventParams); } $this->event = $e; return $this; }