CakePHP插件:ACL或Auth没有加载模型关联?

时间:2013-07-08 00:07:19

标签: php cakephp cakephp-2.0

AppController(插件外部)

class AppController extends Controller {
    public $components = array(
        'RequestHandler',
        'Acl',
        'Auth',
        'Session'
    );
    public $helpers = array('Html', 'Form', 'Session');

    public function beforeFilter() {

        $this->Auth->authorize = array(
            'Actions' => array(
                'actionPath' => 'controllers',
                'userModel' => 'UserManager.User'
            )
        );

        $this->Auth->authenticate = array(
                'Form' => array(
                    'userModel' => 'UserManager.User'
                ),
        );

        //Configure AuthComponent
        $this->Auth->loginAction = array('plugin' => 'user_manager', 'controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('plugin' => 'user_manager', 'controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('plugin' => 'user_manager', 'controller' => 'users', 'action' => 'login');
    }
}

app / Plugins / UserManager / Model / User.php中的UserModel     

class User extends UserManagerAppModel {

    public $name = "User";

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Group' => array(
            'className' => 'UserManager.Group',
            'foreignKey' => 'group_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

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

/**
 * actsAs
 * @var array
 */
    public $actsAs = array('Acl' => array('type' => 'requester'));

/**
 * parentNode
 * @return mixed
 */
    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));
        }
    }

/**
 * bindNode
 * http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html#group-only-acl
 * @param  array $user [description]
 * @return array
 */
    public function bindNode($user) {
        return array('model' => 'UserManager.Group', 'foreign_key' => $user['UserManager.User']['Group']['id']);
    }
}

插件中的Group模型

<?php
App::uses('UserManagerAppModel', 'UserManager.Model');
/**
 * Group Model
 *
 * @property User $User
 */
class Group extends UserManagerAppModel {

public $name = "Group";

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'User' => array(
            'className' => 'UserManager.User',
            'foreignKey' => 'group_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

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

    public function parentNode() {
        return null;
    }
}

当我debug(AuthComponent::user());时,我得到了:

array(
    'id' => '1',
    'username' => 'superadmin',
    'group_id' => '1',
    'created' => '2013-07-07 22:01:39',
    'modified' => '2013-07-07 22:01:39',
    'Group' => array(
        'id' => '1',
        'name' => 'SuperAdmin',
        'created' => '2013-07-07 22:01:24',
        'modified' => '2013-07-07 22:01:24'
    )
)

然后我收到此错误:

AclNode::node() - Couldn't find Aro node identified by "Array ( [Aro0.model] => UserManager.Group [Aro0.foreign_key] => 1 ) "

1 个答案:

答案 0 :(得分:0)

app / Plugin / UserManager / UsersController.php

class UsersController extends UserManagerAppController {

    public function beforeFilter() {
        parent::beforeFilter();

        $this->loadModel('UserManager.User'); //ADDED THIS!
    }

app / Plugin / UserManager / Model / User.php

中更新了bindNode($ user)
public function bindNode($user) {
    return array('model' => 'Group', 'foreign_key' => $user['User']['Group']['id']);
}