/common/models/User.php

https://github.com/michaelweixi/blogdemo2 · PHP · 228 lines · 117 code · 33 blank · 78 comment · 2 complexity · fd4c51fafb32ff3d803bbc8cce10e27b MD5 · raw file

  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. * @property Comment[] $comments
  23. */
  24. class User extends ActiveRecord implements IdentityInterface
  25. {
  26. const STATUS_DELETED = 0;
  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_ACTIVE],
  51. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  52. [['email'], 'unique'],
  53. [['email'], 'required'],
  54. [['email'], 'email'],
  55. ];
  56. }
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'id' => 'ID',
  61. 'username' => '用户名',
  62. 'auth_key' => 'Auth Key',
  63. 'password_hash' => 'Password Hash',
  64. 'password_reset_token' => 'Password Reset Token',
  65. 'email' => 'Email',
  66. 'status' => '状态',
  67. 'created_at' => '创建时间',
  68. 'updated_at' => '修改时间',
  69. ];
  70. }
  71. /**
  72. * @inheritdoc
  73. */
  74. public static function findIdentity($id)
  75. {
  76. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public static function findIdentityByAccessToken($token, $type = null)
  82. {
  83. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  84. }
  85. /**
  86. * Finds user by username
  87. *
  88. * @param string $username
  89. * @return static|null
  90. */
  91. public static function findByUsername($username)
  92. {
  93. return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  94. }
  95. /**
  96. * Finds user by password reset token
  97. *
  98. * @param string $token password reset token
  99. * @return static|null
  100. */
  101. public static function findByPasswordResetToken($token)
  102. {
  103. if (!static::isPasswordResetTokenValid($token)) {
  104. return null;
  105. }
  106. return static::findOne([
  107. 'password_reset_token' => $token,
  108. 'status' => self::STATUS_ACTIVE,
  109. ]);
  110. }
  111. /**
  112. * Finds out if password reset token is valid
  113. *
  114. * @param string $token password reset token
  115. * @return boolean
  116. */
  117. public static function isPasswordResetTokenValid($token)
  118. {
  119. if (empty($token)) {
  120. return false;
  121. }
  122. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  123. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  124. return $timestamp + $expire >= time();
  125. }
  126. /**
  127. * @inheritdoc
  128. */
  129. public function getId()
  130. {
  131. return $this->getPrimaryKey();
  132. }
  133. /**
  134. * @inheritdoc
  135. */
  136. public function getAuthKey()
  137. {
  138. return $this->auth_key;
  139. }
  140. /**
  141. * @inheritdoc
  142. */
  143. public function validateAuthKey($authKey)
  144. {
  145. return $this->getAuthKey() === $authKey;
  146. }
  147. /**
  148. * Validates password
  149. *
  150. * @param string $password password to validate
  151. * @return boolean if password provided is valid for current user
  152. */
  153. public function validatePassword($password)
  154. {
  155. return Yii::$app->security->validatePassword($password, $this->password_hash);
  156. }
  157. /**
  158. * Generates password hash from password and sets it to the model
  159. *
  160. * @param string $password
  161. */
  162. public function setPassword($password)
  163. {
  164. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  165. }
  166. /**
  167. * Generates "remember me" authentication key
  168. */
  169. public function generateAuthKey()
  170. {
  171. $this->auth_key = Yii::$app->security->generateRandomString();
  172. }
  173. /**
  174. * Generates new password reset token
  175. */
  176. public function generatePasswordResetToken()
  177. {
  178. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  179. }
  180. /**
  181. * Removes password reset token
  182. */
  183. public function removePasswordResetToken()
  184. {
  185. $this->password_reset_token = null;
  186. }
  187. public static function allStatus()
  188. {
  189. return [self::STATUS_ACTIVE=>'正常',self::STATUS_DELETED=>'已删除'];
  190. }
  191. public function getStatusStr()
  192. {
  193. return $this->status==self::STATUS_ACTIVE?'正常':'已删除';
  194. }
  195. }