where(['uid' => $uid, 'status' => self::STATUS_ACTIVE])->one(); } /** * 根据用户名获取账号信息 * * @param string $username * @return array|null|\yii\db\ActiveRecord */ public static function findByUsername($username) { return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); } /** * Finds user by password reset token * * @param string $token password reset token * @return static|null */ public static function findByPasswordResetToken($token) { if (!static::isPasswordResetTokenValid($token)) { return null; } return static::findOne([ 'password' => $token, 'status' => self::STATUS_ACTIVE, ]); } /** * Finds out if password reset token is valid * * @param string $token password reset token * @return boolean */ public static function isPasswordResetTokenValid($token) { if (empty($token)) { return false; } $timestamp = (int) substr($token, strrpos($token, '_') + 1); $expire = Yii::$app->params['user.passwordResetTokenExpire']; return $timestamp + $expire >= time(); } /** * @inheritdoc */ public function getId() { return $this->getPrimaryKey(); } /** * @inheritdoc */ public function getAuthKey() { return $this->salt; } /** * @inheritdoc */ public function validateAuthKey($authKey) { return $this->getAuthKey() === $authKey; } /** * 验证密码 * * @param string $password password to validate * @return boolean if password provided is valid for current user */ public function validatePassword($password) { return Yii::$app->security->validatePassword($password, $this->password); } /** * 设置加密后的密码 * * @param string $password */ public function setPassword($password) { $this->password = Yii::$app->security->generatePasswordHash($password); } /** * 设置密码干扰码 */ public function generateAuthKey() { $this->salt = Yii::$app->security->generateRandomString(); } /** * Generates new password reset token */ public function generatePasswordResetToken() { $this->password = Yii::$app->security->generateRandomString() . '_' . time(); } /** * Removes password reset token */ public function removePasswordResetToken() { $this->password = null; } /** * @inheritdoc */ public static function findIdentityByAccessToken($token, $type = null) { $user = static::find()->where(['access_token' => $token, 'status' => self::STATUS_ACTIVE])->one(); if (!$user) { return false; } // if ($user->expire_at < time()) { // throw new UnauthorizedHttpException('the access - token expired ', -1); // } else { // return $user; // } return $user; } public function loginByAccessToken($accessToken, $type) { return static::findIdentityByAccessToken($accessToken, $type); } /** * Generate accessToken string * @return string * @throws \yii\base\Exception */ public function generateAccessToken() { $this->access_token = Yii::$app->security->generateRandomString(); return $this->access_token; } }