PageRenderTime 248ms CodeModel.GetById 38ms RepoModel.GetById 26ms app.codeStats 0ms

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

https://github.com/MrRio/wildflower
PHP | 119 lines | 50 code | 3 blank | 66 comment | 8 complexity | eacac464f83accf5ae7d9b54142ab754 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id: acl.php 7945 2008-12-19 02:16:01Z gwoo $ */
  3. /**
  4. * ACL behavior class.
  5. *
  6. * Enables objects to easily tie into an ACL system
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2006-2008, Cake Software Foundation, Inc.
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
  19. * @package cake
  20. * @subpackage cake.cake.libs.model.behaviors
  21. * @since CakePHP v 1.2.0.4487
  22. * @version $Revision: 7945 $
  23. * @modifiedby $LastChangedBy: gwoo $
  24. * @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. /**
  28. * Short description for file
  29. *
  30. * Long description for file
  31. *
  32. * @package cake
  33. * @subpackage cake.cake.libs.model.behaviors
  34. */
  35. class AclBehavior extends ModelBehavior {
  36. /**
  37. * Maps ACL type options to ACL models
  38. *
  39. * @var array
  40. * @access protected
  41. */
  42. var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
  43. /**
  44. * Sets up the configuation for the model, and loads ACL models if they haven't been already
  45. *
  46. * @param mixed $config
  47. * @return void
  48. * @access public
  49. */
  50. function setup(&$model, $config = array()) {
  51. if (is_string($config)) {
  52. $config = array('type' => $config);
  53. }
  54. $this->settings[$model->name] = array_merge(array('type' => 'requester'), (array)$config);
  55. $type = $this->__typeMaps[$this->settings[$model->name]['type']];
  56. if (!class_exists('AclNode')) {
  57. uses('model' . DS . 'db_acl');
  58. }
  59. $model->{$type} =& ClassRegistry::init($type);
  60. if (!method_exists($model, 'parentNode')) {
  61. trigger_error("Callback parentNode() not defined in {$model->alias}", E_USER_WARNING);
  62. }
  63. }
  64. /**
  65. * Retrieves the Aro/Aco node for this model
  66. *
  67. * @param mixed $ref
  68. * @return array
  69. * @access public
  70. */
  71. function node(&$model, $ref = null) {
  72. $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
  73. if (empty($ref)) {
  74. $ref = array('model' => $model->name, 'foreign_key' => $model->id);
  75. }
  76. return $model->{$type}->node($ref);
  77. }
  78. /**
  79. * Creates a new ARO/ACO node bound to this record
  80. *
  81. * @param boolean $created True if this is a new record
  82. * @return void
  83. * @access public
  84. */
  85. function afterSave(&$model, $created) {
  86. if ($created) {
  87. $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
  88. $parent = $model->parentNode();
  89. if (!empty($parent)) {
  90. $parent = $this->node($model, $parent);
  91. } else {
  92. $parent = null;
  93. }
  94. $model->{$type}->create();
  95. $model->{$type}->save(array(
  96. 'parent_id' => Set::extract($parent, "0.{$type}.id"),
  97. 'model' => $model->name,
  98. 'foreign_key' => $model->id
  99. ));
  100. }
  101. }
  102. /**
  103. * Destroys the ARO/ACO node bound to the deleted record
  104. *
  105. * @return void
  106. * @access public
  107. */
  108. function afterDelete(&$model) {
  109. $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
  110. $node = Set::extract($this->node($model), "0.{$type}.id");
  111. if (!empty($node)) {
  112. $model->{$type}->delete($node);
  113. }
  114. }
  115. }
  116. ?>