Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2. use \Doctrine\ORM;
  3. class Application_Auth_Adapter implements Zend_Auth_Adapter_Interface {
  4.     private $username;
  5.     private $password;
  6.    
  7.     function __construct($username, $password) {
  8.         $this->username = $username;
  9.         $this->password = $password;
  10.     }
  11.    
  12.     function authenticate() {
  13. $em = Zend_Registry::get('em');
  14. $query = $em->createQuery('select u from Application\Models\User u WHERE u.name = :username')
  15.             ->setParameter('username', $this->username);
  16.         try {
  17.             $user = $query->getSingleResult();
  18.             $salt = $user->salt;
  19.             $hashedPassword = hash_hmac('sha256', $this->password, $salt);
  20.             if ($hashedPassword == $user->password) {
  21.                 // login success
  22.                 return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $user);
  23.             } else {
  24.                 // wrong password
  25.                 return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null, array('Wrong password'));
  26.             }
  27.         } catch (ORM\NonUniqueResultException $e) {
  28.             // non unique result
  29.             return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null, array('Non unique username'));
  30.         } catch (ORM\NoResultException $e) {
  31.             // no result found
  32.             return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null, array('User not found'));
  33.         } catch (Exception $e) {
  34.             // exception occured
  35.             return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null, array('Authentication failed 2'));
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement