CakePHP 2.x ACL问题使其工作


CakePHP 2.x ACL Issues getting it working

我一直在努力让CakePHP ACL与我的新应用程序一起工作,这给我带来了巨大的痛苦。由于某些原因,ACL似乎不起作用,但教程是垃圾,并没有很好地解释每个组件。例如ACO如何链接到控制器/功能/查看

我已经让ACL正常工作,直到让页面知道用户是否被允许查看它,他们可以/不能看到的菜单项也有同样的问题。

我注意到,如果我将此代码添加到我的页面,数组将显示组为空:

$user = $this->Auth->user(); 
pr($user);

返回的数组:

Array
(
    [id] => 80
    [first_name] => Bob
    [last_name] => Test
    [email] => email@emial.com
    [username] => TestAdmin
    [tokenhash] => cleared
    [is_active] => 1
    [created] => 2014-10-03 16:32:45
    [modified] => 2014-10-03 16:32:45
    [token_expires_at] => 
    [group_id] => 3
    [Group] => Array
        (
            [id] => 
            [name] => 
            [enabled] => 
            [created] => 
            [modified] => 
        )
)

该网站基本上是一个门户网站,访问者只能登录/注册。用户组都可以访问仪表板,但它最终会进入一个连续的循环,除非我允许每个人都访问仪表板(我想是因为该组没有被识别)

任何帮助都将不胜感激,我意识到你可能需要我发布我使用的代码,所以请让我知道你需要什么。

提前感谢

编辑:

我已经更新了我的AppController,如下所示,它已经开始在数组中显示它应该显示的组!!!奇怪的是,感谢你朝着正确的方向推动。

AppController.php

<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
    public function beforeRender() {
        if((($this->params['controller']==='Users') || ($this->params['controller']==='users'))&&(($this->params['action']=='login') || ($this->params['action']=='register') || ($this->params['action']=='success') || ($this->params['action']=='forgot_password') || ($this->params['action']=='reset_password')) ){
            $this->theme = 'DataHouseLogin';
        }else{
            $this->theme = 'DataHouse';
        }
        parent::beforeRender();
    }
    public $components = array(
        'Acl',
        'RequestHandler',
        'DebugKit.Toolbar' => array('panels' => array('history' => false)),
        'Session',
        'Auth' => array(
            'authorize' => array(
                'Actions' => array(
                    'actionPath' => 'controllers'
                    )
            ), 
            'loginAction' => array(
                'controller' => 'Users', 
                'action' => 'login'
            ),
            'loginRedirect' => array(
                'controller' => 'Dashboard',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'Users',
                'action' => 'login'
            ),
            'authError' => 'Did you really think you are allowed to see that?',
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish'
                )
            )
        )
    );
    public function beforeFilter() {
        //$this->Auth->allowedActions = array('display','index','register');
        $this->set('user', $this->Auth->user());    
        $this->set('acl', $this->Acl);
        $this->Auth->authorize = array(
            'Controller',
            'Actions' => array('actionPath' => 'controllers')
        ); 

        parent::beforeFilter();
    }
    public function isAuthorized($user) {
        // Default deny
        return false;
    } 
}

我认为您应该尝试正确配置Auth组件。尝试将此代码放入您的AppController:

class AppController extends Controller {
 public $components = array('RequestHandler', 'Session',
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        )
    ),
  );  
 public function beforeFilter() {
   $this->Auth->authorize = array(
        'Controller',
        'Actions' => array('actionPath' => 'controllers')
  );    
   $this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'name', 'password' => 'password')));
   $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false, 'plugin' => false);
   $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false, 'plugin' => false);
 }
 public function isAuthorized($user) {
    // Default deny
    return false;
 } 
}

编辑:在UserModel和GroupModel中添加充当属性:

public $actsAs = array('Acl' => array('type' => 'requester'));

在UserModel设置中parentNode函数:

public function parentNode() {
  if (!$this->id && empty($this->data)) {
      return null;
  }
  if (isset($this->data['User']['group_id'])) {
      $groupId = $this->data['User']['group_id'];
  } else {
      $groupId = $this->field('group_id');
  }
  if (!$groupId) {
      return null;
  } else {
      return array('Group' => array('id' => $groupId));
  }
}