0
0

User.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 integer $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 integer $status
  18. * @property integer $created_at
  19. * @property integer $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. * @return static|null
  71. */
  72. public static function findByUsername($username)
  73. {
  74. return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  75. }
  76. /**
  77. * Finds user by password reset token
  78. *
  79. * @param string $token password reset token
  80. * @return static|null
  81. */
  82. public static function findByPasswordResetToken($token)
  83. {
  84. if (!static::isPasswordResetTokenValid($token)) {
  85. return null;
  86. }
  87. return static::findOne([
  88. 'password_reset_token' => $token,
  89. 'status' => self::STATUS_ACTIVE,
  90. ]);
  91. }
  92. /**
  93. * Finds out if password reset token is valid
  94. *
  95. * @param string $token password reset token
  96. * @return bool
  97. */
  98. public static function isPasswordResetTokenValid($token)
  99. {
  100. if (empty($token)) {
  101. return false;
  102. }
  103. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  104. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  105. return $timestamp + $expire >= time();
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. public function getId()
  111. {
  112. return $this->getPrimaryKey();
  113. }
  114. /**
  115. * @inheritdoc
  116. */
  117. public function getAuthKey()
  118. {
  119. return $this->auth_key;
  120. }
  121. /**
  122. * @inheritdoc
  123. */
  124. public function validateAuthKey($authKey)
  125. {
  126. return $this->getAuthKey() === $authKey;
  127. }
  128. /**
  129. * Validates password
  130. *
  131. * @param string $password password to validate
  132. * @return bool if password provided is valid for current user
  133. */
  134. public function validatePassword($password)
  135. {
  136. return Yii::$app->security->validatePassword($password, $this->password_hash);
  137. }
  138. /**
  139. * Generates password hash from password and sets it to the model
  140. *
  141. * @param string $password
  142. */
  143. public function setPassword($password)
  144. {
  145. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  146. }
  147. /**
  148. * Generates "remember me" authentication key
  149. */
  150. public function generateAuthKey()
  151. {
  152. $this->auth_key = Yii::$app->security->generateRandomString();
  153. }
  154. /**
  155. * Generates new password reset token
  156. */
  157. public function generatePasswordResetToken()
  158. {
  159. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  160. }
  161. /**
  162. * Removes password reset token
  163. */
  164. public function removePasswordResetToken()
  165. {
  166. $this->password_reset_token = null;
  167. }
  168. }