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

Php 针对两个不同的数据库表进行身份验证,php,yii2,Php,Yii2,我对Yii 2.0中的身份验证过程有点困惑。我正在开发一个web应用程序,它有两种用户—学生和讲师。每个实体都有自己的数据库表MySQL,如果这与它自己的id、用户名和密码字段有关的话。我已经研究了高级应用程序模板,其中包括针对数据库的身份验证,但在这种情况下,用户表是唯一的。在我的例子中,我必须能够确定要查找用户记录student或讲师的数据库表。标识接口: interface IdentityInterface { /** * Finds an identity by the

我对Yii 2.0中的身份验证过程有点困惑。我正在开发一个web应用程序,它有两种用户—学生和讲师。每个实体都有自己的数据库表MySQL,如果这与它自己的id、用户名和密码字段有关的话。我已经研究了高级应用程序模板,其中包括针对数据库的身份验证,但在这种情况下,用户表是唯一的。在我的例子中,我必须能够确定要查找用户记录student或讲师的数据库表。标识接口:

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);
}
包含FindDidEntity方法,该方法是静态的。我之所以这么说,是因为我在传递额外的参数或访问实现此接口的app\models\User类中的实例变量时遇到了问题,该类将区分用于用户身份验证的数据库表。在我的例子中,findIdentity中的$id参数不是唯一的

如何找到解决方案?

我看到了3个选项:

1如果学生表和讲师表的结构没有绝对不同,那么最好使用具有类型列的公共表用户。如果需要,您可以为它们使用不同的模型,请参见

2由于这是一种非通用和非普遍的方法,您可以覆盖核心类

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

除了用户模型之外,不要忘记搜索所有框架类以查找FindDidEntity的用法,并用您的实现替换它

如我所见,它仅在loginByCookie和RenewalAuthStatus保护方法中的yii\web\User中调用

还要重写该类,然后将用户组件配置中的类切换到自定义类

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


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

学生和讲师表的结构是否完全不同?可能只是使用具有类型column的公共表用户?如果需要,您可以为它们使用不同的模型。否则,我认为您应该使用覆盖框架类来解决这个问题,因为这是一种非常见且普遍的方法。我想这将是一个解决方案,为所有用户提供相同的表,因为它们都包含基本字段id、用户名、密码,并且为每个用户类型字段提供不同的表。但我想要一个比这更干净的解决方案。我将如何对两个不同的表实现身份验证?我指的是非普遍的方法。经过进一步考虑,解决方案1是可行的。谢谢。我认为你做出了正确的选择,在我看来,这绝对是更好的。你能提供一个关于解决方案2的例子吗?你到底需要知道什么?我试图提及该方法的所有重要细节。如果我尝试创建一个扩展yii\web\IdentityInterface的新接口并尝试重写findIdentity静态方法,我会得到PHP错误:如果我尝试添加新参数,声明必须与IdentityInterface::findIdentityid:int | string兼容。因为这个方法是静态的,所以我不能以非静态的方式引用接口变量,我想这就是问题所在。有什么建议吗?