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 (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Security
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Security/IAuthenticator.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../Object.php';
  25. 25:  
  26. 26: require_once dirname(__FILE__'/../Security/Identity.php';
  27. 27:  
  28. 28: require_once dirname(__FILE__'/../Security/AuthenticationException.php';
  29. 29:  
  30. 30:  
  31. 31:  
  32. 32: /**
  33. 33:  * Trivial implementation of IAuthenticator.
  34. 34:  *
  35. 35:  * @author     David Grudl
  36. 36:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  37. 37:  * @package    Nette\Security
  38. 38:  */
  39. 39: class SimpleAuthenticator extends Object implements IAuthenticator
  40. 40: {
  41. 41:     /** @var array */
  42. 42:     private $userlist;
  43. 43:  
  44. 44:  
  45. 45:     /**
  46. 46:      * @param  array  list of usernames and passwords
  47. 47:      */
  48. 48:     public function __construct(array $userlist)
  49. 49:     {
  50. 50:         $this->userlist $userlist;
  51. 51:     }
  52. 52:  
  53. 53:  
  54. 54:  
  55. 55:     /**
  56. 45: /**
  57. 46:      * Performs an authentication against e.g. database.
  58. 47:      * and returns IIdentity on success or throws AuthenticationException
  59. 48:      *
  60. 49:      * @param  array 
  61. 50:      * @return IIdentity 
  62. 51:      * @throws AuthenticationException
  63. 62:      */
  64. 63:     public function authenticate(array $credentials)
  65. 64:     {
  66. 65:         $username $credentials[self::USERNAME];
  67. 66:         foreach ($this->userlist as $name => $pass{
  68. 67:             if (strcasecmp($name$credentials[self::USERNAME]=== 0{
  69. 68:                 if (strcasecmp($pass$credentials[self::PASSWORD]=== 0{
  70. 69:                     // matched!
  71. 70:                     return new Identity($name);
  72. 71:                 }
  73. 72:  
  74. 73:                 throw new AuthenticationException("Invalid password."self::INVALID_CREDENTIAL);
  75. 74:             }
  76. 75:         }
  77. 76:  
  78. 77:         throw new AuthenticationException("User '$username' not found."self::IDENTITY_NOT_FOUND);
  79. 78:     }
  80. 79:  
  81. 80: }