PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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