CakePHP的验证与多个"用户"表 [英] Cakephp Auth with multiple "Users" tables

查看:129
本文介绍了CakePHP的验证与多个"用户"表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何处理只有一个鉴别过程,用户在多个表中。我有4个用户表:用户,管理员,艺术家,teamadmins这都有特定的领域,但我想所有这些用户能够仅通过一个表格来连接在主页上,之后,其具体的仪表板被重定向。

我觉得重定向不应该是一个问题,而增加了一些路线应该工作,但是我真的不知道去哪里找/开始AKE这一切成为可能。

干杯,

尼古拉斯。

修改:这里的最终解决方案(感谢deizel)

 应用::进口('组件','验证');
类SiteAuthComponent扩展AuthComponent {    功能识别($ USER = NULL,$条件= NULL){
        $型号=阵列('用户','管理','艺术家');
        的foreach($模型作为$模型){
            $这个 - >的usermodel = $模式; //切换模式
            $这个 - > PARAMS [数据] [$模型] = $这个 - > PARAMS [数据] [用户]; //在PARAMS /数据交换模型太
            $结果=父::识别($这个 - > PARAMS [数据] [$模式],$条件); //让蛋糕做它的事
            如果($结果){
                返回$结果; //登录成功
            }
        }
        返回null; //登录失败
    }
}


解决方案

CakePHP的 AuthComponent 只支持反对一次一个用户模式验证。该模型是通过设置 验证::选择的usermodel 属性,但它只接受一个字符串,而不是模型数组。

您可以用下面的code动态切换的usermodel ,不过这需要你提前知道哪些模式切换到如(你的用户不得不选择从下拉自己的帐户类型):

 公共职能beforeFilter(){
    如果(使用isset($这个 - >数据['用户'] ['模式'])){
        $这个 - > Auth->的usermodel = $这个 - >数据['用户'] ['模式'];
    }
}

您可能可以扩展核心 AuthComponent 通过覆盖的 AuthComponent ::识别()方法所以它遍历并与每个模型尝试验证:

 应用::进口('组件','AuthComponent');
类AppAuthComponent扩展AuthComponent {    功能识别($ USER = NULL,$条件= NULL){
        $型号=阵列(用户,管理,艺术家,TeamAdmin');
        的foreach($模型作为$模型){
            $这个 - >的usermodel = $模式; //切换模式
            $结果=父::识别($用户,$条件); //让蛋糕做的事情
            如果($结果){
                返回$结果; //登录成功
            }
        }
        返回null; //登录失败
    }
}

您将与 AppAuth未来代替验证在应用程序中出现的使用您的扩展AuthComponent,除非你使用这一招

I would like to know how to deal with only ONE authentification process and "users" in multiple tables. I have 4 Users table: users, admins, artists, teamadmins which all have specific fields, but I would like all of these users to be able to connect via only one form on the homepage, and being redirected after that to their specific dashboards.

I think the redirections shouldn't be a problem, and some routes added should work, but I really don't know where to look/start to ake this all possible.

Cheers,
Nicolas.

EDIT: here's the final solution (thanks to 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
    }
}

解决方案

CakePHP's AuthComponent only supports authentication against a single "User" model at a time. The model is chosen by setting the Auth::userModel property, but it only accepts a string and not an array of models.

You can switch the userModel on the fly with the following code, but this requires you to know in advance which model to switch to (eg. your users have to choose their account type from a dropdown):

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

You can likely extend the core AuthComponent to add the functionality you want by overwriting the AuthComponent::identify() method so it loops over and attempts authentication with each model:

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
    }
}

You will have to replace occurrences of Auth in your application with AppAuth to use your extended AuthComponent, unless you use this trick.

这篇关于CakePHP的验证与多个"用户"表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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