1
0

UserController.php 4.4 KB

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