pictcode / lib / Cake / Model / Behavior / AclBehavior.php @ 9d2f0219
履歴 | 表示 | アノテート | ダウンロード (4.417 KB)
| 1 |
<?php
|
|---|---|
| 2 |
/**
|
| 3 |
* ACL behavior class.
|
| 4 |
*
|
| 5 |
* Enables objects to easily tie into an ACL system
|
| 6 |
*
|
| 7 |
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
| 8 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 9 |
*
|
| 10 |
* Licensed under The MIT License
|
| 11 |
* For full copyright and license information, please see the LICENSE.txt
|
| 12 |
* Redistributions of files must retain the above copyright notice.
|
| 13 |
*
|
| 14 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 15 |
* @link http://cakephp.org CakePHP Project
|
| 16 |
* @package Cake.Model.Behavior
|
| 17 |
* @since CakePHP v 1.2.0.4487
|
| 18 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
| 19 |
*/
|
| 20 |
|
| 21 |
App::uses('ModelBehavior', 'Model'); |
| 22 |
App::uses('AclNode', 'Model'); |
| 23 |
App::uses('Hash', 'Utility'); |
| 24 |
|
| 25 |
/**
|
| 26 |
* ACL behavior
|
| 27 |
*
|
| 28 |
* Enables objects to easily tie into an ACL system
|
| 29 |
*
|
| 30 |
* @package Cake.Model.Behavior
|
| 31 |
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html
|
| 32 |
*/
|
| 33 |
class AclBehavior extends ModelBehavior { |
| 34 |
|
| 35 |
/**
|
| 36 |
* Maps ACL type options to ACL models
|
| 37 |
*
|
| 38 |
* @var array
|
| 39 |
*/
|
| 40 |
protected $_typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco', 'both' => array('Aro', 'Aco')); |
| 41 |
|
| 42 |
/**
|
| 43 |
* Sets up the configuration for the model, and loads ACL models if they haven't been already
|
| 44 |
*
|
| 45 |
* @param Model $model Model using this behavior.
|
| 46 |
* @param array $config Configuration options.
|
| 47 |
* @return void
|
| 48 |
*/
|
| 49 |
public function setup(Model $model, $config = array()) { |
| 50 |
if (isset($config[0])) { |
| 51 |
$config['type'] = $config[0]; |
| 52 |
unset($config[0]); |
| 53 |
} |
| 54 |
$this->settings[$model->name] = array_merge(array('type' => 'controlled'), $config); |
| 55 |
$this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']); |
| 56 |
|
| 57 |
$types = $this->_typeMaps[$this->settings[$model->name]['type']]; |
| 58 |
|
| 59 |
if (!is_array($types)) { |
| 60 |
$types = array($types); |
| 61 |
} |
| 62 |
foreach ($types as $type) { |
| 63 |
$model->{$type} = ClassRegistry::init($type); |
| 64 |
} |
| 65 |
if (!method_exists($model, 'parentNode')) { |
| 66 |
trigger_error(__d('cake_dev', 'Callback %s not defined in %s', 'parentNode()', $model->alias), E_USER_WARNING); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/**
|
| 71 |
* Retrieves the Aro/Aco node for this model
|
| 72 |
*
|
| 73 |
* @param Model $model Model using this behavior.
|
| 74 |
* @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
|
| 75 |
* @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
|
| 76 |
* @return array
|
| 77 |
* @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html#node
|
| 78 |
*/
|
| 79 |
public function node(Model $model, $ref = null, $type = null) { |
| 80 |
if (empty($type)) { |
| 81 |
$type = $this->_typeMaps[$this->settings[$model->name]['type']]; |
| 82 |
if (is_array($type)) { |
| 83 |
trigger_error(__d('cake_dev', 'AclBehavior is setup with more then one type, please specify type parameter for node()'), E_USER_WARNING); |
| 84 |
return array(); |
| 85 |
} |
| 86 |
} |
| 87 |
if (empty($ref)) { |
| 88 |
$ref = array('model' => $model->name, 'foreign_key' => $model->id); |
| 89 |
} |
| 90 |
return $model->{$type}->node($ref); |
| 91 |
} |
| 92 |
|
| 93 |
/**
|
| 94 |
* Creates a new ARO/ACO node bound to this record
|
| 95 |
*
|
| 96 |
* @param Model $model Model using this behavior.
|
| 97 |
* @param bool $created True if this is a new record
|
| 98 |
* @param array $options Options passed from Model::save().
|
| 99 |
* @return void
|
| 100 |
*/
|
| 101 |
public function afterSave(Model $model, $created, $options = array()) { |
| 102 |
$types = $this->_typeMaps[$this->settings[$model->name]['type']]; |
| 103 |
if (!is_array($types)) { |
| 104 |
$types = array($types); |
| 105 |
} |
| 106 |
foreach ($types as $type) { |
| 107 |
$parent = $model->parentNode($type); |
| 108 |
if (!empty($parent)) { |
| 109 |
$parent = $this->node($model, $parent, $type); |
| 110 |
} |
| 111 |
$data = array( |
| 112 |
'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null, |
| 113 |
'model' => $model->name, |
| 114 |
'foreign_key' => $model->id |
| 115 |
); |
| 116 |
if (!$created) { |
| 117 |
$node = $this->node($model, null, $type); |
| 118 |
$data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null; |
| 119 |
} |
| 120 |
$model->{$type}->create(); |
| 121 |
$model->{$type}->save($data); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/**
|
| 126 |
* Destroys the ARO/ACO node bound to the deleted record
|
| 127 |
*
|
| 128 |
* @param Model $model Model using this behavior.
|
| 129 |
* @return void
|
| 130 |
*/
|
| 131 |
public function afterDelete(Model $model) { |
| 132 |
$types = $this->_typeMaps[$this->settings[$model->name]['type']]; |
| 133 |
if (!is_array($types)) { |
| 134 |
$types = array($types); |
| 135 |
} |
| 136 |
foreach ($types as $type) { |
| 137 |
$node = Hash::extract($this->node($model, null, $type), "0.{$type}.id"); |
| 138 |
if (!empty($node)) { |
| 139 |
$model->{$type}->delete($node); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
} |