/backend/models/AdminUser.php

https://github.com/fengahan/Yat · PHP · 211 lines · 105 code · 22 blank · 84 comment · 2 complexity · f93997a6275e9013f23e1bf49e69fb7d MD5 · raw file

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