AdminUser.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 fecadmin\models;
  10. use Yii;
  11. use yii\base\NotSupportedException;
  12. use yii\behaviors\TimestampBehavior;
  13. use yii\db\ActiveRecord;
  14. use yii\web\IdentityInterface;
  15. use yii\filters\RateLimitInterface;
  16. /**
  17. * User model
  18. *
  19. * @property integer $id
  20. * @property string $username
  21. * @property string $password_hash
  22. * @property string $password_reset_token
  23. * @property string $email
  24. * @property string $auth_key
  25. * @property integer $status
  26. * @property integer $created_at
  27. * @property integer $updated_at
  28. * @property string $password write-only password
  29. */
  30. /**
  31. * @author Terry Zhao <2358269014@qq.com>
  32. * @since 1.0
  33. */
  34. class AdminUser extends ActiveRecord implements IdentityInterface ,RateLimitInterface
  35. {
  36. const STATUS_DELETED = 10;
  37. const STATUS_ACTIVE = 1;
  38. # 速度控制 6秒内访问3次,注意,数组的第一个不要设置1,设置1会出问题,一定要
  39. #大于2,譬如下面 6秒内只能访问三次
  40. # 文档标注:返回允许的请求的最大数目及时间,例如,[100, 600] 表示在600秒内最多100次的API调用。
  41. public function getRateLimit($request, $action){
  42. return [6000000, 6];
  43. }
  44. # 文档标注: 返回剩余的允许的请求和相应的UNIX时间戳数 当最后一次速率限制检查时。
  45. public function loadAllowance($request, $action){
  46. //return [1,strtotime(date("Y-m-d H:i:s"))];
  47. //echo $this->allowance;exit;
  48. return [$this->allowance, $this->allowance_updated_at];
  49. }
  50. # allowance 对应user 表的allowance字段 int类型
  51. # allowance_updated_at 对应user allowance_updated_at int类型
  52. # 文档标注:保存允许剩余的请求数和当前的UNIX时间戳。
  53. public function saveAllowance($request, $action, $allowance, $timestamp){
  54. $this->allowance = $allowance;
  55. $this->allowance_updated_at = $timestamp;
  56. $this->save();
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. # 设置 status 默认 ,以及取值的区间
  62. public function rules()
  63. {
  64. return [
  65. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  66. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  67. ];
  68. }
  69. public function attributeLabels()
  70. {
  71. return [
  72. 'username' => '用户名',
  73. 'password_hash' => '密码',
  74. 'password_reset_token' => '重置密码Token',
  75. 'auth_key' => 'Auth Key',
  76. 'status' => '激活状态',
  77. 'email' => '邮箱地址',
  78. 'created_at' => '创建时间INT',
  79. 'updated_at' => '更新时间INT',
  80. //'role' => '权限',
  81. 'access_token ' => '访问令牌',
  82. 'created_at_datetime' => '创建时间',
  83. 'updated_at_datetime' => '更新时间',
  84. ];
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. # 设置table
  90. public static function tableName()
  91. {
  92. return '{{%admin_user}}';
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function behaviors()
  98. {
  99. return [
  100. TimestampBehavior::className(),
  101. ];
  102. }
  103. /**
  104. * @inheritdoc
  105. */
  106. # 通过id 找到identity
  107. public static function findIdentity($id)
  108. {
  109. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  110. }
  111. /**
  112. * @inheritdoc
  113. */
  114. # 通过access_token 找到identity
  115. public static function findIdentityByAccessToken($token, $type = null)
  116. {
  117. return static::findOne(['access_token' => $token, 'status' => self::STATUS_ACTIVE]);
  118. }
  119. # 生成access_token
  120. public function generateAccessToken()
  121. {
  122. $this->access_token = Yii::$app->security->generateRandomString();
  123. }
  124. /**
  125. * Finds user by username
  126. *
  127. * @param string $username
  128. * @return static|null
  129. */
  130. public static function findByUsername($username)
  131. {
  132. return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  133. }
  134. /**
  135. * Finds user by password reset token
  136. *
  137. * @param string $token password reset token
  138. * @return static|null
  139. */
  140. # 此处是忘记密码所使用的
  141. public static function findByPasswordResetToken($token)
  142. {
  143. if (!static::isPasswordResetTokenValid($token)) {
  144. return null;
  145. }
  146. return static::findOne([
  147. 'password_reset_token' => $token,
  148. 'status' => self::STATUS_ACTIVE,
  149. ]);
  150. }
  151. /**
  152. * Finds out if password reset token is valid
  153. *
  154. * @param string $token password reset token
  155. * @return boolean
  156. */
  157. public static function isPasswordResetTokenValid($token)
  158. {
  159. if (empty($token)) {
  160. return false;
  161. }
  162. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  163. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  164. return $timestamp + $expire >= time();
  165. }
  166. /**
  167. * @inheritdoc
  168. */
  169. public function getId()
  170. {
  171. return $this->getPrimaryKey();
  172. }
  173. /**
  174. * @inheritdoc
  175. */
  176. public function getAuthKey()
  177. {
  178. return $this->auth_key;
  179. }
  180. /**
  181. * @inheritdoc
  182. */
  183. public function validateAuthKey($authKey)
  184. {
  185. return $this->getAuthKey() === $authKey;
  186. }
  187. /**
  188. * Validates password
  189. *
  190. * @param string $password password to validate
  191. * @return boolean if password provided is valid for current user
  192. */
  193. public function validatePassword($password)
  194. {
  195. return Yii::$app->security->validatePassword($password, $this->password_hash);
  196. }
  197. /**
  198. * Generates password hash from password and sets it to the model
  199. *
  200. * @param string $password
  201. */
  202. public function setPassword($password)
  203. {
  204. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  205. }
  206. /**
  207. * Generates "remember me" authentication key
  208. */
  209. public function generateAuthKey()
  210. {
  211. $this->auth_key = Yii::$app->security->generateRandomString();
  212. }
  213. /**
  214. * Generates new password reset token
  215. */
  216. public function generatePasswordResetToken()
  217. {
  218. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  219. }
  220. /**
  221. * Removes password reset token
  222. */
  223. public function removePasswordResetToken()
  224. {
  225. $this->password_reset_token = null;
  226. }
  227. }