2007-02-09 08:13:13 +00:00
|
|
|
<?php
|
2007-02-09 18:18:01 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
2007-02-10 22:59:08 +00:00
|
|
|
* ACL behavior class.
|
2007-02-09 18:18:01 +00:00
|
|
|
*
|
2007-02-10 22:59:08 +00:00
|
|
|
* Enables objects to easily tie into an ACL system
|
2007-02-09 18:18:01 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
|
|
* @package cake
|
2007-05-01 02:44:00 +00:00
|
|
|
* @subpackage cake.cake.libs.model.behaviors
|
2007-02-09 18:18:01 +00:00
|
|
|
* @since CakePHP v 1.2.0.4487
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Short description for file
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model.behaviors
|
|
|
|
*/
|
2007-02-09 08:13:13 +00:00
|
|
|
class AclBehavior extends ModelBehavior {
|
|
|
|
|
2007-02-10 20:28:22 +00:00
|
|
|
/**
|
|
|
|
* Maps ACL type options to ACL models
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
2007-02-09 08:13:13 +00:00
|
|
|
var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
|
2007-02-10 20:28:22 +00:00
|
|
|
/**
|
|
|
|
* Sets up the configuation for the model, and loads ACL models if they haven't been already
|
|
|
|
*
|
|
|
|
* @param mixed $config
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-02-09 08:13:13 +00:00
|
|
|
function setup(&$model, $config = array()) {
|
|
|
|
if (is_string($config)) {
|
|
|
|
$config = array('type' => $config);
|
|
|
|
}
|
2007-02-09 19:13:38 +00:00
|
|
|
$this->settings[$model->name] = am(array('type' => 'requester'), $config);
|
2007-02-09 08:13:13 +00:00
|
|
|
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
|
|
|
|
|
|
|
|
if (!ClassRegistry::isKeySet($type)) {
|
2007-05-01 02:30:26 +00:00
|
|
|
uses('model' . DS . 'db_acl');
|
2007-02-09 08:13:13 +00:00
|
|
|
$object =& new $type();
|
|
|
|
} else {
|
|
|
|
$object =& ClassRegistry::getObject($type);
|
|
|
|
}
|
|
|
|
$model->{$type} =& $object;
|
|
|
|
if (!method_exists($model, 'parentNode')) {
|
|
|
|
trigger_error("Callback parentNode() not defined in {$model->name}", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Retrieves the Aro/Aco node for this model
|
|
|
|
*
|
|
|
|
* @param mixed $ref
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function node(&$model, $ref = null) {
|
|
|
|
$type = $this->__typeMaps[low($this->settings[$model->name]['type'])];
|
|
|
|
if (empty($ref)) {
|
|
|
|
$ref = array('model' => $model->name, 'foreign_key' => $model->id);
|
|
|
|
}
|
2007-02-10 20:28:22 +00:00
|
|
|
return $model->{$type}->node($ref);
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
2007-02-09 21:26:09 +00:00
|
|
|
/**
|
|
|
|
* Creates a new ARO/ACO node bound to this record
|
|
|
|
*
|
|
|
|
* @param boolean $created True if this is a new record
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-02-09 08:13:13 +00:00
|
|
|
function afterSave(&$model, $created) {
|
|
|
|
if ($created) {
|
|
|
|
$type = $this->__typeMaps[low($this->settings[$model->name]['type'])];
|
2007-02-15 06:09:20 +00:00
|
|
|
$parent = $model->parentNode();
|
|
|
|
if (!empty($parent)) {
|
|
|
|
$parent = $this->node($model, $parent);
|
|
|
|
} else {
|
|
|
|
$parent = null;
|
|
|
|
}
|
2007-02-10 22:59:08 +00:00
|
|
|
|
2007-02-10 04:15:25 +00:00
|
|
|
$model->{$type}->create();
|
2007-02-09 08:13:13 +00:00
|
|
|
$model->{$type}->save(array(
|
2007-02-14 07:26:42 +00:00
|
|
|
'parent_id' => Set::extract($parent, "0.{$type}.id"),
|
2007-02-09 08:13:13 +00:00
|
|
|
'model' => $model->name,
|
|
|
|
'foreign_key' => $model->id
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2007-02-09 21:26:09 +00:00
|
|
|
/**
|
|
|
|
* Destroys the ARO/ACO node bound to the deleted record
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-02-09 08:13:13 +00:00
|
|
|
function afterDelete(&$model) {
|
|
|
|
$type = $this->__typeMaps[low($this->settings[$model->name]['type'])];
|
2007-02-14 07:26:42 +00:00
|
|
|
$node = Set::extract($this->node($model), "0.{$type}.id");
|
|
|
|
if (!empty($node)) {
|
|
|
|
$model->{$type}->delete($node);
|
|
|
|
}
|
2007-02-09 08:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|