Yii Framework 2.0用户数据库登录 [英] Yii Framework 2.0 Login With User Database

查看:159
本文介绍了Yii Framework 2.0用户数据库登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图在互联网上搜索如何编写Yii框架2.0中的代码,以便用户可以使用存储在数据库中的凭据登录,而不是从数组,前缀在models / User.php。我知道如何做在Yii 1。但在Yii 2.0,我真的不知道如何这样做。因为Yii 2.0还没有发布(只有beta版本可用),我没有找到许多Yii 2.0教程在Internet上关于登录数据库。请帮我这个!非常感谢!

I have been trying to search on the Internet how to write the code in Yii framework 2.0 so that user can login with the credentials stored in the database and not from the array, prefixed in models/User.php. I know how to do that in Yii 1. But in Yii 2.0, I really don't know how to do that. Since Yii 2.0 has not been released yet (only the beta version is available), I could not find many Yii 2.0 tutorials on the Internet about logging-in with the database. Please help me with this! Thanks in advanced!

推荐答案

您可以使用 https://github.com/amnah/yii2-user

如果您想编写自己的自定义脚本来管理用户,您可以覆盖Yii2 identityClass。

If you want to write your own custom script to manage the users you can override Yii2 identityClass.

您的配置添加:

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

请注意您的用户模型MUST IMPLEMENT \yii \ web \IdentityInterface

Please note that your user model MUST IMPLEMENT \yii\web\IdentityInterface

以下是可用于实现数据库身份验证的模型类的示例

Here is the example of the model class that you can use to implement database authentication



namespace app\models;

//app\models\gii\Users is the model generated using Gii from users table

use app\models\gii\Users as DbUser;

class User extends \yii\base\Object implements \yii\web\IdentityInterface {

public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
public $email;
public $phone_number;
public $user_type;

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

/**
 * @inheritdoc
 */
public static function findIdentityByAccessToken($token, $userType = null) {

    $dbUser = DbUser::find()
            ->where(["accessToken" => $token])
            ->one();
    if (!count($dbUser)) {
        return null;
    }
    return new static($dbUser);
}

/**
 * Finds user by username
 *
 * @param  string      $username
 * @return static|null
 */
public static function findByUsername($username) {
    $dbUser = DbUser::find()
            ->where([
                "username" => $username
            ])
            ->one();
    if (!count($dbUser)) {
        return null;
    }
    return new static($dbUser);
}

/**
 * @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;
}

}

我希望这将有助于你。干杯:)

I hope that would be helpful to you . Cheers :)

这篇关于Yii Framework 2.0用户数据库登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