美文网首页
yii2 用户表与用户登录

yii2 用户表与用户登录

作者: 顺子_aba3 | 来源:发表于2022-02-16 12:26 被阅读0次
    CREATE TABLE [user](
      [id] integer PRIMARY KEY AUTOINCREMENT UNIQUE, 
      [username] varchar(255) NOT NULL, 
      [auth_key] varchar(32) NOT NULL, 
      [password_hash] varchar(255) NOT NULL, 
      [password_reset_token] varchar(255) DEFAULT NULL, 
      [email] varchar(255) NOT NULL, 
      [role] smallint(6) NOT NULL DEFAULT 10, 
      [status] smallint(6) NOT NULL DEFAULT 10, 
      [created_at] int(11) NOT NULL, 
      [updated_at] int(11) NOT NULL);
    
    <?php
    
    namespace app\models;
    namespace app\models;
    
    class User extends /*\yii\base\Object*/ \yii\db\ActiveRecord implements \yii\web\IdentityInterface
    {
        /*public $id;
        public $username;
        public $password;
        public $authKey;
        public $accessToken;
    
        private static $users = [
            '100' => [
                'id' => '100',
                'username' => 'admin',
                'password' => 'admin',
                'authKey' => 'test100key',
                'accessToken' => '100-token',
            ],
            '101' => [
                'id' => '101',
                'username' => 'demo',
                'password' => 'demo',
                'authKey' => 'test101key',
                'accessToken' => '101-token',
            ],
        ];
        */
    
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'user';
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['username', 'password'], 'required'],
                [['username'], 'string', 'max' => 50],
                [['password'], 'string', 'max' => 32],
                [['authKey'], 'string', 'max' => 100],
                [['accessToken'], 'string', 'max' => 100],
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id' => 'ID',
                'username' => 'Username',
                'password' => 'Password',
                'authKey' => 'AuthKey',
                'accessToken' => 'AccessToken',
            ];
        }
    
        /**
         * @inheritdoc
         */
        public static function findIdentity($id)
        {
            return static::findOne($id);
            //return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
        }
    
        /**
         * @inheritdoc
         */
        public static function findIdentityByAccessToken($token, $type = null)
        {
            return static::findOne(['access_token' => $token]);
            /*foreach (self::$users as $user) {
                if ($user['accessToken'] === $token) {
                    return new static($user);
                }
            }
    
            return null;*/
        }
    
        /**
         * Finds user by username
         *
         * @param  string      $username
         * @return static|null
         */
        public static function findByUsername($username)
        {
              $user = User::find()
                ->where(['username' => $username])
                ->asArray()
                ->one();
    
                if($user){
                return new static($user);
            }
    
            return null;
            /*foreach (self::$users as $user) {
                if (strcasecmp($user['username'], $username) === 0) {
                    return new static($user);
                }
            }
    
            return null;*/
        }
    
        /**
         * @inheritdoc
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @inheritdoc
         */
        public function getAuthKey()
        {
            return $this->authKey;
        }
    
        /**
         * @inheritdoc
         */
        public function validateAuthKey($authKey)
        {
            return $this->authKey === $authKey;
        }
    
        /**
         * Validates password
         *
         * @param  string  $password password to validate
         * @return boolean if password provided is valid for current user
         */
        public function validatePassword($password)
        {
            return $this->password === $password;
        }
    }
    ```php

    相关文章

      网友评论

          本文标题:yii2 用户表与用户登录

          本文链接:https://www.haomeiwen.com/subject/wizrlrtx.html