Cakephp Auth具有多个"用户"表

Nic*_*las 16 authentication cakephp multiple-tables cakephp-1.3

我想知道如何只处理一个身份验证过程和多个表中的"用户".我有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
    }
}
Run Code Online (Sandbox Code Playgroud)

dei*_*zel 20

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

您可以userModel使用以下代码动态切换,但这需要您事先知道要切换到哪个型号(例如,您的用户必须从下拉列表中选择其帐户类型):

public function beforeFilter() {
    if (isset($this->data['User']['model'])) {
        $this->Auth->userModel = $this->data['User']['model'];
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以AuthComponent通过覆盖AuthComponent::identify()方法来扩展核心以添加所需的功能,以便它循环并尝试使用每个模型进行身份验证:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

除非使用此技巧,否则必须使用扩展的AuthComponent 替换Auth应用程序中出现的内容.AppAuth