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.

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