Cakephp Auth 与多个“用户”表

Posted

技术标签:

【中文标题】Cakephp Auth 与多个“用户”表【英文标题】:Cakephp Auth with multiple "Users" tables 【发布时间】:2011-02-27 18:04:30 【问题描述】:

我想知道如何只处理一个身份验证过程和多个表中的“用户”。我有 4 个用户表:用户、管理员、艺术家、团队管理员,它们都有特定的字段,但我希望所有这些用户都能够通过主页上的一个表单进行连接,然后被重定向到他们的特定仪表板。

我认为重定向应该不是问题,添加的一些路由应该可以工作,但我真的不知道从哪里看/开始让这一切成为可能。

干杯, 尼古拉斯。

编辑:这是最终解决方案(感谢 deizel)

App::import('Component', 'Auth');
class SiteAuthComponent extends AuthComponent 

    function identify($user = null, $conditions = null) 
        $models = array('User', 'Admin', 'Artist');
        foreach ($models as $model) 
            $this->userModel = $model; // switch model
            $this->params["data"][$model] = $this->params["data"]["User"]; // switch model in params/data too
            $result = parent::identify($this->params["data"][$model], $conditions); // let cake do its thing
            if ($result) 
                return $result; // login success
            
        
        return null; // login failure
    

【问题讨论】:

为什么不为用户创建一个单表。您可以轻松地在表中为用户的“类型”定义一个单独的列。这样您就不需要为这些用户提供 4 种不同的模型和控制器? @Gaurav:嗨。我没有这样做首先是因为我在选择框架之前进行了分析,其次是因为艺术家和团队艺术家之间存在关系,所以在分析视图中您需要定义 2 个表。这里的问题不是关于改变模式(我已经想到了,使用空列),而是试图为我的模式找到一个 php 解决方案。干杯。 我正在尝试实现一个非常相似的功能,其中每个用户类型都有不同的仪表板。我想知道你把代码放在哪里来扩展 Auth 组件?还有,你是如何实现重定向的?在每个控制器中设置重定向变量?谢谢 您只需要创建一个新组件(在我的示例中称为 SiteAuthComponent),将其添加到 $components 数组中的 app_controller 中,然后不要使用 $this->Auth->function() 在任何地方使用 $this->SiteAuth->function()需要调用您网站中的 Auth 组件。祝你好运! CakePHP 2.x 解决方案:***.com/questions/11135066/… 【参考方案1】:

CakePHP 的AuthComponent 一次只支持针对单个“用户”模型的身份验证。通过设置Auth::userModel property 选择模型,但它只接受字符串而不接受模型数组。

您可以使用以下代码即时切换userModel,但这需要您提前知道要切换到哪个模型(例如,您的用户必须从下拉列表中选择他们的帐户类型):

public function beforeFilter() 
    if (isset($this->data['User']['model'])) 
        $this->Auth->userModel = $this->data['User']['model'];
    

您可以通过覆盖AuthComponent::identify() method 来扩展核心AuthComponent 以添加您想要的功能,这样它就会循环并尝试对每个模型进行身份验证:

App::import('Component', 'AuthComponent');
class AppAuthComponent extends AuthComponent 

    function identify($user = null, $conditions = null) 
        $models = array('User', 'Admin', 'Artist', 'TeamAdmin');
        foreach ($models as $model) 
            $this->userModel = $model; // switch model
            $result = parent::identify($user, $conditions); // let cake do it's thing
            if ($result) 
                return $result; // login success
            
        
        return null; // login failure
    

您必须将应用程序中出现的Auth 替换为AppAuth 才能使用扩展的AuthComponent,除非您使用this trick。

【讨论】:

感谢您的详细解答。我会尽快回复您并验证您的答案(如果不是,我会发布 cmets,但看起来很有希望)。干杯。 所以我选择了第二种解决方案,它与您的代码完美配合,我必须添加一行:$this->params["data"][$model] = $this->params["data"]["User"];。谢谢! 嗨,@Nicolas,你能给我一个修改过的代码,以便我可以正确引用它吗?在这里我发现你添加的行与我目前正在学习蛋糕完全一样,谢谢!跨度> @Nicolas 好的,你用最终解决方案编辑了问题。【参考方案2】:

虽然烦人,但我认为最好的解决方案可能是使用 Cake 内置的 ACL 支持(请参阅 http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html)。

如果您按照您所说的方式进行身份验证,则必须跟踪控制器代码中的权限,检查 userModel 是什么。如果您使用访问控制列表,则权限树将已经存在于数据库中,这将大大简化您的代码,并使其更加模块化。

这也意味着将您的数据模型重构为具有单个 users 表和 groups 表,而不是针对每种用户类型的实体类。

我自己刚刚经历了这个过程...... :(

【讨论】:

【参考方案3】:

这也是一种可能

public function beforeFilter() 
    parent::beforeFilter();
    $this->Auth->authenticate = array(
        AuthComponent::ALL => array('userModel' => 'AnotherModel'),
        'Form',
        'Basic'
    );

【讨论】:

这不允许第二个身份验证用户模型,它只是从默认的预期“用户”表中更改它。【参考方案4】:

这是 deizel 建议并由 Nicolas 修改的最终解决方案:

App::import('Component', 'Auth');
class SiteAuthComponent extends AuthComponent 

    function identify($user = null, $conditions = null) 
        $models = array('User', 'Admin', 'Artist');
        foreach ($models as $model) 
            $this->userModel = $model; // switch model
            $this->params["data"][$model] = $this->params["data"]["User"]; // switch model in params/data too
            $result = parent::identify($this->params["data"][$model], $conditions); // let cake do its thing
            if ($result) 
                return $result; // login success
            
        
        return null; // login failure
    

【讨论】:

以上是关于Cakephp Auth 与多个“用户”表的主要内容,如果未能解决你的问题,请参考以下文章

带有 CakePHP Auth 组件的唯一用户名和电子邮件

Cake PHP中同一个表的多个外键?

CakePHP:hasOne 相关表在更新时保存多个条目

CakePHP 3 下的摘要认证

Cakephp 和 Postgres,表关系

Cakephp 3每次请求都会更新会话