如何使用多个Auth组件? [英] How to use multiple Auth components?

查看:98
本文介绍了如何使用多个Auth组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用用户模型将Auth组件配置为管理页面".但是现在,我还想为客户端创建/配置Auth.我尝试重写" inialize()

I configure a Auth component to "Admin page", using the users model. But now, I also want create/configure a Auth to the clients. I try "rewrite" the inialize()

//This is in my ClientsController.php
public function initialize()
{
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'userModel' => 'clients',
                'fields' => ['username' => 'client_email', 'password' => 'client_password']
            ]
        ],
        'loginRedirect' => [
            'controller' => 'Clients',
            'action' => 'index'
        ],
        'logoutRedirect' => [
            'controller' => 'Clients',
            'action' => 'login'
        ],
    ]);
}

这样,我会收到此日志(如果使用parent :: initalize()会收到相同的日志)

With this, I receive this log(if uses parent::initalize() receive the same)

[RuntimeException] The "Auth" alias has already been loaded with the following config: array (...

我不想手动创建身份验证".如何使用一种认证中的更多认证?

I not want create a "Auth" manualy. How to use more of one Auth?

谢谢....

推荐答案

重新配置

您不一定需要使用多个auth组件实例,您可以使用config()组件方法在扩展控制器中简单地对其进行重新配置,类似于:

Reconfigure

You don't necessarily need to use multiple auth component instances, you can simply reconfigure it in the extended controller, using the components config() method, something along the lines of:

public function initialize()
{
    parent::initialize();

    // ...

    $this->Auth->config(
        [
            'authenticate' => [
                'Form' => [
                    'userModel' => 'clients',
                    'fields' => [
                        'username' => 'client_email',
                        'password' => 'client_password'
                    ]
                ]
            ],
            'loginRedirect' => [
                'controller' => 'Clients',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Clients',
                'action' => 'login'
            ],
            'storage' => [
                'className' => 'Session',
                'key' => 'Auth.Client'
            ]
        ],
        null,
        false
    );
}

请注意使用storage选项,您应在此处定义其他密钥(默认为Auth.User),否则经过身份验证的客户端可能能够访问管理区域,反之亦然反之亦然,因为用户数据将存储在同一会话密钥中!

Note the use of the storage option, you should define a different key here (the default is Auth.User), otherwise an authenticated client might be able to access the admin area and vice versa, as the user data would get stored in the same session key!

如果需要,您可以使用多个auth组件,因此必须使用别名,以便这些组件不会尝试相互覆盖:

You could use multiple auth components if required, to do so you'd have to use aliasing, so that the components don't try to override each other:

$this->loadComponent('ClientAuth', [
    'className' => 'Auth',
    // ....
]);

在这种情况下也不要忘记使用其他会话密钥!

您将相应地以$this->ClientAuth的身份访问该组件实例,并且可能必须允许通过$this->Auth来访问login()方法,即.在ClientsController::initialize()beforeFilter()中执行:

You'd access that component instance as $this->ClientAuth accordingly, and you may have to allow access to the login() method via $this->Auth, ie. in ClientsController::initialize() or beforeFilter() do:

$this->Auth->allow('login');

可能还有其他副作用,所以要小心.

There might be further side-effects, so be careful.

这篇关于如何使用多个Auth组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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