You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
4.8 KiB

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