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

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

问题描述

我对 cakePHP 还很陌生,并且已经阅读了 cake 站点上的所有教程.我正在使用 cake 2.1 构建一个小型示例应用程序,但遇到了问题.基本上,我希望管理员用户在登录时重定向到与普通用户重定向到的页面不同的页面.我相信有一种简单的方法可以做到这一点,但我正在努力!

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!

我启用了 Admin 路由并且正在使用 auth 组件(和 ACL 组件,尽管这不会影响我的问题).我有 2 个登录,一个用于管理员 - admin_login() 和一个用于普通用户的登录 - login() - 正常的非管理员登录工作正常.

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.

在我的 AppController.php 中有这个:

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

}

如您所见,默认设置是在登录时将用户重定向到帖子控制器 add() 函数,该函数工作正常.但是,如何为我的 admin_login() 函数设置不同的登录重定向?

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?

我在这里阅读了许多帖子,但大多数都与旧版 cake 相关,而不是 cake 2.

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

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

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?

推荐答案

如果你的数据库中有角色 -> "admin, user",放入你的 AppController - beforeFilter():

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

如果您想知道 $this->Auth->user() 中的内容,只需输入:

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