2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ACL behavior class.
|
|
|
|
*
|
|
|
|
* Enables objects to easily tie into an ACL system
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2010-01-26 22:15:15 +00:00
|
|
|
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
2010-01-14 04:47:14 +00:00
|
|
|
* Copyright 2006-2010, 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.
|
|
|
|
*
|
2010-01-14 04:47:14 +00:00
|
|
|
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model.behaviors
|
|
|
|
* @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
|
|
|
*/
|
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
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model.behaviors
|
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
|
|
|
|
* @access protected
|
|
|
|
*/
|
2011-02-27 15:08:52 +00:00
|
|
|
var $__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-09-25 16:49:56 +00:00
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function setup(&$model, $config = array()) {
|
|
|
|
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);
|
|
|
|
$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']];
|
2008-06-13 16:40:45 +00:00
|
|
|
if (!class_exists('AclNode')) {
|
2009-07-23 20:53:37 +00:00
|
|
|
require LIBS . 'model' . DS . 'db_acl.php';
|
2008-06-13 16:40:45 +00:00
|
|
|
}
|
2011-02-27 15:08:52 +00:00
|
|
|
if (!is_array($types)) {
|
|
|
|
$types = array($types);
|
|
|
|
}
|
|
|
|
foreach($types as $type) {
|
|
|
|
if (PHP5) {
|
|
|
|
$model->{$type} = ClassRegistry::init($type);
|
|
|
|
} else {
|
|
|
|
$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')) {
|
2010-01-16 20:23:31 +00:00
|
|
|
trigger_error(sprintf(__('Callback parentNode() not defined in %s', true), $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
|
2008-09-25 16:49:56 +00:00
|
|
|
* @access public
|
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-02-27 15:08:52 +00:00
|
|
|
function node(&$model, $ref = null, $type = null) {
|
|
|
|
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)) {
|
2011-02-27 15:18:32 +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-09-25 16:49:56 +00:00
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
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,
|
|
|
|
'model' => $model->alias,
|
|
|
|
'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-09-25 16:49:56 +00:00
|
|
|
* @access public
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2011-02-27 15:08:52 +00:00
|
|
|
}
|