简体   繁体   English

CakePHP 2.x ACL使其无法正常工作

[英]CakePHP 2.x ACL Issues getting it working

I have been trying to get the CakePHP ACL's working with my new application, it is causing me a world of pain. 我一直在尝试使CakePHP ACL与我的新应用程序一起使用,这使我感到非常痛苦。 For some reason the ACL doesnt seem to be working however the tutorials are rubbish and don't explain each component very well. 由于某些原因,ACL似乎无法正常运行,但是这些教程很垃圾,并且不能很好地解释每个组件。 eg how a ACO links to a controller / function / views 例如,ACO如何链接到控制器/功能/视图

I have got the ACL working correctly up until getting the pages to know if the user is allowed to view it, also same issue with menu items that they can / cant see. 我已经使ACL正常工作,直到让页面知道用户是否被允许查看为止,这也是他们无法看到的菜单项的同样问题。

I have noticed that if i add this code to my page the array shows the group as blank: 我注意到,如果我将此代码添加到我的页面,则数组会将组显示为空白:

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

The array returned: 数组返回:

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] => 
        )

)

The site is basically a portal, visitors should only have access to login / register. 该站点基本上是门户,访问者只能访问登录/注册。 and the users groups all have access to the dashboard however it ends up in a continuous loop unless i allow everyone access to the dashboard (due to the group not being recognised i guess) 并且所有用户组都可以访问仪表板,但是除非最终我不允许所有人访问仪表板,否则它会以连续循环的形式结束(我猜是由于该组未被识别)

any help would be appreciated, I realise that you may need me to post the code im using so please let me know what you would require. 任何帮助将不胜感激,我意识到您可能需要我发布使用的代码,所以请告诉我您需要什么。

Thanks in advance 提前致谢

EDIT: 编辑:

I have updated my AppController as below and it has started showing the group in the array as it should!!! 我已经更新了我的AppController,如下所示,它已经开始按需要在数组中显示组! weird thanks for the push in the right direction. 奇怪的感谢您朝着正确的方向前进。

AppController.php 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;
    } 
}

I think you should try to configure Auth component correctly. 我认为您应该尝试正确配置Auth组件。 Try to put this code in your AppController: 尝试将以下代码放入您的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;
 } 
}

EDIT: in UserModel and GroupModel add act as property: 编辑:在UserModel和GroupModel中添加充当属性:

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

in UserModel setup parentNode function: 在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));
  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM