| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace common\models\base;
- use Yii;
- use yii\base\NotSupportedException;
- use yii\db\ActiveRecord;
- use yii\web\IdentityInterface;
- use yii\behaviors\TimestampBehavior;
- use common\models\rbac\AuthAssignment;
- /**
- * User model
- *
- * @property integer $id
- * @property string $username
- * @property string $password_hash
- * @property string $password_reset_token
- * @property string $email
- * @property string $auth_key
- * @property integer $status
- * @property integer $created_at
- * @property integer $updated_at
- * @property string $password write-only password
- */
- class User extends BaseActiveRecord implements IdentityInterface
- {
- /**
- * 禁用登录
- */
- const STATUS_DELETED = 0;
- /**
- * 正常登录
- */
- const STATUS_ACTIVE = 1;
- /**
- * 规则
- */
- const ROLE_ADMIN = 10;
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- TimestampBehavior::class,
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- ['status', 'default', 'value' => self::STATUS_ACTIVE],
- ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
- ];
- }
- /**
- * @inheritdoc
- */
- public static function findIdentity($id)
- {
- return static::find($id);
- }
- /**
- * @param mixed $token
- * @param null $type
- * @return void|IdentityInterface
- * @throws NotSupportedException
- */
- public static function findIdentityByAccessToken($token, $type = null)
- {
- return static::findOne(['access_token' => $token]);
- // throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
- }
-
- public function getId()
- {
- return $this->getPrimaryKey();
- }
- /**
- * 获取授权码
- * @return string
- */
- public function getAuthKey()
- {
- return $this->auth_key;
- }
- /**
- * 验证授权码
- *
- * @inheritdoc
- */
- public function validateAuthKey($authKey)
- {
- return $this->getAuthKey() === $authKey;
- }
- /**
- * 验证密码是否正确
- *
- * @param $password
- * @return bool
- */
- public function validatePassword($password)
- {
- return Yii::$app->security->validatePassword($password, $this->password);
- }
- /**
- * 通过用户名查找用户
- *
- * @param $username
- * @return array|null|ActiveRecord
- */
- public static function findByUsername($username)
- {
- return static::find()->where([
- 'username' => $username,
- 'status' => self::STATUS_ACTIVE,
- ])->one();
- }
- /**
- * 查找密码重置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_reset_token' => $token,
- 'status' => self::STATUS_ACTIVE,
- ]);
- }
- /**
- * 发现如果密码重置令牌是有效的
- *
- * @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
- */
-
-
- /**
- * 设置密码
- *
- * @param $password
- * @throws \yii\base\Exception
- */
- public function setPassword($password)
- {
- $this->password_hash = Yii::$app->security->generatePasswordHash($password);
- }
- /**
- * 设置授权码
- *
- * @throws \yii\base\Exception
- */
- public function generateAuthKey()
- {
- $this->auth_key = Yii::$app->security->generateRandomString();
- }
- /**
- * 设置密码重置秘钥
- *
- * @throws \yii\base\Exception
- */
- public function generatePasswordResetToken()
- {
- $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
- }
- /**
- * 设置密码重置秘钥为空
- */
- public function removePasswordResetToken()
- {
- $this->password_reset_token = null;
- }
- /**
- * 关联授权角色
- *
- * @return \yii\db\ActiveQuery
- */
- public function getAssignment()
- {
- return $this->hasOne(AuthAssignment::class, ['user_id' => 'id'])
- ->where(['app_id' => Yii::$app->id]);
- }
- }
|