Yii2 modify the default logon module in the demo to connect to the database

Source: Internet
Author: User
: This article describes how to modify the default logon module in the demo of yii2 to connect to the database. For more information about PHP tutorials, see.
  [            'id' => '100',            'username' => 'admin',            'password' => 'admin',            'authKey' => 'test100key',            'accessToken' => '100-token',        ],    ];    /**     * @inheritdoc     */    public static function findIdentity($id)    {        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;    }    /**     * @inheritdoc     */    public static function findIdentityByAccessToken($token, $type = null)    {        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)    {        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;    }}

Change the code as follows:

 get('db_admin');    }    /**     * Finds user by username     *     * @param  string      $username     * @return static|null     */    public static function findByUsername($username)    {$user = User::find()->where(['username'=>$username])->asArray()->one();if(isset($user)){            $user['authKey'] = self::$AUTH_KEY;            $user['accessToken'] = self::$ACCESS_TOKEN;}return isset($user) ? new static($user) : null;    }    /**     * 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;    }    /**     * @inheritdoc     */    public function getId()    {        return $this->id;    }    /**     * @inheritdoc     */    public function getAuthKey()    {        return $this->authKey;    }    /**     * @inheritdoc     */    public static function findIdentity($id)    {$user = User::find()->where(['id'=>$id])->asArray()->one();if(isset($user)){            $user['authKey'] = self::$AUTH_KEY;            $user['accessToken'] = self::$ACCESS_TOKEN;}return isset($user) ? new static($user) : null;    }    /**     * @inheritdoc     */    public static function findIdentityByAccessToken($token, $type = null)    {return null;    }    /**     * @inheritdoc     */    public function validateAuthKey($authKey)    {        return $this->authKey === $authKey;    }}

The above describes how to modify the default logon module in the demo of yii2 to connect to the database, including some content, and hope to help friends who are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.