[go: up one dir, main page]

Menu

[r5]: / models / behaviors / acl.php  Maximize  Restore  History

Download this file

111 lines (97 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
class AclBehavior extends ModelBehavior {
/**
* Maps ACL type options to ACL models
*
* @var array
* @access protected
*/
/**
* Sets up the configuation for the model, and loads ACL models if they haven't been already
*
* @param mixed $config
*/
function setup(&$model, $config = array()) {
if (empty($config)) {
$config = array('Aro');
}
elseif (is_string($config)) {
$config = array($config);
}
$this->settings[$model->name]['types'] = $config;
foreach ($this->settings[$model->name]['types'] as $type)
{
if (!ClassRegistry::isKeySet($type)) {
uses('model' . DS . 'db_acl');
$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, $type, $ref = null) {
if (empty($ref)) {
$ref = array('model' => $model->name, 'foreign_key' => $model->id);
}
return $model->{$type}->node($ref);
}
/**
* Creates a new ARO/ACO node bound to this record
*
* @param boolean $created True if this is a new record
*/
function afterSave(&$model, $created) {
if ($created) {
foreach ($this->settings[$model->name]['types'] as $type)
{
if ($parent = $model->parentNode($type)) {
$parent = $this->node($model, $type, $parent);
} else {
$parent = $model->{$type}->node($model->name);
}
$parent_id = Set::extract($parent, "0.{$type}.id");
if($model->name=="Group"){
if(!empty($parent) && count($parent)>=2){
$root = $parent[count($parent)-2];
$alias = $root[$type]['alias'].":".$model->data['Group']['title'];
}
else
$alias = $model->data['Group']['title'];
}
else
$alias = $model->name . "::" . $model->id;
$model->{$type}->create();
$model->{$type}->save(array(
'parent_id' => $parent_id,
'model' => $model->name,
'foreign_key' => $model->id,
'alias' => $alias
));
}
}
}
/**
* Destroys the ARO/ACO node bound to the deleted record
*
*/
function afterDelete(&$model) {
foreach ($this->settings[$model->name]['types'] as $type)
{
$node = Set::extract($this->node($model, $type), "0.{$type}.id");
if (!empty($node)) {
$model->{$type}->delete($node);
}
}
}
}
?>