使用CakePHP检查登录时的用户角色?

使用CakePHP检查登录时的用户角色?,cakephp,cakephp-2.2,Cakephp,Cakephp 2.2,我想为我的站点创建一个管理面板,为此我创建了admin.ctp。在DB表中,名称user包含列role,其中role=admin/regular(用户) 只有一个登录表单,问题是,是否可以进行“检查”,如果user.role=admin,则重定向到admin/user/dashboard,如果user.role=regular则重定向到layout=default?my AppController.php包含: function beforeFilter(){ $this->Aut

我想为我的站点创建一个管理面板,为此我创建了
admin.ctp
。在DB表中,名称user包含列
role
,其中role=admin/regular(用户)

只有一个登录表单,问题是,是否可以进行“检查”,如果
user.role=admin
,则重定向到
admin/user/dashboard
,如果
user.role=regular
则重定向到
layout=default
?my AppController.php包含:

function beforeFilter(){
    $this->Auth->allow('index','view','login','home');
    $this->set('admin',$this->_isAdmin());

    $this->set('logged_in',$this->Auth->loggedIn());
    $this->set('current_user',$this->Auth->User());
    if ((isset($this->params['prefix']) && ($this->params['prefix'] == 'admin'))) {
        $this->layout = 'admin';
    }
和usersController.php

function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('*');
    if($this->action=='add' || $this->action=='edit'){
        $this->Auth->authenticate=$this->User;
    }
}

function login(){
    if(!($this->Auth->loggedIn())){
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                if($user['role'] === 'admin'){
                    $this->redirect($this->Auth->redirect('admin',array('controller' => 'user','action' => 'admin_dashboard')));
                }
                $this->redirect($this->Auth->redirect(array('controller' => 'posts','action' => 'index')));
            } else {
                $this->Session->setFlash('Your username/password combination was incorrect.',
                    'alert',array(
                        'plugin' => 'TwitterBootstrap',
                        'class' => 'alert-error'
                    ));
                $this->set('forget', 'Forgot Your Password');

            }
        }
    }else
    {
        $this->redirect($this->Auth->redirect(array('controller' => 'posts','action' => 'index')));
    }
}
使用CakePHP2.2.3。 提前谢谢

在AppController集合中:

$this->Auth->authorize = array('Controller');
然后执行以下操作

public function isAuthorized($user = null)
{
    if (isset($this->request->params['admin']))
    {
        if(!isAdmin)
        {
            return false;
        }
    }

    return true;
}
在AppController集合中:

$this->Auth->authorize = array('Controller');
然后执行以下操作

public function isAuthorized($user = null)
{
    if (isset($this->request->params['admin']))
    {
        if(!isAdmin)
        {
            return false;
        }
    }

    return true;
}
这就是我要做的(记住根据您的组模型相应地更改
字段('name')

这就是我要做的(记住根据您的组模型相应地更改
字段('name')


CakePHP实际上已经提供了一个非常有用的身份验证和授权框架,启用起来很简单

下面介绍如何根据存储在数据库中的角色进行授权。我还提供了一个示例
beforeFilter()
,如果用户是管理员,该示例将更改登录重定向操作:

AppController.php: UsersController.php:
您可以通过多种方式安排授权,因此请调整
if/else
语句以最适合您的需要。
isAuthorized()
方法相对简单,如果要允许访问,只需返回
true
,如果不允许,只需返回
false

CakePHP实际上已经提供了一个非常有用的身份验证和授权框架,启用起来非常简单

下面介绍如何根据存储在数据库中的角色进行授权。我还提供了一个示例
beforeFilter()
,如果用户是管理员,该示例将更改登录重定向操作:

AppController.php: UsersController.php:
您可以通过多种方式安排授权,因此请调整
if/else
语句以最适合您的需要。
isAuthorized()
方法相对简单,如果您想允许访问,只需返回
true
,如果不允许,只需返回
false

我实际上想发布一些代码,或许可以为我自己解释一下这个主题。 我已经为每个用户实现了一个简单的仪表板,它扩展了/Users/view——我的下一步是尝试获得对每一组数据的特定于用户的访问

应用控制器

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->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
        $this->Auth->allow('display');
    }
}
public function dashboard() {
    $id = $this->Auth->user('id');
    $this->set('user', $this->User->read(null, $id));

    $group_name = $this->User->Group->field('name', array('id' => $this->Auth->User('group_id')));
    $this->redirect = array('controller' => 'users', 'action' => 'dashboard');
}   
用户控制器

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->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
        $this->Auth->allow('display');
    }
}
public function dashboard() {
    $id = $this->Auth->user('id');
    $this->set('user', $this->User->read(null, $id));

    $group_name = $this->User->Group->field('name', array('id' => $this->Auth->User('group_id')));
    $this->redirect = array('controller' => 'users', 'action' => 'dashboard');
}   
dashboard.ctp

