AclBehavior.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. App::uses('ModelBehavior', 'Model');
  21. App::uses('AclNode', 'Model');
  22. App::uses('Hash', 'Utility');
  23. /**
  24. * ACL behavior
  25. *
  26. * Enables objects to easily tie into an ACL system
  27. *
  28. * @package Cake.Model.Behavior
  29. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html
  30. */
  31. class AclBehavior extends ModelBehavior {
  32. /**
  33. * Maps ACL type options to ACL models
  34. *
  35. * @var array
  36. */
  37. protected $_typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco', 'both' => array('Aro', 'Aco'));
  38. /**
  39. * Sets up the configuration for the model, and loads ACL models if they haven't been already
  40. *
  41. * @param Model $model
  42. * @param array $config
  43. * @return void
  44. */
  45. public function setup(Model $model, $config = array()) {
  46. if (isset($config[0])) {
  47. $config['type'] = $config[0];
  48. unset($config[0]);
  49. }
  50. $this->settings[$model->name] = array_merge(array('type' => 'controlled'), $config);
  51. $this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
  52. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  53. if (!is_array($types)) {
  54. $types = array($types);
  55. }
  56. foreach ($types as $type) {
  57. $model->{$type} = ClassRegistry::init($type);
  58. }
  59. if (!method_exists($model, 'parentNode')) {
  60. trigger_error(__d('cake_dev', 'Callback %s not defined in %s', 'parentNode()', $model->alias), E_USER_WARNING);
  61. }
  62. }
  63. /**
  64. * Retrieves the Aro/Aco node for this model
  65. *
  66. * @param Model $model
  67. * @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
  68. * @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
  69. * @return array
  70. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html#node
  71. */
  72. public function node(Model $model, $ref = null, $type = null) {
  73. if (empty($type)) {
  74. $type = $this->_typeMaps[$this->settings[$model->name]['type']];
  75. if (is_array($type)) {
  76. trigger_error(__d('cake_dev', 'AclBehavior is setup with more then one type, please specify type parameter for node()'), E_USER_WARNING);
  77. return null;
  78. }
  79. }
  80. if (empty($ref)) {
  81. $ref = array('model' => $model->name, 'foreign_key' => $model->id);
  82. }
  83. return $model->{$type}->node($ref);
  84. }
  85. /**
  86. * Creates a new ARO/ACO node bound to this record
  87. *
  88. * @param Model $model
  89. * @param boolean $created True if this is a new record
  90. * @param array $options Options passed from Model::save().
  91. * @return void
  92. */
  93. public function afterSave(Model $model, $created, $options = array()) {
  94. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  95. if (!is_array($types)) {
  96. $types = array($types);
  97. }
  98. foreach ($types as $type) {
  99. $parent = $model->parentNode();
  100. if (!empty($parent)) {
  101. $parent = $this->node($model, $parent, $type);
  102. }
  103. $data = array(
  104. 'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
  105. 'model' => $model->name,
  106. 'foreign_key' => $model->id
  107. );
  108. if (!$created) {
  109. $node = $this->node($model, null, $type);
  110. $data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
  111. }
  112. $model->{$type}->create();
  113. $model->{$type}->save($data);
  114. }
  115. }
  116. /**
  117. * Destroys the ARO/ACO node bound to the deleted record
  118. *
  119. * @param Model $model
  120. * @return void
  121. */
  122. public function afterDelete(Model $model) {
  123. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  124. if (!is_array($types)) {
  125. $types = array($types);
  126. }
  127. foreach ($types as $type) {
  128. $node = Hash::extract($this->node($model, null, $type), "0.{$type}.id");
  129. if (!empty($node)) {
  130. $model->{$type}->delete($node);
  131. }
  132. }
  133. }
  134. }