User.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Default auth user
  4. *
  5. * @package Kohana/Auth
  6. * @author Kohana Team
  7. * @copyright (c) Kohana Team
  8. * @license https://koseven.ga/LICENSE.md
  9. */
  10. class Model_Auth_User extends ORM {
  11. /**
  12. * A user has many tokens and roles
  13. *
  14. * @var array Relationhips
  15. */
  16. protected $_has_many = [
  17. 'user_tokens' => ['model' => 'User_Token'],
  18. 'roles' => ['model' => 'Role', 'through' => 'roles_users'],
  19. ];
  20. /**
  21. * Rules for the user model. Because the password is _always_ a hash
  22. * when it's set,you need to run an additional not_empty rule in your controller
  23. * to make sure you didn't hash an empty string. The password rules
  24. * should be enforced outside the model or with a model helper method.
  25. *
  26. * @return array Rules
  27. */
  28. public function rules()
  29. {
  30. return [
  31. 'username' => [
  32. ['not_empty'],
  33. ['max_length', [':value', 32]],
  34. [[$this, 'unique'], ['username', ':value']],
  35. ],
  36. 'password' => [
  37. ['not_empty'],
  38. ],
  39. 'email' => [
  40. ['not_empty'],
  41. ['email'],
  42. [[$this, 'unique'], ['email', ':value']],
  43. ],
  44. ];
  45. }
  46. /**
  47. * Filters to run when data is set in this model. The password filter
  48. * automatically hashes the password when it's set in the model.
  49. *
  50. * @return array Filters
  51. */
  52. public function filters()
  53. {
  54. return [
  55. 'password' => [
  56. [[Auth::instance(), 'hash']]
  57. ]
  58. ];
  59. }
  60. /**
  61. * Labels for fields in this model
  62. *
  63. * @return array Labels
  64. */
  65. public function labels()
  66. {
  67. return [
  68. 'username' => 'username',
  69. 'email' => 'email address',
  70. 'password' => 'password',
  71. ];
  72. }
  73. /**
  74. * Complete the login for a user by incrementing the logins and saving login timestamp
  75. *
  76. * @return void
  77. */
  78. public function complete_login()
  79. {
  80. if ($this->_loaded)
  81. {
  82. // Update the number of logins
  83. $this->logins = new Database_Expression('logins + 1');
  84. // Set the last login date
  85. $this->last_login = time();
  86. // Save the user
  87. $this->update();
  88. }
  89. }
  90. /**
  91. * Tests if a unique key value exists in the database.
  92. *
  93. * @param mixed the value to test
  94. * @param string field name
  95. * @return boolean
  96. */
  97. public function unique_key_exists($value, $field = NULL)
  98. {
  99. if ($field === NULL)
  100. {
  101. // Automatically determine field by looking at the value
  102. $field = $this->unique_key($value);
  103. }
  104. return (bool) DB::select([DB::expr('COUNT(*)'), 'total_count'])
  105. ->from($this->_table_name)
  106. ->where($field, '=', $value)
  107. ->where($this->_primary_key, '!=', $this->pk())
  108. ->execute($this->_db)
  109. ->get('total_count');
  110. }
  111. /**
  112. * Allows a model use both email and username as unique identifiers for login
  113. *
  114. * @param string unique value
  115. * @return string field name
  116. */
  117. public function unique_key($value)
  118. {
  119. return Valid::email($value) ? 'email' : 'username';
  120. }
  121. /**
  122. * Password validation for plain passwords.
  123. *
  124. * @param array $values
  125. * @return Validation
  126. */
  127. public static function get_password_validation($values)
  128. {
  129. return Validation::factory($values)
  130. ->rule('password', 'min_length', [':value', 8])
  131. ->rule('password_confirm', 'matches', [':validation', ':field', 'password']);
  132. }
  133. /**
  134. * Create a new user
  135. *
  136. * Example usage:
  137. * ~~~
  138. * $user = ORM::factory('User')->create_user($_POST, array(
  139. * 'username',
  140. * 'password',
  141. * 'email',
  142. * );
  143. * ~~~
  144. *
  145. * @param array $values
  146. * @param array $expected
  147. * @throws ORM_Validation_Exception
  148. */
  149. public function create_user($values, $expected)
  150. {
  151. // Validation for passwords
  152. $extra_validation = Model_User::get_password_validation($values)
  153. ->rule('password', 'not_empty');
  154. return $this->values($values, $expected)->create($extra_validation);
  155. }
  156. /**
  157. * Update an existing user
  158. *
  159. * [!!] We make the assumption that if a user does not supply a password, that they do not wish to update their password.
  160. *
  161. * Example usage:
  162. * ~~~
  163. * $user = ORM::factory('User')
  164. * ->where('username', '=', 'kiall')
  165. * ->find()
  166. * ->update_user($_POST, array(
  167. * 'username',
  168. * 'password',
  169. * 'email',
  170. * );
  171. * ~~~
  172. *
  173. * @param array $values
  174. * @param array $expected
  175. * @throws ORM_Validation_Exception
  176. */
  177. public function update_user($values, $expected = NULL)
  178. {
  179. if (empty($values['password']))
  180. {
  181. unset($values['password'], $values['password_confirm']);
  182. }
  183. // Validation for passwords
  184. $extra_validation = Model_User::get_password_validation($values);
  185. return $this->values($values, $expected)->update($extra_validation);
  186. }
  187. } // End Auth User Model