CakePHP认证组件 - 可能有两个单独的登录? [英] CakePHP Auth Component -- Possible to have two separate logins?

查看:132
本文介绍了CakePHP认证组件 - 可能有两个单独的登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CakePHP 2.0应用程序使用AuthComponent与 User 模型成功登录管理帖子和其他控制器和模型。我会称之为网站的超级管理员部分。

I have a CakePHP 2.0 app that uses the AuthComponent with User model to successfully login to manage posts and other controllers and models. I'll call that the "super admin" part of the site.

但是,我需要为广告客户创建一个完全独立的网站部分。我已创建广告客户和控制器 AdvertisersController 的模型。我还创建了广告客户表。我可以成功添加广告客户用户,但是当我尝试登录时,它会失败,并显示无效的用户名或密码错误。

However, I need to create an entirely separate part of the site for advertisers. I have created model Advertiser and controller AdvertisersController. I have also created table advertisers. I am able to successfully add an advertiser user, but when I attempt to login, it fails with the invalid username or password error.

我按照食谱中的建议 Authentication ,并尝试了我的 AdvertisersController beforeFilter

I followed suggestions in the Cookbook for Authentication and tried both of these options in the beforeFilter of my AdvertisersController:

$this->Auth->authenticate = array(
    AuthComponent::ALL => array(
        'userModel' => 'Advertiser', 
        'fields' => array('username' => 'username', 'password' => 'password'),
            'Form',
            'Basic'
    ));


$this->Auth->authenticate = array(
    'Form' => array('userModel' => 'Advertiser'),
    'Basic' => array('userModel' => 'Advertiser')
);

任何想法?

编辑:我将AdvertisersController更新为:

I updated my AdvertisersController to this:

public function beforeFilter() {

    parent::beforeFilter();

    $this->Auth->authenticate = array(
        AuthComponent::ALL => array('userModel' => 'Advertiser'),
        'Form',
        'Basic'
    );
    $this->Auth->allow('add', 'logout'); // Letting advertisers register themselves

} 

现在登录失败 - 没有失败的密码错误。我甚至试过一个假的用户名和密码。这是我的控制器中的登录方法:

Now the login is failing altogether -- no failed password error. I even tried a bogus username and password. Here is the login method in my controller:

public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect(array('controller' => 'advertisers', 'action' => 'admin'));
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }

$ c> else print_r($ this-> data); exit; 语句在第一个if关闭后获得一个空数组。甚至不能访问登录页面。

I added an else print_r($this->data); exit; statement after the close of the first if and got an empty array. Couldn't even get to the login page.

此外,表单的输出是这样的 - POST是方法。

Also, the output for the form is this -- POST is the method.

<form action="/advertisers/login?url=advertisers%2Flogin" id="AdvertiserLoginForm" method="post" accept-charset="utf-8">
    <div style="display:none;"><input type="hidden" name="_method" value="POST"/></div>    
    <fieldset>
        <legend>Please enter your username and password</legend>
        <div class="input text required">
            <label for="AdvertiserUsername">Username</label>
            <input name="data[Advertiser][username]" maxlength="50" type="text" id="AdvertiserUsername"/>
        </div>
        <div class="input password required">
            <label for="AdvertiserPassword">Password</label>
            <input name="data[Advertiser][password]" type="password" id="AdvertiserPassword"/>
        </div>    
    </fieldset>
    <div class="submit">
        <input  type="submit" value="Login"/>
    </div>
</form>


推荐答案

我更多地转发了这个问题在这里。 Joshua是一个很大的帮助获得管理员前缀排序的这种情况。如果它似乎对你的应用程序太多 - 看到他的评论在这里。它看起来比它更复杂,它有助于保持用户控件更加定义。

I reposted this question with more information here. Joshua was a big help getting admin prefixing sorted out for this scenario. If it seems like "too much" for your app -- see his comments here. It looks more complicated than it is, and it helps keep user controls much more defined.

这篇关于CakePHP认证组件 - 可能有两个单独的登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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