Source for file SimpleAuthenticator.php

Documentation is available at SimpleAuthenticator.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Security
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Trivial implementation of IAuthenticator.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Security
  20. 20:  */
  21. 21: class SimpleAuthenticator extends Object implements IAuthenticator
  22. 22: {
  23. 23:     /** @var array */
  24. 24:     private $userlist;
  25. 25:  
  26. 26:  
  27. 27:     /**
  28. 28:      * @param  array  list of usernames and passwords
  29. 29:      */
  30. 30:     public function __construct(array $userlist)
  31. 31:     {
  32. 32:         $this->userlist $userlist;
  33. 33:     }
  34. 34:  
  35. 35:  
  36. 36:  
  37. 37:     /**
  38. 27: /**
  39. 28:      * Performs an authentication against e.g. database.
  40. 29:      * and returns IIdentity on success or throws AuthenticationException
  41. 30:      *
  42. 31:      * @param  array 
  43. 32:      * @return IIdentity 
  44. 33:      * @throws AuthenticationException
  45. 44:      */
  46. 45:     public function authenticate(array $credentials)
  47. 46:     {
  48. 47:         $username $credentials[self::USERNAME];
  49. 48:         foreach ($this->userlist as $name => $pass{
  50. 49:             if (strcasecmp($name$credentials[self::USERNAME]=== 0{
  51. 50:                 if (strcasecmp($pass$credentials[self::PASSWORD]=== 0{
  52. 51:                     // matched!
  53. 52:                     return new Identity($name);
  54. 53:                 }
  55. 54:  
  56. 55:                 throw new AuthenticationException("Invalid password."self::INVALID_CREDENTIAL);
  57. 56:             }
  58. 57:         }
  59. 58:  
  60. 59:         throw new AuthenticationException("User '$username' not found."self::IDENTITY_NOT_FOUND);
  61. 60:     }
  62. 61:  
  63. 62: }