Yii2 Login with database

2019-07-13 02:28发布

问题:

I have a table in my DB called 'member' where I intend to store username, password and all other related info of a user and I want to use those username/password for login instead yii2's default User.php model. I have been trying for almost a day and modified the Member.php model but can't make it work. Every time I use my custom username/password from db, it says username or password is incorrect. Can anyone please help me out? Thanks in advance. :)

FYI, I have no such field in member table such as authKey or accessToken. I have tried all the related stackoverflow posts but in vein.

Member.php Model

namespace app\models;
use Yii;
use yii\web\IdentityInterface;

class Member extends \yii\db\ActiveRecord implements IdentityInterface
{
    public static function tableName()
    {
        return 'member';
    }

    public function rules()
    {
        return [
            [['username', 'password', 'first_name', 'last_name', 'role'], 'required'],
            [['created_by_date', 'last_modified_by_date'], 'safe'],
            [['username', 'password', 'role', 'created_by_id', 'last_modified_by_id'], 'string', 'max' => 50],
            [['first_name', 'last_name', 'middle_name', 'phone', 'mobile'], 'string', 'max' => 100],
            [['email'], 'string', 'max' => 250],
            [['address_line1', 'address_line2', 'address_line3'], 'string', 'max' => 512]
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'middle_name' => 'Middle Name',
            'email' => 'Email',
            'phone' => 'Phone',
            'mobile' => 'Mobile',
            'address_line1' => 'Address Line1',
            'address_line2' => 'Address Line2',
            'address_line3' => 'Address Line3',
            'role' => 'Role',
            'created_by_id' => 'Created By ID',
            'created_by_date' => 'Created By Date',
            'last_modified_by_id' => 'Last Modified By ID',
            'last_modified_by_date' => 'Last Modified By Date',
        ];
    }

    public static function find()
    {
        return new MemberQuery(get_called_class());
    }

    public static function findIdentity($id) 
    {
        $dbUser = self::find()
            ->where([
                "id" => $id
            ])
            ->one();
        if (!count($dbUser)) {
            return null;
        }
        return new static($dbUser);
    }

    public static function findIdentityByAccessToken($token, $userType = null) 
    {
        $dbUser = self::find()
            ->where(["accessToken" => $token])
            ->one();
        if (!count($dbUser)) {
            return null;
        }
        return new static($dbUser);
    }


    public static function findByUsername($username) 
    {
        $dbUser = self::find()
            ->where(["username" => $username])
            ->one();
        if (!count($dbUser)) {
            return null;
        }
        return $dbUser;
    }

    public function getId() 
    {
        return $this->id;
    }

    public function getAuthKey() 
    {
        return $this->authKey;
    }

    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;
    }
}

config/web.php

'user' => [
        'identityClass' => 'app\models\Member',
        'enableAutoLogin' => true,
    ],

I didnt change the User.php model. Here it is:

namespace app\models;

class User extends \yii\base\Object implements \yii\web\IdentityInterface
{
    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 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;
}
}

回答1:

You should make sure you change the getUser() method on models/LoginForm.php to use your Member model class, otherwise it will keep validating against the default User model.

public function getUser() {
    if ($this->_user === false) {
        $this->_user = Member::findByUsername($this->username);
    }
    return $this->_user;
}

Also, here is an example of my own User model class

namespace app\models;

use Yii;

class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface {
    const SCENARIO_LOGIN = 'login';
    const SCENARIO_CREATE = 'create';

    public static function tableName() {
        return 'user';
    }

    public function scenarios() {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_LOGIN] = ['username', 'password'];
        $scenarios[self::SCENARIO_CREATE] = ['username', 'password', 'authKey'];
        return $scenarios;
    }

    public function rules() {
        return [
            [['username', 'email'], 'string', 'max' => 45],
            [['email'], 'email'],
            [['password'], 'string', 'max' => 60],
            [['authKey'], 'string', 'max' => 32],

            [['username', 'password', 'email'], 'required', 'on' => self::SCENARIO_CREATE],
            [['authKey'], 'default', 'on' => self::SCENARIO_CREATE, 'value' => Yii::$app->getSecurity()->generateRandomString()],
            [['password'], 'filter', 'on' => self::SCENARIO_CREATE, 'filter' => function($value) {
                return Yii::$app->getSecurity()->generatePasswordHash($value);
            }],

            [['username', 'password'], 'required', 'on' => self::SCENARIO_LOGIN],

            [['username'], 'unique'],
            [['email'], 'unique'],
            [['authKey'], 'unique'],
        ];
    }

    public function attributeLabels() {
        return [
            'id' => 'Id',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'authKey' => 'authKey',
        ];
    }

    public static function findIdentity($id) {
        return self::findOne($id);
    }

    public static function findIdentityByAccessToken($token, $type = null) {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    public static function findByUsername($username) {
        return static::findOne(['username' => $username]);
    }

    public function getId() {
        return $this->getPrimaryKey();
    }

    public function getAuthKey() {
        return $this->authKey;
    }

    public function validateAuthKey($authKey) {
        return $this->authKey === $authKey;
    }

    public function validatePassword($password) {
        return Yii::$app->getSecurity()->validatePassword($password, $this->password);
    }
}

Make sure the methods from IdentityInterface you implement but don't want to use throw an exception, just like i do on the findIdentityByAccessToken method.



回答2:

You should extend User class with your Member class and set in main config:

[...]
'modules' => [
        'user' => [
            'class' => 'member class
            'modelMap' => [
                'User' => 'app\models\member',

More info: yii 2 : override user model