PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/model/behaviors/acl.php

https://github.com/msadouni/cakephp2x
PHP | 120 lines | 51 code | 8 blank | 61 comment | 7 complexity | 633d907398a095c8b5a8fff85077cb9d MD5 | raw file
  1. <?php
  2. /**
  3. * ACL behavior class.
  4. *
  5. * Enables objects to easily tie into an ACL system
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2006-2009, 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 2006-2009, Cake Software Foundation, Inc.
  16. * @link http://cakephp.org CakePHP Project
  17. * @package cake
  18. * @subpackage cake.cake.libs.model.behaviors
  19. * @since CakePHP v 1.2.0.4487
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. /**
  23. * Short description for file
  24. *
  25. * Long description for file
  26. *
  27. * @package cake
  28. * @subpackage cake.cake.libs.model.behaviors
  29. */
  30. class AclBehavior extends ModelBehavior {
  31. /**
  32. * Maps ACL type options to ACL models
  33. *
  34. * @var array
  35. * @access protected
  36. */
  37. private $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
  38. /**
  39. * Sets up the configuation for the model, and loads ACL models if they haven't been already
  40. *
  41. * @param mixed $config
  42. * @return void
  43. * @access public
  44. */
  45. public function setup(&$model, $config = array()) {
  46. if (is_string($config)) {
  47. $config = array('type' => $config);
  48. }
  49. $this->settings[$model->name] = array_merge(array('type' => 'requester'), (array)$config);
  50. $type = $this->__typeMaps[$this->settings[$model->name]['type']];
  51. if (!class_exists('AclNode')) {
  52. require LIBS . 'model' . DS . 'db_acl.php';
  53. }
  54. $model->{$type} = ClassRegistry::init($type);
  55. if (!method_exists($model, 'parentNode')) {
  56. trigger_error("Callback parentNode() not defined in {$model->alias}", E_USER_WARNING);
  57. }
  58. }
  59. /**
  60. * Retrieves the Aro/Aco node for this model
  61. *
  62. * @param mixed $ref
  63. * @return array
  64. * @access public
  65. */
  66. public function node(&$model, $ref = null) {
  67. $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
  68. if (empty($ref)) {
  69. $ref = array('model' => $model->name, 'foreign_key' => $model->id);
  70. }
  71. return $model->{$type}->node($ref);
  72. }
  73. /**
  74. * Creates a new ARO/ACO node bound to this record
  75. *
  76. * @param boolean $created True if this is a new record
  77. * @return void
  78. * @access public
  79. */
  80. public function afterSave(&$model, $created) {
  81. $type = $this->__typeMaps[strtolower($this->settings[$model->alias]['type'])];
  82. $parent = $model->parentNode();
  83. if (!empty($parent)) {
  84. $parent = $this->node($model, $parent);
  85. }
  86. $data = array(
  87. 'parent_id' => isset($parent[0][$type]['id']) ? $parent[0][$type]['id'] : null,
  88. 'model' => $model->alias,
  89. 'foreign_key' => $model->id
  90. );
  91. if (!$created) {
  92. $node = $this->node($model);
  93. $data['id'] = isset($node[0][$type]['id']) ? $node[0][$type]['id'] : null;
  94. }
  95. $model->{$type}->create();
  96. $model->{$type}->save($data);
  97. }
  98. /**
  99. * Destroys the ARO/ACO node bound to the deleted record
  100. *
  101. * @return void
  102. * @access public
  103. */
  104. public function afterDelete(&$model) {
  105. $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
  106. $node = Set::extract($this->node($model), "0.{$type}.id");
  107. if (!empty($node)) {
  108. $model->{$type}->delete($node);
  109. }
  110. }
  111. }
  112. ?>