User.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace common\models\base;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\db\ActiveRecord;
  6. use yii\web\IdentityInterface;
  7. use yii\behaviors\TimestampBehavior;
  8. use common\models\rbac\AuthAssignment;
  9. /**
  10. * User model
  11. *
  12. * @property integer $id
  13. * @property string $username
  14. * @property string $password_hash
  15. * @property string $password_reset_token
  16. * @property string $email
  17. * @property string $auth_key
  18. * @property integer $status
  19. * @property integer $created_at
  20. * @property integer $updated_at
  21. * @property string $password write-only password
  22. */
  23. class User extends BaseActiveRecord implements IdentityInterface
  24. {
  25. /**
  26. * 禁用登录
  27. */
  28. const STATUS_DELETED = 0;
  29. /**
  30. * 正常登录
  31. */
  32. const STATUS_ACTIVE = 1;
  33. /**
  34. * 规则
  35. */
  36. const ROLE_ADMIN = 10;
  37. /**
  38. * @inheritdoc
  39. */
  40. public function behaviors()
  41. {
  42. return [
  43. TimestampBehavior::class,
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function rules()
  50. {
  51. return [
  52. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  53. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  54. ];
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. public static function findIdentity($id)
  60. {
  61. return static::find($id);
  62. }
  63. /**
  64. * @param mixed $token
  65. * @param null $type
  66. * @return void|IdentityInterface
  67. * @throws NotSupportedException
  68. */
  69. public static function findIdentityByAccessToken($token, $type = null)
  70. {
  71. return static::findOne(['access_token' => $token]);
  72. // throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  73. }
  74. public function getId()
  75. {
  76. return $this->getPrimaryKey();
  77. }
  78. /**
  79. * 获取授权码
  80. * @return string
  81. */
  82. public function getAuthKey()
  83. {
  84. return $this->auth_key;
  85. }
  86. /**
  87. * 验证授权码
  88. *
  89. * @inheritdoc
  90. */
  91. public function validateAuthKey($authKey)
  92. {
  93. return $this->getAuthKey() === $authKey;
  94. }
  95. /**
  96. * 验证密码是否正确
  97. *
  98. * @param $password
  99. * @return bool
  100. */
  101. public function validatePassword($password)
  102. {
  103. return Yii::$app->security->validatePassword($password, $this->password);
  104. }
  105. /**
  106. * 通过用户名查找用户
  107. *
  108. * @param $username
  109. * @return array|null|ActiveRecord
  110. */
  111. public static function findByUsername($username)
  112. {
  113. return static::find()->where([
  114. 'username' => $username,
  115. 'status' => self::STATUS_ACTIVE,
  116. ])->one();
  117. }
  118. /**
  119. * 查找密码重置token
  120. *
  121. * @param string $token password reset token
  122. * @return static|null
  123. */
  124. public static function findByPasswordResetToken($token)
  125. {
  126. if (!static::isPasswordResetTokenValid($token)) {
  127. return null;
  128. }
  129. return static::findOne([
  130. 'password_reset_token' => $token,
  131. 'status' => self::STATUS_ACTIVE,
  132. ]);
  133. }
  134. /**
  135. * 发现如果密码重置令牌是有效的
  136. *
  137. * @param string $token password reset token
  138. * @return boolean
  139. */
  140. public static function isPasswordResetTokenValid($token)
  141. {
  142. if (empty($token)) {
  143. return false;
  144. }
  145. $timestamp = (int)substr($token, strrpos($token, '_') + 1);
  146. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  147. return $timestamp + $expire >= time();
  148. }
  149. /**
  150. * @inheritdoc
  151. */
  152. /**
  153. * 设置密码
  154. *
  155. * @param $password
  156. * @throws \yii\base\Exception
  157. */
  158. public function setPassword($password)
  159. {
  160. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  161. }
  162. /**
  163. * 设置授权码
  164. *
  165. * @throws \yii\base\Exception
  166. */
  167. public function generateAuthKey()
  168. {
  169. $this->auth_key = Yii::$app->security->generateRandomString();
  170. }
  171. /**
  172. * 设置密码重置秘钥
  173. *
  174. * @throws \yii\base\Exception
  175. */
  176. public function generatePasswordResetToken()
  177. {
  178. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  179. }
  180. /**
  181. * 设置密码重置秘钥为空
  182. */
  183. public function removePasswordResetToken()
  184. {
  185. $this->password_reset_token = null;
  186. }
  187. /**
  188. * 关联授权角色
  189. *
  190. * @return \yii\db\ActiveQuery
  191. */
  192. public function getAssignment()
  193. {
  194. return $this->hasOne(AuthAssignment::class, ['user_id' => 'id'])
  195. ->where(['app_id' => Yii::$app->id]);
  196. }
  197. }