2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ACL behavior class.
|
|
|
|
*
|
|
|
|
* Enables objects to easily tie into an ACL system
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-26 22:15:15 +00:00
|
|
|
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Behavior
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP v 1.2.0.4487
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 13:45:06 +00:00
|
|
|
App::uses('AclNode', 'Model');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* ACL behavior
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Behavior
|
2010-04-05 03:31:50 +00:00
|
|
|
* @link http://book.cakephp.org/view/1320/ACL
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class AclBehavior extends ModelBehavior {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Maps ACL type options to ACL models
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-03-19 14:14:10 +00:00
|
|
|
private $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco', 'both' => array('Aro', 'Aco'));
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets up the configuation for the model, and loads ACL models if they haven't been already
|
|
|
|
*
|
|
|
|
* @param mixed $config
|
2008-07-31 03:26:15 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function setup($model, $config = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_string($config)) {
|
|
|
|
$config = array('type' => $config);
|
|
|
|
}
|
2011-02-27 15:18:32 +00:00
|
|
|
$this->settings[$model->name] = array_merge(array('type' => 'controlled'), (array)$config);
|
2010-07-30 02:04:22 +00:00
|
|
|
$this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
|
2008-07-31 03:26:15 +00:00
|
|
|
|
2011-02-27 15:18:32 +00:00
|
|
|
$types = $this->__typeMaps[$this->settings[$model->name]['type']];
|
2011-05-28 13:45:06 +00:00
|
|
|
|
2011-02-27 15:08:52 +00:00
|
|
|
if (!is_array($types)) {
|
|
|
|
$types = array($types);
|
|
|
|
}
|
2011-03-19 14:14:10 +00:00
|
|
|
foreach ($types as $type) {
|
|
|
|
$model->{$type} = ClassRegistry::init($type);
|
2010-01-13 17:27:41 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!method_exists($model, 'parentNode')) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Callback parentNode() not defined in %s', $model->alias), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the Aro/Aco node for this model
|
|
|
|
*
|
|
|
|
* @param mixed $ref
|
2011-02-27 15:08:52 +00:00
|
|
|
* @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
2010-04-05 03:31:50 +00:00
|
|
|
* @link http://book.cakephp.org/view/1322/node
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-03-19 14:14:10 +00:00
|
|
|
public function node($model, $ref = null, $type = null) {
|
2011-02-27 15:08:52 +00:00
|
|
|
if (empty($type)) {
|
2011-02-27 15:18:32 +00:00
|
|
|
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
|
2011-02-27 15:08:52 +00:00
|
|
|
if (is_array($type)) {
|
|
|
|
trigger_error(__('AclBehavior is setup with more then one type, please specify type parameter for node()', true), E_USER_WARNING);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($ref)) {
|
2008-07-31 03:26:15 +00:00
|
|
|
$ref = array('model' => $model->name, 'foreign_key' => $model->id);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $model->{$type}->node($ref);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a new ARO/ACO node bound to this record
|
|
|
|
*
|
|
|
|
* @param boolean $created True if this is a new record
|
2008-07-31 03:26:15 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function afterSave($model, $created) {
|
2011-02-27 15:18:32 +00:00
|
|
|
$types = $this->__typeMaps[$this->settings[$model->name]['type']];
|
2011-02-27 15:08:52 +00:00
|
|
|
if (!is_array($types)) {
|
|
|
|
$types = array($types);
|
2009-09-19 05:17:20 +00:00
|
|
|
}
|
2011-02-27 15:08:52 +00:00
|
|
|
foreach ($types as $type) {
|
|
|
|
$parent = $model->parentNode();
|
|
|
|
if (!empty($parent)) {
|
|
|
|
$parent = $this->node($model, $parent, $type);
|
|
|
|
}
|
|
|
|
$data = array(
|
|
|
|
'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
|
2011-06-23 19:48:06 +00:00
|
|
|
'model' => $model->name,
|
2011-02-27 15:08:52 +00:00
|
|
|
'foreign_key' => $model->id
|
|
|
|
);
|
|
|
|
if (!$created) {
|
|
|
|
$node = $this->node($model, null, $type);
|
|
|
|
$data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
|
|
|
|
}
|
|
|
|
$model->{$type}->create();
|
|
|
|
$model->{$type}->save($data);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Destroys the ARO/ACO node bound to the deleted record
|
|
|
|
*
|
2008-07-31 03:26:15 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function afterDelete($model) {
|
2011-02-27 15:18:32 +00:00
|
|
|
$types = $this->__typeMaps[$this->settings[$model->name]['type']];
|
2011-02-27 15:08:52 +00:00
|
|
|
if (!is_array($types)) {
|
|
|
|
$types = array($types);
|
|
|
|
}
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$node = Set::extract($this->node($model, null, $type), "0.{$type}.id");
|
|
|
|
if (!empty($node)) {
|
|
|
|
$model->{$type}->delete($node);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|