AclBehavior.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP Project
  17. * @package Cake.Model.Behavior
  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.Model.Behavior
  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. protected $_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 Model $model
  39. * @param array $config
  40. * @return void
  41. */
  42. public function setup($model, $config = array()) {
  43. if (is_string($config)) {
  44. $config = array('type' => $config);
  45. }
  46. $this->settings[$model->name] = array_merge(array('type' => 'controlled'), (array)$config);
  47. $this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
  48. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  49. if (!is_array($types)) {
  50. $types = array($types);
  51. }
  52. foreach ($types as $type) {
  53. $model->{$type} = ClassRegistry::init($type);
  54. }
  55. if (!method_exists($model, 'parentNode')) {
  56. trigger_error(__d('cake_dev', 'Callback parentNode() not defined in %s', $model->alias), E_USER_WARNING);
  57. }
  58. }
  59. /**
  60. * Retrieves the Aro/Aco node for this model
  61. *
  62. * @param Model $model
  63. * @param mixed $ref
  64. * @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
  65. * @return array
  66. * @link http://book.cakephp.org/view/1322/node
  67. */
  68. public function node($model, $ref = null, $type = null) {
  69. if (empty($type)) {
  70. $type = $this->_typeMaps[$this->settings[$model->name]['type']];
  71. if (is_array($type)) {
  72. trigger_error(__d('cake_dev', 'AclBehavior is setup with more then one type, please specify type parameter for node()'), E_USER_WARNING);
  73. return null;
  74. }
  75. }
  76. if (empty($ref)) {
  77. $ref = array('model' => $model->name, 'foreign_key' => $model->id);
  78. }
  79. return $model->{$type}->node($ref);
  80. }
  81. /**
  82. * Creates a new ARO/ACO node bound to this record
  83. *
  84. * @param Model $model
  85. * @param boolean $created True if this is a new record
  86. * @return void
  87. */
  88. public function afterSave($model, $created) {
  89. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  90. if (!is_array($types)) {
  91. $types = array($types);
  92. }
  93. foreach ($types as $type) {
  94. $parent = $model->parentNode();
  95. if (!empty($parent)) {
  96. $parent = $this->node($model, $parent, $type);
  97. }
  98. $data = array(
  99. 'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
  100. 'model' => $model->name,
  101. 'foreign_key' => $model->id
  102. );
  103. if (!$created) {
  104. $node = $this->node($model, null, $type);
  105. $data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
  106. }
  107. $model->{$type}->create();
  108. $model->{$type}->save($data);
  109. }
  110. }
  111. /**
  112. * Destroys the ARO/ACO node bound to the deleted record
  113. *
  114. * @param Model $model
  115. * @return void
  116. */
  117. public function afterDelete($model) {
  118. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  119. if (!is_array($types)) {
  120. $types = array($types);
  121. }
  122. foreach ($types as $type) {
  123. $node = Set::extract($this->node($model, null, $type), "0.{$type}.id");
  124. if (!empty($node)) {
  125. $model->{$type}->delete($node);
  126. }
  127. }
  128. }
  129. }