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)
|
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
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.model.behaviors
|
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
|
|
|
*/
|
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
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package 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
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__typeMaps = array('requester' => 'Aro', 'controlled' => '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);
|
|
|
|
}
|
2008-07-31 03:26:15 +00:00
|
|
|
$this->settings[$model->name] = array_merge(array('type' => 'requester'), (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
|
|
|
|
|
|
|
$type = $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
|
|
|
}
|
2010-07-06 02:19:22 +00:00
|
|
|
$model->{$type} = ClassRegistry::init($type);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!method_exists($model, 'parentNode')) {
|
2010-12-05 01:37:13 +00:00
|
|
|
trigger_error(__('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
|
|
|
|
* @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
|
|
|
*/
|
2010-12-12 22:38:49 +00:00
|
|
|
public function node($model, $ref = null) {
|
2010-07-30 02:04:22 +00:00
|
|
|
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
|
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) {
|
2010-07-30 02:04:22 +00:00
|
|
|
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
|
2009-09-19 05:17:20 +00:00
|
|
|
$parent = $model->parentNode();
|
|
|
|
if (!empty($parent)) {
|
|
|
|
$parent = $this->node($model, $parent);
|
|
|
|
}
|
|
|
|
$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);
|
|
|
|
$data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-19 05:17:20 +00:00
|
|
|
$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) {
|
2010-07-30 02:04:22 +00:00
|
|
|
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
|
2008-05-30 11:40:08 +00:00
|
|
|
$node = Set::extract($this->node($model), "0.{$type}.id");
|
|
|
|
if (!empty($node)) {
|
|
|
|
$model->{$type}->delete($node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|