AdminUser.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\models\mysqldb;
  10. use Yii;
  11. use yii\db\ActiveRecord;
  12. use yii\web\IdentityInterface;
  13. /**
  14. * User model
  15. *
  16. * @property integer $id
  17. * @property string $username
  18. * @property string $password_hash
  19. * @property string $password_reset_token
  20. * @property string $email
  21. * @property string $auth_key
  22. * @property integer $status
  23. * @property integer $created_at
  24. * @property integer $updated_at
  25. * @property string $password write-only password
  26. */
  27. /**
  28. * @author Terry Zhao <2358269014@qq.com>
  29. * @since 1.0
  30. */
  31. class AdminUser extends ActiveRecord implements IdentityInterface
  32. {
  33. const STATUS_DELETED = 10;
  34. const STATUS_ACTIVE = 1;
  35. /**
  36. * @inheritdoc
  37. */
  38. # 设置 status 默认 ,以及取值的区间
  39. public function rules()
  40. {
  41. return [
  42. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  43. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  44. ];
  45. }
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'username' => '用户名',
  50. 'password_hash' => '密码',
  51. 'password_reset_token' => '重置密码Token',
  52. 'auth_key' => 'Auth Key',
  53. 'status' => '激活状态',
  54. 'email' => '邮箱地址',
  55. 'created_at' => '创建时间INT',
  56. 'updated_at' => '更新时间INT',
  57. //'role' => '权限',
  58. 'access_token ' => '访问令牌',
  59. 'created_at_datetime' => '创建时间',
  60. 'updated_at_datetime' => '更新时间',
  61. ];
  62. }
  63. /**
  64. * @inheritdoc
  65. */
  66. # 设置table
  67. public static function tableName()
  68. {
  69. return '{{%admin_user}}';
  70. }
  71. /**
  72. * @inheritdoc
  73. */
  74. # 通过id 找到identity
  75. public static function findIdentity($id)
  76. {
  77. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. # 通过access_token 找到identity
  83. public static function findIdentityByAccessToken($token, $type = null)
  84. {
  85. return static::findOne(['access_token' => $token, 'status' => self::STATUS_ACTIVE]);
  86. }
  87. # 生成access_token
  88. public function generateAccessToken()
  89. {
  90. $this->access_token = Yii::$app->security->generateRandomString();
  91. }
  92. /**
  93. * Finds user by username
  94. *
  95. * @param string $username
  96. * @return static|null
  97. */
  98. public static function findByUsername($username)
  99. {
  100. return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  101. }
  102. /**
  103. * Finds user by password reset token
  104. *
  105. * @param string $token password reset token
  106. * @return static|null
  107. */
  108. # 此处是忘记密码所使用的
  109. public static function findByPasswordResetToken($token)
  110. {
  111. if (!static::isPasswordResetTokenValid($token)) {
  112. return null;
  113. }
  114. return static::findOne([
  115. 'password_reset_token' => $token,
  116. 'status' => self::STATUS_ACTIVE,
  117. ]);
  118. }
  119. /**
  120. * Finds out if password reset token is valid
  121. *
  122. * @param string $token password reset token
  123. * @return boolean
  124. */
  125. public static function isPasswordResetTokenValid($token)
  126. {
  127. if (empty($token)) {
  128. return false;
  129. }
  130. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  131. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  132. return $timestamp + $expire >= time();
  133. }
  134. /**
  135. * @inheritdoc
  136. */
  137. public function getId()
  138. {
  139. return $this->getPrimaryKey();
  140. }
  141. /**
  142. * @inheritdoc
  143. */
  144. public function getAuthKey()
  145. {
  146. return $this->auth_key;
  147. }
  148. /**
  149. * @inheritdoc
  150. */
  151. public function validateAuthKey($authKey)
  152. {
  153. return $this->getAuthKey() === $authKey;
  154. }
  155. /**
  156. * Validates password
  157. *
  158. * @param string $password password to validate
  159. * @return boolean if password provided is valid for current user
  160. */
  161. public function validatePassword($password)
  162. {
  163. return Yii::$app->security->validatePassword($password, $this->password_hash);
  164. }
  165. /**
  166. * Generates password hash from password and sets it to the model
  167. *
  168. * @param string $password
  169. */
  170. public function setPassword($password)
  171. {
  172. $this->password_hash = Yii::$app->security->generatePasswordHash($password, 6);
  173. }
  174. /**
  175. * Generates "remember me" authentication key
  176. */
  177. public function generateAuthKey()
  178. {
  179. $this->auth_key = Yii::$app->security->generateRandomString();
  180. }
  181. /**
  182. * Generates new password reset token
  183. */
  184. public function generatePasswordResetToken()
  185. {
  186. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  187. }
  188. /**
  189. * Removes password reset token
  190. */
  191. public function removePasswordResetToken()
  192. {
  193. $this->password_reset_token = null;
  194. }
  195. }