AclBehavior.php 4.2 KB

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