Heim  >  Artikel  >  Backend-Entwicklung  >  yii2 ändert das Anmeldemodul in der Standarddemo, um eine Verbindung zur Datenbank herzustellen

yii2 ändert das Anmeldemodul in der Standarddemo, um eine Verbindung zur Datenbank herzustellen

巴扎黑
巴扎黑Original
2016-11-11 10:46:341118Durchsuche

PHP-Code

<?php  
  
namespace app\models;  
  
class User extends \yii\base\Object implements \yii\web\IdentityInterface  
{  
    public $id;  
    public $username;  
    public $password;  
    public $authKey;  
    public $accessToken;  
  
    private static $users = [  
        &#39;100&#39; => [  
            &#39;id&#39; => &#39;100&#39;,  
            &#39;username&#39; => &#39;admin&#39;,  
            &#39;password&#39; => &#39;admin&#39;,  
            &#39;authKey&#39; => &#39;test100key&#39;,  
            &#39;accessToken&#39; => &#39;100-token&#39;,  
        ],  
    ];  
  
    /** 
     * @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[&#39;accessToken&#39;] === $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[&#39;username&#39;], $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;  
    }  
}

kann in den folgenden Code

PHP-Code

<?php  
  
namespace app\models;  
  
use Yii;  
  
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface  
{  
    public $id;  
    public $username;  
    public $password;  
    public $authKey;  
    public $accessToken;  
  
    static private $AUTH_KEY = "authkey_test"; //remember me need this  
    static private $ACCESS_TOKEN = "accesstoken_test"; // remember me need this  
  
    public static function tableName()  
    {  
        return &#39;tb_admin&#39;;  
    }  
  
    /** 
     * @return \yii\db\Connection the database connection used by this AR class. 
     */  
    public static function getDb()  
    {  
        return Yii::$app->get(&#39;db_admin&#39;);  
    }  
  
    /** 
     * Finds user by username 
     * 
     * @param  string      $username 
     * @return static|null 
     */  
    public static function findByUsername($username)  
    {  
    $user = User::find()->where([&#39;username&#39;=>$username])->asArray()->one();  
    if(isset($user)){  
            $user[&#39;authKey&#39;] = self::$AUTH_KEY;  
            $user[&#39;accessToken&#39;] = 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([&#39;id&#39;=>$id])->asArray()->one();  
    if(isset($user)){  
            $user[&#39;authKey&#39;] = self::$AUTH_KEY;  
            $user[&#39;accessToken&#39;] = 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;  
    }  
}


geändert werden
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn