File.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * File Auth driver.
  4. * [!!] this Auth driver does not support roles nor autologin.
  5. *
  6. * @package Kohana/Auth
  7. * @author Kohana Team
  8. * @copyright (c) Kohana Team
  9. * @license https://koseven.ga/LICENSE.md
  10. */
  11. class Kohana_Auth_File extends Auth {
  12. // User list
  13. protected $_users;
  14. /**
  15. * Constructor loads the user list into the class.
  16. */
  17. public function __construct($config = [])
  18. {
  19. parent::__construct($config);
  20. // Load user list
  21. $this->_users = Arr::get($config, 'users', []);
  22. }
  23. /**
  24. * Logs a user in.
  25. *
  26. * @param string $username Username
  27. * @param string $password Password
  28. * @param boolean $remember Enable autologin (not supported)
  29. * @return boolean
  30. */
  31. protected function _login($username, $password, $remember)
  32. {
  33. if ($remember)
  34. {
  35. throw new Kohana_Exception('File based auth does not support remember');
  36. }
  37. if (is_string($password))
  38. {
  39. // Create a hashed password
  40. $password = $this->hash($password);
  41. }
  42. if (isset($this->_users[$username]) AND $this->_users[$username] === $password)
  43. {
  44. // Complete the login
  45. return $this->complete_login($username);
  46. }
  47. // Login failed
  48. return FALSE;
  49. }
  50. /**
  51. * Forces a user to be logged in, without specifying a password.
  52. *
  53. * @param mixed $username Username
  54. * @return boolean
  55. */
  56. public function force_login($username)
  57. {
  58. // Complete the login
  59. return $this->complete_login($username);
  60. }
  61. /**
  62. * Get the stored password for a username.
  63. *
  64. * @param mixed $username Username
  65. * @return string
  66. */
  67. public function password($username)
  68. {
  69. return Arr::get($this->_users, $username, FALSE);
  70. }
  71. /**
  72. * Compare password with original (plain text). Works for current (logged in) user
  73. *
  74. * @param string $password Password
  75. * @return boolean
  76. */
  77. public function check_password($password)
  78. {
  79. $username = $this->get_user();
  80. if ($username === FALSE)
  81. return FALSE;
  82. return ($password === $this->password($username));
  83. }
  84. }