AclBehavior.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 2005-2011, Cake Software Foundation, Inc.
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
  16. * @link http://cakephp.org CakePHP Project
  17. * @package cake.libs.model.behaviors
  18. * @since CakePHP v 1.2.0.4487
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('AclNode', 'Model');
  22. /**
  23. * ACL behavior
  24. *
  25. * @package cake.libs.model.behaviors
  26. * @link http://book.cakephp.org/view/1320/ACL
  27. */
  28. class AclBehavior extends ModelBehavior {
  29. /**
  30. * Maps ACL type options to ACL models
  31. *
  32. * @var array
  33. */
  34. private $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco', 'both' => array('Aro', 'Aco'));
  35. /**
  36. * Sets up the configuation for the model, and loads ACL models if they haven't been already
  37. *
  38. * @param mixed $config
  39. * @return void
  40. */
  41. public function setup($model, $config = array()) {
  42. if (is_string($config)) {
  43. $config = array('type' => $config);
  44. }
  45. $this->settings[$model->name] = array_merge(array('type' => 'controlled'), (array)$config);
  46. $this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
  47. $types = $this->__typeMaps[$this->settings[$model->name]['type']];
  48. if (!is_array($types)) {
  49. $types = array($types);
  50. }
  51. foreach ($types as $type) {
  52. $model->{$type} = ClassRegistry::init($type);
  53. }
  54. if (!method_exists($model, 'parentNode')) {
  55. trigger_error(__d('cake_dev', 'Callback parentNode() not defined in %s', $model->alias), E_USER_WARNING);
  56. }
  57. }
  58. /**
  59. * Retrieves the Aro/Aco node for this model
  60. *
  61. * @param mixed $ref
  62. * @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
  63. * @return array
  64. * @link http://book.cakephp.org/view/1322/node
  65. */
  66. public function node($model, $ref = null, $type = null) {
  67. if (empty($type)) {
  68. $type = $this->__typeMaps[$this->settings[$model->name]['type']];
  69. if (is_array($type)) {
  70. trigger_error(__('AclBehavior is setup with more then one type, please specify type parameter for node()', true), E_USER_WARNING);
  71. return null;
  72. }
  73. }
  74. if (empty($ref)) {
  75. $ref = array('model' => $model->name, 'foreign_key' => $model->id);
  76. }
  77. return $model->{$type}->node($ref);
  78. }
  79. /**
  80. * Creates a new ARO/ACO node bound to this record
  81. *
  82. * @param boolean $created True if this is a new record
  83. * @return void
  84. */
  85. public function afterSave($model, $created) {
  86. $types = $this->__typeMaps[$this->settings[$model->name]['type']];
  87. if (!is_array($types)) {
  88. $types = array($types);
  89. }
  90. foreach ($types as $type) {
  91. $parent = $model->parentNode();
  92. if (!empty($parent)) {
  93. $parent = $this->node($model, $parent, $type);
  94. }
  95. $data = array(
  96. 'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
  97. 'model' => $model->name,
  98. 'foreign_key' => $model->id
  99. );
  100. if (!$created) {
  101. $node = $this->node($model, null, $type);
  102. $data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
  103. }
  104. $model->{$type}->create();
  105. $model->{$type}->save($data);
  106. }
  107. }
  108. /**
  109. * Destroys the ARO/ACO node bound to the deleted record
  110. *
  111. * @return void
  112. */
  113. public function afterDelete($model) {
  114. $types = $this->__typeMaps[$this->settings[$model->name]['type']];
  115. if (!is_array($types)) {
  116. $types = array($types);
  117. }
  118. foreach ($types as $type) {
  119. $node = Set::extract($this->node($model, null, $type), "0.{$type}.id");
  120. if (!empty($node)) {
  121. $model->{$type}->delete($node);
  122. }
  123. }
  124. }
  125. }