User.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午1:02
  7. */
  8. namespace backendApi\modules\v1\models;
  9. use common\helpers\Date;
  10. use common\helpers\Tool;
  11. use Yii;
  12. use yii\web\IdentityInterface;
  13. class User extends Admin implements IdentityInterface
  14. {
  15. /**
  16. * @param mixed $token
  17. * @param null $type
  18. * @return null|IdentityInterface|static
  19. */
  20. public static function findIdentityByAccessToken($token, $type = null)
  21. {
  22. // 从redis中把 token 找到
  23. return Yii::$app->tokenRedis->hget($token, 'ID');
  24. // if($userId){
  25. // return static::findOne(['ID' => $userId]);
  26. // }
  27. // return null;
  28. }
  29. public function fields()
  30. {
  31. $fields = parent::fields();
  32. // 删除一些包含敏感信息的字段
  33. unset($fields['PASSWORD_HASH'], $fields['PASSWORD_RESET_TOKEN'], $fields['AUTH_KEY']);
  34. return $fields;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function behaviors()
  40. {
  41. return [
  42. 'yii\behaviors\TimestampBehavior',
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public static function findIdentity($id)
  49. {
  50. return static::findOne(['ID' => $id]);
  51. }
  52. /**
  53. * Finds user by adminName
  54. *
  55. * @param string $adminName
  56. * @return static|null
  57. */
  58. public static function findByUsername($adminName)
  59. {
  60. return static::findOne(['ADMIN_NAME' => $adminName,'IS_DEL'=>0]);
  61. }
  62. /**
  63. * 通过用户名获取信息并带着token表内容
  64. * @param $adminName
  65. * @return array
  66. */
  67. public static function findByUsernameWithToken($adminName){
  68. $result = static::find()->select(static::tableName().'.*,T.ACCESS_TOKEN,T.REFRESH_TOKEN,T.CREATED_AT,T.UPDATED_AT')->join('LEFT JOIN', AdminToken::tableName().' AS T', static::tableName().'.ID=T.ADMIN_ID')->where(static::tableName().'.ADMIN_NAME=:ADMIN_NAME', ['ADMIN_NAME'=>$adminName])->asArray()->one();
  69. return $result;
  70. }
  71. /**
  72. * 静态方法校验两个密码
  73. * @param $password
  74. * @param $validatePassword
  75. * @return bool
  76. */
  77. public static function validatePasswordStatic($password, $validatePassword)
  78. {
  79. return Yii::$app->security->validatePassword($password, $validatePassword);
  80. }
  81. /**
  82. * 生成PCAccessToken
  83. * @param $appType (pc|app)
  84. * @return string
  85. * @throws \yii\base\Exception
  86. */
  87. public static function generateAccessToken($appType){
  88. $appTypeUper = strtoupper($appType);
  89. // 从redis的AccessTokenIncr中自增一个值
  90. $incrValue = Yii::$app->tokenRedis->incr($appTypeUper.'AccessTokenIncr');
  91. // apa(admin_pc_access)
  92. return md5('a'.$appType[0].'a_' . Yii::$app->security->generateRandomString(8) . Date::nowTime() . $incrValue);
  93. }
  94. /**
  95. * 生成PCRefreshToken
  96. * @param $appType (pc|app)
  97. * @return string
  98. * @throws \yii\base\Exception
  99. */
  100. public static function generateRefreshToken($appType){
  101. $appTypeUper = strtoupper($appType);
  102. // 从redis的AccessTokenIncr中自增一个值
  103. $incrValue = Yii::$app->tokenRedis->incr($appTypeUper.'RefreshTokenIncr');
  104. // apr(admin_pc_access)
  105. return md5('a'.$appType[0].'r_' . Yii::$app->security->generateRandomString(8) . Date::nowTime() . $incrValue);
  106. }
  107. /**
  108. * 通过重设密码 token 找到用户
  109. * @param $token
  110. * @return null|static
  111. */
  112. public static function findByPasswordResetToken($token)
  113. {
  114. if (!static::isPasswordResetTokenValid($token)) {
  115. return null;
  116. }
  117. return static::findOne([
  118. 'PASSWORD_RESET_TOKEN' => $token,
  119. ]);
  120. }
  121. /**
  122. * Finds out if password reset token is valid
  123. *
  124. * @param string $token password reset token
  125. * @return bool
  126. */
  127. public static function isPasswordResetTokenValid($token)
  128. {
  129. if (empty($token)) {
  130. return false;
  131. }
  132. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  133. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  134. return $timestamp + $expire >= time();
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function getId()
  140. {
  141. return $this->getPrimaryKey();
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function getAuthKey()
  147. {
  148. return $this->AUTH_KEY;
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function validateAuthKey($authKey)
  154. {
  155. return $this->getAuthKey() === $authKey;
  156. }
  157. /**
  158. * Validates password
  159. *
  160. * @param string $password password to validate
  161. * @return bool if password provided is valid for current user
  162. */
  163. public function validatePassword($password)
  164. {
  165. return Yii::$app->security->validatePassword($password, $this->PASSWORD_HASH);
  166. }
  167. /**
  168. * Generates password hash from password and sets it to the model
  169. * @param $password
  170. * @throws \yii\base\Exception
  171. */
  172. public function setPassword($password)
  173. {
  174. $this->PASSWORD_HASH = Yii::$app->security->generatePasswordHash($password);
  175. }
  176. /**
  177. * Generates "remember me" authentication key
  178. * @throws \yii\base\Exception
  179. */
  180. public function generateAuthKey()
  181. {
  182. $this->AUTH_KEY = Yii::$app->security->generateRandomString();
  183. }
  184. /**
  185. * Generates new password reset token
  186. * @throws \yii\base\Exception
  187. */
  188. public function generatePasswordResetToken()
  189. {
  190. $this->PASSWORD_RESET_TOKEN = Yii::$app->security->generateRandomString() . '_' . Date::nowTime();
  191. }
  192. /**
  193. * Removes password reset token
  194. */
  195. public function removePasswordResetToken()
  196. {
  197. $this->PASSWORD_RESET_TOKEN = null;
  198. }
  199. }