PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/QuickApps/Cake/Model/Behavior/AclBehavior.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 142 lines | 72 code | 9 blank | 61 comment | 11 complexity | a5c0c4c0b134e8e93c40dd88eb2d21c4 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  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('AclNode', 'Model');
  22. App::uses('Hash', 'Utility');
  23. /**
  24. * ACL behavior
  25. *
  26. * Enables objects to easily tie into an ACL system
  27. *
  28. * @package Cake.Model.Behavior
  29. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html
  30. */
  31. class AclBehavior extends ModelBehavior {
  32. /**
  33. * Maps ACL type options to ACL models
  34. *
  35. * @var array
  36. */
  37. protected $_typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco', 'both' => array('Aro', 'Aco'));
  38. /**
  39. * Sets up the configuration for the model, and loads ACL models if they haven't been already
  40. *
  41. * @param Model $model
  42. * @param array $config
  43. * @return void
  44. */
  45. public function setup(Model $model, $config = array()) {
  46. if (isset($config[0])) {
  47. $config['type'] = $config[0];
  48. unset($config[0]);
  49. }
  50. $this->settings[$model->name] = array_merge(array('type' => 'controlled'), $config);
  51. $this->settings[$model->name]['type'] = strtolower($this->settings[$model->name]['type']);
  52. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  53. if (!is_array($types)) {
  54. $types = array($types);
  55. }
  56. foreach ($types as $type) {
  57. $model->{$type} = ClassRegistry::init($type);
  58. }
  59. if (!method_exists($model, 'parentNode')) {
  60. trigger_error(__d('cake_dev', 'Callback parentNode() not defined in %s', $model->alias), E_USER_WARNING);
  61. }
  62. }
  63. /**
  64. * Retrieves the Aro/Aco node for this model
  65. *
  66. * @param Model $model
  67. * @param mixed $ref
  68. * @param string $type Only needed when Acl is set up as 'both', specify 'Aro' or 'Aco' to get the correct node
  69. * @return array
  70. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/acl.html#node
  71. */
  72. public function node(Model $model, $ref = null, $type = null) {
  73. if (empty($type)) {
  74. $type = $this->_typeMaps[$this->settings[$model->name]['type']];
  75. if (is_array($type)) {
  76. trigger_error(__d('cake_dev', 'AclBehavior is setup with more then one type, please specify type parameter for node()'), E_USER_WARNING);
  77. return null;
  78. }
  79. }
  80. if (empty($ref)) {
  81. $ref = array('model' => $model->name, 'foreign_key' => $model->id);
  82. }
  83. return $model->{$type}->node($ref);
  84. }
  85. /**
  86. * Creates a new ARO/ACO node bound to this record
  87. *
  88. * @param Model $model
  89. * @param boolean $created True if this is a new record
  90. * @return void
  91. */
  92. public function afterSave(Model $model, $created) {
  93. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  94. if (!is_array($types)) {
  95. $types = array($types);
  96. }
  97. foreach ($types as $type) {
  98. $parent = $model->parentNode();
  99. if (!empty($parent)) {
  100. $parent = $this->node($model, $parent, $type);
  101. }
  102. $data = array(
  103. 'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
  104. 'model' => $model->name,
  105. 'foreign_key' => $model->id
  106. );
  107. if (!$created) {
  108. $node = $this->node($model, null, $type);
  109. $data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
  110. }
  111. $model->{$type}->create();
  112. $model->{$type}->save($data);
  113. }
  114. }
  115. /**
  116. * Destroys the ARO/ACO node bound to the deleted record
  117. *
  118. * @param Model $model
  119. * @return void
  120. */
  121. public function afterDelete(Model $model) {
  122. $types = $this->_typeMaps[$this->settings[$model->name]['type']];
  123. if (!is_array($types)) {
  124. $types = array($types);
  125. }
  126. foreach ($types as $type) {
  127. $node = Hash::extract($this->node($model, null, $type), "0.{$type}.id");
  128. if (!empty($node)) {
  129. $model->{$type}->delete($node);
  130. }
  131. }
  132. }
  133. }