Following the post (http://goo.gl/PLeNJR) for ACL: Checking Permissions in Views like some others I faced a “undefined index User…” and some other PHP warnings when trying to implement this example. After doing some testing following the working code I came up with. I think it may help some one there.
Just want to mention that I am using it CakePHP 2.5.6
[php]if ($user = $this->Auth->user()) {
$userGroup = $this->User->Role->findById($user[‘role_id’]);
$aro = $this->Acl->Aro->find(‘first’, array(
‘conditions’ => array(
‘Aro.model’ => ‘Role’,
‘Aro.foreign_key’ => $user[‘role_id’],
),
));
$acos = $this->Acl->Aco->children();
foreach($acos as $aco){
$permission = $this->Acl->Aro->Permission->find(‘first’, array(
‘conditions’ => array(
‘Permission.aro_id’ => $aro[‘Aro’][‘id’],
‘Permission.aco_id’ => $aco[‘Aco’][‘id’],
),
));
if(isset($permission[‘Permission’][‘id’])){
if ($permission[‘Permission’][‘_create’] == 1 ||
$permission[‘Permission’][‘_read’] == 1 ||
$permission[‘Permission’][‘_update’] == 1 ||
$permission[‘Permission’][‘_delete’] == 1) {
if(!empty($permission[‘Aco’][‘parent_id’])) {
$parentAco = $this->Acl->Aco->find(‘first’, array(
‘conditions’ => array(
‘id’ => $permission[‘Aco’][‘parent_id’]
)
));
$this->Session->write(
‘Auth.Permissions.’.$parentAco[‘Aco’][‘alias’]
.’.’.$permission[‘Aco’][‘alias’],
true
);
} else {
$this->Session->write(
‘Auth.Permissions.’.$permission[‘Aco’][‘alias’],
true
);
}
}
}
}
}[/php]