Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use \Doctrine\ORM;
- class Application_Auth_Adapter implements Zend_Auth_Adapter_Interface {
- private $username;
- private $password;
- function __construct($username, $password) {
- $this->username = $username;
- $this->password = $password;
- }
- function authenticate() {
- $em = Zend_Registry::get('em');
- $query = $em->createQuery('select u from Application\Models\User u WHERE u.name = :username')
- ->setParameter('username', $this->username);
- try {
- $user = $query->getSingleResult();
- $salt = $user->salt;
- $hashedPassword = hash_hmac('sha256', $this->password, $salt);
- if ($hashedPassword == $user->password) {
- // login success
- return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $user);
- } else {
- // wrong password
- return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null, array('Wrong password'));
- }
- } catch (ORM\NonUniqueResultException $e) {
- // non unique result
- return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null, array('Non unique username'));
- } catch (ORM\NoResultException $e) {
- // no result found
- return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null, array('User not found'));
- } catch (Exception $e) {
- // exception occured
- return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null, array('Authentication failed 2'));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement