2

我想为我的网站创建一个管理面板,为此我创建了admin.ctp. 在数据库表名用户中包含列role,其中角色=管理员/常规(用户)。

只有一个登录表单,问题是,是否可以“检查” if user.role=adminthen 重定向到admin/user/dashboardand if user.role=regularthen layout=default?我的 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')));
    }
}

使用 cakephp 2.2.3。提前致谢!

4

5 回答 5

2

这就是我的做法(请记住field('name')根据您的组模型进行相应更改)。

if ($this->Auth->login())
{
    this->User->Group->id = $this->Auth->user('group_id');
    switch ($this->User->Group->field('name'))
    {
        case 'admin':
            $this->redirect($this->Auth->redirect('admin',array('controller' => 'user','action' => 'admin_dashboard')));
            break;
        case 'regular':
            $this->redirect($this->Auth->redirect(array('controller' => 'posts','action' => 'index')));
            break;
        default:
            //...
            break;
    }
}
于 2013-01-18T22:58:22.630 回答
1

我实际上想发布一些代码来为自己阐明这个主题。我已经为每个用户实现了一个简单的仪表板,扩展了 /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');
}   

仪表板.ctp

$this->extend('/Users/view');
于 2013-01-23T21:37:45.123 回答
1

在 AppController 集中:

$this->Auth->authorize = array('Controller');

然后执行以下操作

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

    return true;
}
于 2013-01-18T22:10:46.923 回答
1

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

下面是如何根据存储在数据库中的角色进行授权,来自CakePHP 手册beforeFilter()如果用户是管理员,我还包括一个更改登录重定向操作的示例:

应用控制器.php:

public $components = array(
  'Auth' => array(
    'authorize' => array('Controller'),
    'loginRedirect' => array('/'),
));

public function beforeFilter() {
  // This allows us to use $user in all controllers.
  $this->set('user', $this->Auth->user());

  // If the user is an admin, override the loginRedirect
  if ('admin' === $this->Auth->user('role')) {
    $this->Auth->loginRedirect = array(
      'controller' => 'users',
      'action' => 'admin_dashboard',
    ));
  }
}

用户控制器.php:

public function isAuthorized($user) {
  // Ensure the user has a valid role. If not, deny access to all actions:
  if ((!isset($user['role'])) || ('' === $user['role'])) return false;

  // If we're trying to access the admin view, verify permission:
  if ('admin_dashboard' === $this->action)
  {
    if ('admin' === $user['role']) return true;  // User is admin, allow
    return false;                                // User isn't admin, deny
  }

  return true;
}

您可以通过多种方式安排授权,因此请调整if/else语句以最适合您的需要。方法比较简单,允许访问或者不允许访问isAuthorized()只需要返回即可。truefalse

于 2013-01-18T23:19:24.827 回答
-1
<?php 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');
}   
?>
于 2014-01-07T09:46:55.580 回答