/** * Set authentication result. */ public function setResult(Result $result) { if (!$result->isValid()) { switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: $this->setMessages(['login' => [$this->translate->translate('This user could not be found.')]]); break; case Result::FAILURE_CREDENTIAL_INVALID: $this->setMessages(['password' => [$this->translate->translate('Wrong password provided.')]]); break; } } }
/** * Performs an authentication attempt * * @return \Zend\Authentication\Result * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface If authentication cannot be performed */ public function authenticate() { $qb = $this->db->createQueryBuilder(); if (strpos($this->options['credential_treatment'], '?') === false) { $this->options['credential_treatment'] = '?'; } $expression = '(CASE WHEN ? = ' . $this->options['credential_treatment'] . ' THEN 1 ELSE 0 END) AS ?'; $qb->createPositionalParameter($this->options['credential_column']); $qb->createPositionalParameter($this->credential); $qb->createPositionalParameter('zend_auth_credential_match'); $qb->select(['*', $expression])->from($this->options['table_name'])->where($this->options['identity_column'] . ' = ' . $qb->createPositionalParameter($this->identity)); //TODO check fetch type $resultIdentities = $qb->execute()->fetchAll(); if (count($resultIdentities) < 1) { return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, $this->identity, ['A record with the supplied identity could not be found.']); } else { if (count($resultIdentities) > 1) { return new Result(Result::FAILURE_IDENTITY_AMBIGUOUS, $this->identity, ['More than one record matches the supplied identity.']); } } $authResult = new Result(Result::FAILURE, $this->identity); foreach ($resultIdentities as $resultIdentity) { if ((int) $resultIdentity['zend_auth_credential_match'] !== 1) { $authResult = new Result(Result::FAILURE_CREDENTIAL_INVALID, $this->identity, ['Supplied credential is invalid.']); } else { unset($resultIdentity['zend_auth_credential_match']); $this->resultRow = $resultIdentity; $authResult = new Result(Result::SUCCESS, $this->identity, ['Authentication successful.']); } if ($authResult->isValid()) { break; } } return $authResult; }