针对两个不同的数据库表进行身份验证

Authenticating against two different database tables

我对 Yii 2.0 中的身份验证过程有点困惑。我正在开发一个有两种用户(学生和讲师)的网络应用程序。每个实体都有自己的数据库 table(MySQL,如果重要)有自己的 ID、用户名和密码字段。我查看了包含针对数据库的身份验证的高级应用程序模板,但在那种情况下,用户 table 是唯一的。在我的例子中,我必须能够确定哪个数据库 table 来查找用户记录(学生或讲师)。身份接口:

interface IdentityInterface
{
    /**
    * Finds an identity by the given ID.
    * @param string|integer $id the ID to be looked for
    * @return IdentityInterface the identity object that matches the given ID.
    * Null should be returned if such an identity cannot be found
    * or the identity is not in an active state (disabled, deleted, etc.)
    */
    public static function findIdentity($id);
    /**
    * Finds an identity by the given token.
    * @param mixed $token the token to be looked for
    * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
    * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
    * @return IdentityInterface the identity object that matches the given token.
    * Null should be returned if such an identity cannot be found
    * or the identity is not in an active state (disabled, deleted, etc.)
    */
    public static function findIdentityByAccessToken($token, $type = null);
    /**
    * Returns an ID that can uniquely identify a user identity.
    * @return string|integer an ID that uniquely identifies a user identity.
    */
    public function getId();
    /**
    * Returns a key that can be used to check the validity of a given identity ID.
    *
    * The key should be unique for each individual user, and should be persistent
    * so that it can be used to check the validity of the user identity.
    *
    * The space of such keys should be big enough to defeat potential identity attacks.
    *
    * This is required if [[User::enableAutoLogin]] is enabled.
    * @return string a key that is used to check the validity of a given identity ID.
    * @see validateAuthKey()
    */
    public function getAuthKey();
    /**
    * Validates the given auth key.
    *
    * This is required if [[User::enableAutoLogin]] is enabled.
    * @param string $authKey the given auth key
    * @return boolean whether the given auth key is valid.
    * @see getAuthKey()
    */
    public function validateAuthKey($authKey);
}

包含 findIdentity() 方法,不幸的是它是静态的。我这样说是因为我在传递额外参数或从实现此接口的 app\models\User class 访问实例变量时遇到问题,这将区分数据库 table 以进行用户身份验证。在我的例子中 findIdentity() 中的 $id 参数是 不是 唯一的。

如何找到解决方案?

我看到 3 个选项:

1)如果studentslecturerstables的结构没有绝对的区别,也许用common更好table userstype 列。如果需要,您可以为它们使用不同的模型(参见 )。

2) 因为它是不常见和非广泛的方法,你可以覆盖核心 classes.

使用不同的命名空间创建 IdentityInterface 的副本并更改 findIdentity() 声明以满足您的需要。然后使用您的自定义界面而不是内置界面。

除了 User 模型不要忘记搜索所有框架 classes 以查找 findIdentity() 的用法并将其替换为您的实现。

据我所知,它仅在 yii\web\User 中的 loginByCookie()renewAuthStatus() 受保护方法中调用。

同时覆盖此 class,然后将 user 组件配置中的 class 切换为您的自定义配置。

3) 以其他方式传递附加参数。例如 Yii::$app->params。不过似乎是一种黑客解决方案。

我个人建议使用第一种或第三种方法(以防您反对使用第一种方法)。