Advertisement
Guest User

Untitled

a guest
Nov 26th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?php
  2. namespace User\Service;
  3.  
  4. use Zend\Authentication\Adapter\AdapterInterface;
  5. use Zend\Authentication\Result;
  6. use Zend\Crypt\Password\Bcrypt;
  7. use User\Entity\User;
  8.  
  9. class AuthAdapter implements AdapterInterface
  10. {
  11.  
  12.     private $email;
  13.     private $password;
  14.     private $entityManager;
  15.        
  16.     public function __construct($entityManager)
  17.     {
  18.         $this->entityManager = $entityManager;
  19.     }
  20.  
  21.     public function setEmail($email)
  22.     {
  23.         $this->email = $email;        
  24.     }
  25.    
  26.     public function setPassword($password)
  27.     {
  28.         $this->password = (string)$password;        
  29.     }
  30.  
  31.     public function authenticate()
  32.     {                
  33.         $user = $this->entityManager->getRepository(User::class)
  34.                 ->findOneByEmail($this->email);
  35.        
  36.         if ($user==null) {
  37.             return new Result(
  38.                 Result::FAILURE_IDENTITY_NOT_FOUND,
  39.                 null,
  40.                 ['Invalid credentials.']);        
  41.         }  
  42.        
  43.         if ($user->getStatus()==User::STATUS_RETIRED) {
  44.             return new Result(
  45.                 Result::FAILURE,
  46.                 null,
  47.                 ['User is retired.']);        
  48.         }
  49.        
  50.         $bcrypt = new Bcrypt();
  51.         $passwordHash = $user->getPassword();
  52.        
  53.         if ($bcrypt->verify($this->password, $passwordHash)) {
  54.             return new Result(
  55.                     Result::SUCCESS,
  56.                     $this->email,
  57.                     ['Authenticated successfully.']);        
  58.         }            
  59.        
  60.         return new Result(
  61.                 Result::FAILURE_CREDENTIAL_INVALID,
  62.                 null,
  63.                 ['Invalid credentials.']);        
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement