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 ) "