/lib/Cake/Model/Behavior/AclBehavior.php

https://github.com/masihnewbie/cakephp · PHP · 138 lines · 71 code · 8 blank · 59 comment · 11 complexity · e7b197588c885f890a031e2cfd0ffd90 MD5 · raw file

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