当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

yii2修改默认demo中登录模块连接数据库

发表于: 2015-04-11   作者:ciaos   来源:转载   浏览次数:
PHP
摘要: <?php namespace app\models; class User extends \yii\base\Object implements \yii\web\IdentityInterface { public $id; public $username; public $password; public $authKey;
<?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 = [
        '100' => [
            '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;
    }
}

改为如下代码即可

<?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 'tb_admin';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->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;
    }
}

 

yii2修改默认demo中登录模块连接数据库

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
默认端口是3389(十进制) 1、 在运行里面输入:”regedit”,进入注册表,然后找到HKEY_LOCAL_MACHIN
默认端口是3389(十进制) 1、 在运行里面输入:”regedit”,进入注册表,然后找到HKEY_LOCAL_MACHIN
在Yii2的basic版本中默认是从一个数组验证用户名和密码,如何改为从数据表中查询验证呢?且数据库的
Drupal7自定义用户登录 转载:Drupal7的User Login用户登录教程 http://blog.163.com/ziyun_2006/bl
假如我们有两个模型:RouteMap和Homuncule,其中Homuncule是包含在RouteMap中。RouteMap上安装有用
假如我们有两个模型:RouteMap和Homuncule,其中Homuncule是包含在RouteMap中。RouteMap上安装有用
假如我们有两个模型:RouteMap和Homuncule,其中Homuncule是包含在RouteMap中。RouteMap上安装有用
wnmp中,mysql的默认密码修改。 给我们的印象是,以前mysql默认密码都为空,好久没折腾这个了,今天
1,首先链接到数据库 mysql -h 127.0.0.1 -uroot -p 2,选择数据库 use mysql; 3,修改user表的密码
“是否允许连接到数据库引擎”和“登录”的区别 sql server 2005中登录名的状态里面有个设置选项,
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号