$this->extend('/Users/view');

实际上,我想发布一些代码,也许可以为我自己解释一下这个问题。 我已经为每个用户实现了一个简单的仪表板,它扩展了/Users/view——我的下一步是尝试获得对每一组数据的特定于用户的访问

应用控制器

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->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
        $this->Auth->allow('display');
    }
}
public function dashboard() {
    $id = $this->Auth->user('id');
    $this->set('user', $this->User->read(null, $id));

    $group_name = $this->User->Group->field('name', array('id' => $this->Auth->User('group_id')));
    $this->redirect = array('controller' => 'users', 'action' => 'dashboard');
}   
用户控制器

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->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
        $this->Auth->allow('display');
    }
}
public function dashboard() {
    $id = $this->Auth->user('id');
    $this->set('user', $this->User->read(null, $id));

    $group_name = $this->User->Group->field('name', array('id' => $this->Auth->User('group_id')));
    $this->redirect = array('controller' => 'users', 'action' => 'dashboard');
}   
dashboard.ctp

$this->extend('/Users/view');



谢谢您的回复。在您的第一个“如果”中有一个额外的开头括号“(”。我想做的是,当用户登录时,
code
if($user['role']='admin'))然后重定向到siteurl/admin/user/dashboard。但是我无法得到这个结果,现在任何普通用户都可以通过放置手动url来访问这个admin/user/dashboard。@yoggi,请让我知道我的UsersController有什么问题,是否有逻辑问题?比如login()if($user['role']='admin')){$this->redirect($this->Auth->redirect('admin',array('controller'=>'user','action'=>'admin_dashboard'));}这应该是重定向吗?@user1984675:请参阅我的编辑。您可以使用AppController的
beforeFilter()
若要动态更改
loginDirect
操作,请将我重定向到同一位置,请检查我分别尝试过的
routes.php
以下几行:
Router::connect('/admin/*',array('controller'=>'users','action'=>'admin_dashboard');
Router::connect('/admin/:controller/:action/*',array('admin'=>true,'prefix'=>'admin','controller'=>'user','action'=>'admin\u dashboard');
路由器::connect('/admin',array('controller'=>'users','action'=>'index','admin'=>true));
谢谢您的回复。还有一个额外的开头括号'('在您的第一个'if'。我想做的是,当用户登录时,
code
if($user['role']='admin'))然后重定向到siteurl/admin/user/dashboard。但是我无法得到这个结果,现在任何普通用户都可以通过放置手动url来访问这个admin/user/dashboard。@yoggi,请让我知道我的UsersController有什么问题,是否有逻辑问题?比如login()if($user['role']='admin')){$this->redirect($this->Auth->redirect('admin',array('controller'=>'user','action'=>'admin_dashboard'));}这应该是重定向吗?@user1984675:请参阅我的编辑。您可以使用AppController的
beforeFilter()
若要动态更改
loginDirect
操作,请将我重定向到同一位置,请检查我分别尝试过的
routes.php
以下几行:
Router::connect('/admin/*',array('controller'=>'users','action'=>'admin_dashboard');
Router::connect('/admin/:controller/:action/*',数组('admin'=>true,'prefix'=>admin','controller'=>user','action'=>admin\u dashboard');
路由器::连接('/admin',数组('controller'=>users','action'=>index','admin'=>true));