User.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\db\ActiveRecord;
  7. use yii\web\IdentityInterface;
  8. /**
  9. * User model.
  10. *
  11. * @property int $id
  12. * @property string $username
  13. * @property string $password_hash
  14. * @property string $password_reset_token
  15. * @property string $email
  16. * @property string $auth_key
  17. * @property int $status
  18. * @property int $created_at
  19. * @property int $updated_at
  20. * @property string $password write-only password
  21. */
  22. class User extends ActiveRecord implements IdentityInterface
  23. {
  24. const STATUS_DELETED = 0;
  25. const STATUS_ACTIVE = 10;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%user}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function behaviors()
  37. {
  38. return [
  39. TimestampBehavior::className(),
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function rules()
  46. {
  47. return [
  48. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  49. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function findIdentity($id)
  56. {
  57. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public static function findIdentityByAccessToken($token, $type = null)
  63. {
  64. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  65. }
  66. /**
  67. * Finds user by username.
  68. *
  69. * @param string $username
  70. *
  71. * @return static|null
  72. */
  73. public static function findByUsername($username)
  74. {
  75. return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  76. }
  77. /**
  78. * Finds user by password reset token.
  79. *
  80. * @param string $token password reset token
  81. *
  82. * @return static|null
  83. */
  84. public static function findByPasswordResetToken($token)
  85. {
  86. if (!static::isPasswordResetTokenValid($token)) {
  87. return;
  88. }
  89. return static::findOne([
  90. 'password_reset_token' => $token,
  91. 'status' => self::STATUS_ACTIVE,
  92. ]);
  93. }
  94. /**
  95. * Finds out if password reset token is valid.
  96. *
  97. * @param string $token password reset token
  98. *
  99. * @return bool
  100. */
  101. public static function isPasswordResetTokenValid($token)
  102. {
  103. if (empty($token)) {
  104. return false;
  105. }
  106. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  107. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  108. return $timestamp + $expire >= time();
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function getId()
  114. {
  115. return $this->getPrimaryKey();
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function getAuthKey()
  121. {
  122. return $this->auth_key;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function validateAuthKey($authKey)
  128. {
  129. return $this->getAuthKey() === $authKey;
  130. }
  131. /**
  132. * Validates password.
  133. *
  134. * @param string $password password to validate
  135. *
  136. * @return bool if password provided is valid for current user
  137. */
  138. public function validatePassword($password)
  139. {
  140. return Yii::$app->security->validatePassword($password, $this->password_hash);
  141. }
  142. /**
  143. * Generates password hash from password and sets it to the model.
  144. *
  145. * @param string $password
  146. */
  147. public function setPassword($password)
  148. {
  149. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  150. }
  151. /**
  152. * Generates "remember me" authentication key.
  153. */
  154. public function generateAuthKey()
  155. {
  156. $this->auth_key = Yii::$app->security->generateRandomString();
  157. }
  158. /**
  159. * Generates new password reset token.
  160. */
  161. public function generatePasswordResetToken()
  162. {
  163. $this->password_reset_token = Yii::$app->security->generateRandomString().'_'.time();
  164. }
  165. /**
  166. * Removes password reset token.
  167. */
  168. public function removePasswordResetToken()
  169. {
  170. $this->password_reset_token = null;
  171. }
  172. }