0
votes

Using cake acl I am trying to set permissions for logged in users. I have this in the login function:

if ($user = $this->Auth->user()) {
    CakeLog::write('debug', 'this->Auth->user');
    //debug($user['User']);
    $userGroup = $this->User->Group->findById($user['User']['group_id']);
    $aro = $this->Acl->Aro->find('first', array(
        'conditions' => array(
            'Aro.model' => 'Group',
            'Aro.foreign_key' => $this->_user['User']['group_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) {
                $this->Session->write(
                    'Auth.Permissions.' . $permission['Aco']['alias'], true
                );
                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.' . $permission['Aco']['alias']
                        . '.' . $parentAco['Aco']['alias'], true
                    );
                }
            }
        }
    }
}

I am getting this error:

Undefined property: UsersController::$_user [APP\controllers\users_controller.php, line 82]

I can change out '$this->_user['User']['group_id']' with '$this->_user['User']['group_id']', would that be correct. If not what should I set public $_user = ?

Thanks

2
What's on line 82? All that error means is that you're missing a $_user variable on the class. - jeremyharris
this is on line 82 'Aro.foreign_key' => $this->_user['User']['group_id'],' - rstewart
I can set the _user variable, but what should _user be set to? Is it the same as $user['User']['group_id']? - rstewart
I have no idea what it should be, that's up to the programmer that put it in there :) My guess is it holds the currently logged in user's data. If it's empty, then you need to populate it when the user logs in. This is only a guess, it depends on your code. - jeremyharris

2 Answers

0
votes

I replaced _user with $user and it seems to be working.

-1
votes

if you need group_id of authenticated user you can call

AuthComponent::user('group_id');