0
votes

Using CakePHP 2.2.3 I'm nearly finished with my project and now going back through to setup authorization.

I'm implementing ACL, truncated both the users and groups tables for a fresh start, ran the command to recreate the aco/aro/aros_acos tables and have followed the tutorial.

When I create a group, it creates a corresponding ARO entry but the lft, and rght fields are NULL. I commented out all of my other code in the users/groups models and controllers to try to narrow it down, but it doesn't seem to help.

I will post my code below, with comments and validations removed for the sake of space.

group model:

App::uses('AppModel', 'Model');

class Group extends AppModel {
public $actsAs = array('Acl' => array('type' => 'requester'));

public function parentNode() {
    return null;
}

public $hasMany = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'group_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);
}

User model:

App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
//setup ACL settings and function
public $actsAs = array('Acl' => array('type' => 'requester'));

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));
    }
} // end parentNode()

public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
    $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}

AppController:

App::uses('Controller', 'Controller');
class AppController extends Controller {

public $components = array(
    //'Security',
            'Acl',
    'Auth' => array(
        'authorize' => array(
                        'Actions' => array('actionPath' => 'controllers')
                            )/*,
        'authenticate' => array(
            'Form' => array(
                'scope' => array('User.activated' => 1 )
                )
            ) */
        ),
    'Session'
 ); 
public $helpers = array(
    'Html',
    'Text',
    'Session',
    'Form'
); 

 /* public function isAuthorized($user = null) {
    return true;
 } */

public function beforeFilter(){

$this->Auth->loginRedirect = array('controller' => 'products', 'action' => 'index' );
    $this->Auth->logoutRedirect = array('controller' => 'products', 'action' => 'index');
    $this->Auth->authError = 'You are not allowed to see that.';
 }

I even did an ACL implementation on a fresh install of cakephp 2.4.6, and everything works great. I have the projects side by side for comparison but can't find a difference in my ACL setup

Why aren't my lft and rght fields being set in my ARO table?

1

1 Answers

0
votes

Short Answer: Remove MVC files associated with ACL tables.

Less Short Answer: I setup ACL on a fresh install of cake 2.2.3, and everything worked great. Overwrote my code from my user and group models and controllers as well as AppController, and still no go.

I've seen a similar situation when I forget to add $actsAs = array('Tree'); to a model.

I realized I baked controllers/models/views for all ACL tables. DOH! (look for aroscontroller, acoscontroller, etc.)

I removed all the MVC files for these tables and it works great now.

This isn't a typical issue since normally one would add ACL schema after baking, but I started with a database I used on another project and forgot to remove the tables.

I really hope my stupidity helps someone else in this situation.