对于任何来这里的人来说,这就是我的设置方式。
首先,我设置了一个基本的基于角色的 ACL
然后我拒绝reports/all
普通用户访问
$config['rules']['deny'][reports/all'] = 'Role/default' ;
然后在我想保护的模型中添加了这个:
public function beforeFind($queryData){
//TODO:make this cleaner
App::uses('ComponentCollection', 'Controller');
App::uses('AclComponent', 'Controller/Component');
$collection = new ComponentCollection();
$this->Acl = new AclComponent($collection);
if(!$this->Acl->check(array('User'=>AuthComponent::user()),'/reports/all/')){ // must be a user (not admin)
$this->bindModel(array('hasOne' => array('ReportsUser')));
$queryData['conditions']['ReportsUser.user_id'] = AuthComponent::user('id');
$queryData['recursive'] = 2;
}
return $queryData;
}
在 ACL 不允许访问的情况下,reports/all
我们向任何查找查询添加条件,以便它仅显示具有正确 user_id 的报告。