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)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
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
|
|
|
*/
|
2012-07-21 18:29:49 +00:00
|
|
|
App::uses('ModelBehavior', 'Model');
|
2011-05-28 13:45:06 +00:00
|
|
|
App::uses('AclNode', 'Model');
|
2012-03-11 01:57:18 +00:00
|
|
|
App::uses('Hash', 'Utility');
|
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-12-08 15:35:02 +00:00
|
|
|
* Enables objects to easily tie into an ACL system
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model.Behavior
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html
|
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-08-20 04:43:34 +00:00
|
|
|
protected $_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
|
|
|
/**
|
2011-10-15 18:52:41 +00:00
|
|
|
* Sets up the configuration for the model, and loads ACL models if they haven't been already
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @param Model $model
|
|
|
|
* @param array $config
|
2008-07-31 03:26:15 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function setup(Model $model, $config = array()) {
|
2011-10-16 10:06:41 +00:00
|
|
|
if (isset($config[0])) {
|
|
|
|
$config['type'] = $config[0];
|
|
|
|
unset($config[0]);
|
|
|
|
}
|
|
|
|
$this->settings[$model->name] = array_merge(array('type' => 'controlled'), $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-08-20 04:43:34 +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
|
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @param Model $model
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
|
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
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html#node
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function node(Model $model, $ref = null, $type = null) {
|
2011-02-27 15:08:52 +00:00
|
|
|
if (empty($type)) {
|
2011-08-20 04:43:34 +00:00
|
|
|
$type = $this->_typeMaps[$this->settings[$model->name]['type']];
|
2011-02-27 15:08:52 +00:00
|
|
|
if (is_array($type)) {
|
2011-08-09 10:03:32 +00:00
|
|
|
trigger_error(__d('cake_dev', 'AclBehavior is setup with more then one type, please specify type parameter for node()'), E_USER_WARNING);
|
2011-02-27 15:08:52 +00:00
|
|
|
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
|
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @param Model $model
|
2008-05-30 11:40:08 +00:00
|
|
|
* @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
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function afterSave(Model $model, $created) {
|
2011-08-20 04:43:34 +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
|
|
|
|
*
|
2011-07-30 22:38:57 +00:00
|
|
|
* @param Model $model
|
2008-07-31 03:26:15 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:10:29 +00:00
|
|
|
public function afterDelete(Model $model) {
|
2011-08-20 04:43:34 +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) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$node = Hash::extract($this->node($model, null, $type), "0.{$type}.id");
|
2011-02-27 15:08:52 +00:00
|
|
|
if (!empty($node)) {
|
|
|
|
$model->{$type}->delete($node);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|