PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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