具有多个“用户"的 Cakephp 身份验证桌子 [英] Cakephp Auth with multiple "Users" tables

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

问题描述

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

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.

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

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 的 AuthComponent 一次仅支持针对单个用户"模型的身份验证.通过设置 Auth::userModel 属性,但它只接受一个字符串而不是一个模型数组.

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.

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

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'];
    }
}

您可以通过覆盖 AuthComponent::identify() 方法 因此它循环并尝试对每个模型进行身份验证:

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

您必须将应用程序中出现的 Auth 替换为 AppAuth 才能使用扩展的 AuthComponent,除非您使用 这个技巧.

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天全站免登陆