acl.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: acl.php 7847 2008-11-08 02:54:07Z renan.saddam $ */
00003 /**
00004  * ACL behavior class.
00005  *
00006  * Enables objects to easily tie into an ACL system
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP :  Rapid Development Framework (http://www.cakephp.org)
00011  * Copyright 2006-2008, Cake Software Foundation, Inc.
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright     Copyright 2006-2008, Cake Software Foundation, Inc.
00018  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
00019  * @package       cake
00020  * @subpackage    cake.cake.libs.model.behaviors
00021  * @since         CakePHP v 1.2.0.4487
00022  * @version       $Revision: 7847 $
00023  * @modifiedby    $LastChangedBy: renan.saddam $
00024  * @lastmodified  $Date: 2008-11-07 21:54:07 -0500 (Fri, 07 Nov 2008) $
00025  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Short description for file
00029  *
00030  * Long description for file
00031  *
00032  * @package       cake
00033  * @subpackage    cake.cake.libs.model.behaviors
00034  */
00035 class AclBehavior extends ModelBehavior {
00036 /**
00037  * Maps ACL type options to ACL models
00038  *
00039  * @var array
00040  * @access protected
00041  */
00042     var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
00043 /**
00044  * Sets up the configuation for the model, and loads ACL models if they haven't been already
00045  *
00046  * @param mixed $config
00047  * @return void
00048  * @access public
00049  */
00050     function setup(&$model, $config = array()) {
00051         if (is_string($config)) {
00052             $config = array('type' => $config);
00053         }
00054         $this->settings[$model->name] = array_merge(array('type' => 'requester'), (array)$config);
00055 
00056         $type = $this->__typeMaps[$this->settings[$model->name]['type']];
00057         if (!class_exists('AclNode')) {
00058             uses('model' . DS . 'db_acl');
00059         }
00060         $model->{$type} =& ClassRegistry::init($type);
00061         if (!method_exists($model, 'parentNode')) {
00062             trigger_error("Callback parentNode() not defined in {$model->alias}", E_USER_WARNING);
00063         }
00064     }
00065 /**
00066  * Retrieves the Aro/Aco node for this model
00067  *
00068  * @param mixed $ref
00069  * @return array
00070  * @access public
00071  */
00072     function node(&$model, $ref = null) {
00073         $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
00074         if (empty($ref)) {
00075             $ref = array('model' => $model->name, 'foreign_key' => $model->id);
00076         }
00077         return $model->{$type}->node($ref);
00078     }
00079 /**
00080  * Creates a new ARO/ACO node bound to this record
00081  *
00082  * @param boolean $created True if this is a new record
00083  * @return void
00084  * @access public
00085  */
00086     function afterSave(&$model, $created) {
00087         if ($created) {
00088             $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
00089             $parent = $model->parentNode();
00090             if (!empty($parent)) {
00091                 $parent = $this->node($model, $parent);
00092             } else {
00093                 $parent = null;
00094             }
00095 
00096             $model->{$type}->create();
00097             $model->{$type}->save(array(
00098                 'parent_id'     => Set::extract($parent, "0.{$type}.id"),
00099                 'model'         => $model->name,
00100                 'foreign_key'   => $model->id
00101             ));
00102         }
00103     }
00104 /**
00105  * Destroys the ARO/ACO node bound to the deleted record
00106  *
00107  * @return void
00108  * @access public
00109  */
00110     function afterDelete(&$model) {
00111         $type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
00112         $node = Set::extract($this->node($model), "0.{$type}.id");
00113         if (!empty($node)) {
00114             $model->{$type}->delete($node);
00115         }
00116     }
00117 }
00118 
00119 ?>

Generated on Sun Nov 22 00:30:51 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7