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