File.php 2.0 KB

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