AclBehavior.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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-2012, 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-2012, 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('ModelBehavior', 'Model');
  22. App::uses('AclNode', 'Model');
  23. App::uses('Hash', 'Utility');
  24. /**
  25. * ACL behavior
  26. *
  27. * Enables objects to easily tie into an ACL system
  28. *
  29. * @package Cake.Model.Behavior
  30. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html
  31. */
  32. class AclBehavior extends ModelBehavior {
  33. /**
  34. * Maps ACL type options to ACL models
  35. *
  36. * @var array
  37. */
  38. protected $_typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco', 'both' => array('Aro', 'Aco'));
  39. /**
  40. * Sets up the configuration for the model, and loads ACL models if they haven't been already
  41. *
  42. * @param Model $model
  43. * @param array $config
  44. * @return void
  45. */
  46. public function setup(Model $model, $config = array()) {
  47. if (isset($config[0])) {
  48. $config['type'] = $config[0];
  49. unset($config[0]);
  50. }
  51. $this->settings[$model->name] = array_merge(array('type' => 'controlled'), $config);
  52. $this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
  53. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  54. if (!is_array($types)) {
  55. $types = array($types);
  56. }
  57. foreach ($types as $type) {
  58. $model->{$type} = ClassRegistry::init($type);
  59. }
  60. if (!method_exists($model, 'parentNode')) {
  61. trigger_error(__d('cake_dev', 'Callback parentNode() not defined in %s', $model->alias), E_USER_WARNING);
  62. }
  63. }
  64. /**
  65. * Retrieves the Aro/Aco node for this model
  66. *
  67. * @param Model $model
  68. * @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
  69. * @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
  70. * @return array
  71. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html#node
  72. */
  73. public function node(Model $model, $ref = null, $type = null) {
  74. if (empty($type)) {
  75. $type = $this->_typeMaps[$this->settings[$model->name]['type']];
  76. if (is_array($type)) {
  77. trigger_error(__d('cake_dev', 'AclBehavior is setup with more then one type, please specify type parameter for node()'), E_USER_WARNING);
  78. return null;
  79. }
  80. }
  81. if (empty($ref)) {
  82. $ref = array('model' => $model->name, 'foreign_key' => $model->id);
  83. }
  84. return $model->{$type}->node($ref);
  85. }
  86. /**
  87. * Creates a new ARO/ACO node bound to this record
  88. *
  89. * @param Model $model
  90. * @param boolean $created True if this is a new record
  91. * @return void
  92. */
  93. public function afterSave(Model $model, $created) {
  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. }