cakephp auth-> loginRedirect for admin [英] cakephp auth->loginRedirect for admin

查看:93
本文介绍了cakephp auth-> loginRedirect for admin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚接触cakePHP,并且已经阅读了蛋糕网站上的所有教程。我正在建立一个小的示例应用程序使用蛋糕2.1,并有一个问题。基本上,我希望管理员用户在登录时重定向到正常用户被重定向到的不同的页面。我确定有一个简单的方法来做到这一点,但我很努力!



我已启用管理路由,并使用auth组件(和ACL组件不影响我的问题)。我有2个登录一个为admin - admin_login()和正常用户的登录 - login() - 正常的非管理员登录工作正常。



在我的AppController.php我有这个:

 类AppController extends Controller {

public $ components = array(
'Acl',
'Auth'=> array(
'authorize'=> array(
'Actions'=> array('actionPath' )

),
'Session'
);
public $ helpers = array('Html','Form','Session');

public function beforeFilter(){
//配置AuthComponent
$ this-> Auth-> allow('display'); //允许访问应用程序的主页
$ this-> Auth-> loginAction = array('controller'=>'users','action'=>'login');
$ this-> Auth-> logoutRedirect = array('controller'=>'users','action'=>'login');
$ this-> Auth-> loginRedirect = array('controller'=>'posts','action'=>'add');
}

}



所以你可以看到默认是用户被重定向在登录到posts控制器add()函数,这工作正常。但是,如何为我的admin_login()函数设置不同的登录重定向?



我已经读了一些帖子,但大多数涉及到旧版本的蛋糕而不是蛋糕2。



如果它有帮助,这是我的admin_login函数在UsersController.php

  public function beforeFilter(){
parent :: beforeFilter();
$ this-> Auth-> allow('logout');
}

public function admin_login(){
if($ this-> request-> is('post')){
if - > Auth-> login()){
if($ this-> Session-> read('Auth.User')){
$ this-& setFlash('You are logged in!');
/ / $ this-> redirect($ this-> Auth-> redirect());

//这不工作! Grrrrrr
// $ this-> redirect(array('controller'=>'pages','action'=>'admin_index'));

//这几乎工作了...
if($ this-> Auth-> user())$ this-> redirect(array('controller'=> 'pages','action'=>'admin_index'));
}
}
else {
$ this-> Session-> setFlash('您的用户名或密码不正确。
}
}

任何人都能指向正确的方向?如果你在你的数据库中有角色 - >admin,user,放在AppController - beforeFilter()中:

>

  if($ this-> Auth-> user('role')=='admin'){
$ this-> Auth-> loginRedirect = array('controller'=>'controller1','action'=>'action1');
} else {
$ this-> Auth-> loginRedirect = array('controller'=>'controller2','action'=>'action2');
}



如果你想知道你的$ this-> Auth-> user()put:

  debug($ this-> Auth-> user 


I'm fairly new to cakePHP and have read all the tutorials on the cake site. I'm building a small sample app using cake 2.1 and have hit a problem. Basically, I want admin users redirected to a different page on login than what a normal user gets redirected to. I'm sure there's an easy way to do this but I'm struggling!

I have Admin routing enabled and am using the auth component (and the ACL component though that's not affecting my issue). I have 2 logins one for admin - admin_login() and a login for normal users - login() - the normal non admin login works fine.

In my AppController.php I have this:

class AppController extends Controller {

public $components = array(
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        )
    ),
    'Session'
);
public $helpers = array('Html', 'Form', 'Session');

public function beforeFilter() {
    //Configure AuthComponent
    $this->Auth->allow('display'); // Allows access to the homepage of the app
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
}

}

So as you can see the default is for users to be redirected on login to the posts controllers add() function, which works fine. However, how can I set a different login redirect for my admin_login() function?

I've read a number of posts on here but most relate to older versions of cake and not cake 2.

If it helps, this is my admin_login function in the UsersController.php

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('logout');
}

public function admin_login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            if ($this->Session->read('Auth.User')) {
                $this->Session->setFlash('You are logged in!');
                //$this->redirect($this->Auth->redirect());

                // This doesnt work! Grrrrrr
                //$this->redirect(array('controller'=>'pages', 'action'=>'admin_index'));

                // This nearly works...
                if($this->Auth->user())$this->redirect(array('controller' => 'pages', 'action' => 'admin_index'));
            }            
        }
        else {
            $this->Session->setFlash('Your username or password was incorrect.');
        }
    }

Could anyone point me in the right direction?

解决方案

If you have role -> "admin, user" in your database, put in your AppController - beforeFilter():

if($this->Auth->user('role') == 'admin'){
     $this->Auth->loginRedirect = array('controller' => 'controller1', 'action' => 'action1');
}else{
     $this->Auth->loginRedirect = array('controller' => 'controller2', 'action' => 'action2');
}

If you want to know what is in your $this->Auth->user() just put:

debug($this->Auth->user());

这篇关于cakephp auth-> loginRedirect for admin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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