AdminUser.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace backend\models;
  3. use Yii;
  4. use yii\web\IdentityInterface;
  5. /**
  6. * This is the model class for table "{{%admin_user}}".
  7. *
  8. * @property integer $id
  9. * @property string $username
  10. * @property string $auth_key
  11. * @property string $password_hash
  12. * @property string $password_reset_token
  13. * @property string $email
  14. * @property integer $status
  15. * @property integer $created_at
  16. * @property integer $updated_at
  17. */
  18. class AdminUser extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
  19. {
  20. const STATUS_DELETED = 0;
  21. const STATUS_ACTIVE = 10;
  22. const ROLE_ADMIN = 'admin';//管理员
  23. const ROLE_BUSINESS = 'business';//接单业务员
  24. public static $role_arr = [
  25. 'admin'=>'管理员',
  26. 'business'=>'业务员'
  27. ];
  28. /**
  29. * @inheritdoc
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%admin_user}}';
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['username', 'auth_key', 'password_hash', 'created_at', 'updated_at'], 'required'],
  42. [['status', 'created_at', 'updated_at'], 'integer'],
  43. [['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
  44. [['auth_key','role','realname'], 'string', 'max' => 32],
  45. [['username'], 'unique'],
  46. // [['email'], 'unique'],
  47. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  48. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  49. [['password_reset_token'], 'unique'],
  50. ];
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'username' => 'Username',
  60. 'auth_key' => 'Auth Key',
  61. 'password_hash' => 'Password Hash',
  62. 'password_reset_token' => 'Password Reset Token',
  63. 'email' => 'Email',
  64. 'status' => 'Status',
  65. 'created_at' => 'Created At',
  66. 'updated_at' => 'Updated At',
  67. ];
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. public static function findIdentity($id)
  73. {
  74. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  75. }
  76. /**
  77. * API access_token 认证
  78. * @inheritdoc
  79. */
  80. public static function findIdentityByAccessToken($token, $type = null)
  81. {
  82. $username = Yii::$app->request->post('username');
  83. $model = static::findOne(['username'=>$username,'status'=>self::STATUS_ACTIVE]);
  84. if($model === null){
  85. return null;
  86. }
  87. return $model->access_token === $token?$model:null;
  88. // throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  89. }
  90. /**
  91. * Finds user by username
  92. *
  93. * @param string $username
  94. * @return static|null
  95. */
  96. public static function findByUsername($username)
  97. {
  98. return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  99. }
  100. /**
  101. * Finds user by password reset token
  102. *
  103. * @param string $token password reset token
  104. * @return static|null
  105. */
  106. public static function findByPasswordResetToken($token)
  107. {
  108. if (!static::isPasswordResetTokenValid($token)) {
  109. return null;
  110. }
  111. return static::findOne([
  112. 'password_reset_token' => $token,
  113. 'status' => self::STATUS_ACTIVE,
  114. ]);
  115. }
  116. /**
  117. * Finds out if password reset token is valid
  118. *
  119. * @param string $token password reset token
  120. * @return bool
  121. */
  122. public static function isPasswordResetTokenValid($token)
  123. {
  124. if (empty($token)) {
  125. return false;
  126. }
  127. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  128. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  129. return $timestamp + $expire >= time();
  130. }
  131. /**
  132. * @inheritdoc
  133. */
  134. public function getId()
  135. {
  136. return $this->getPrimaryKey();
  137. }
  138. /**
  139. * @inheritdoc
  140. */
  141. public function getAuthKey()
  142. {
  143. return $this->auth_key;
  144. }
  145. /**
  146. * @inheritdoc
  147. */
  148. public function validateAuthKey($authKey)
  149. {
  150. return $this->getAuthKey() === $authKey;
  151. }
  152. /**
  153. * Validates password
  154. *
  155. * @param string $password password to validate
  156. * @return bool if password provided is valid for current user
  157. */
  158. public function validatePassword($password)
  159. {
  160. return Yii::$app->security->validatePassword($password, $this->password_hash);
  161. }
  162. /**
  163. * Generates password hash from password and sets it to the model
  164. *
  165. * @param string $password
  166. */
  167. public function setPassword($password)
  168. {
  169. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  170. }
  171. /**
  172. * Generates "remember me" authentication key
  173. */
  174. public function generateAuthKey()
  175. {
  176. $this->auth_key = Yii::$app->security->generateRandomString();
  177. }
  178. /**
  179. * Generates new password reset token
  180. */
  181. public function generatePasswordResetToken()
  182. {
  183. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  184. }
  185. /**
  186. * Removes password reset token
  187. */
  188. public function removePasswordResetToken()
  189. {
  190. $this->password_reset_token = null;
  191. }
  192. }