acl.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: libs_2model_2behaviors_2acl_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
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  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2006-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs.model.behaviors
00023  * @since           CakePHP v 1.2.0.4487
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Short description for file
00031  *
00032  * Long description for file
00033  *
00034  * @package     cake
00035  * @subpackage  cake.cake.libs.model.behaviors
00036  */
00037 class AclBehavior extends ModelBehavior {
00038 
00039 /**
00040  * Maps ACL type options to ACL models
00041  *
00042  * @var array
00043  * @access protected
00044  */
00045     var $__typeMaps = array('requester' => 'Aro', 'controlled' => 'Aco');
00046 /**
00047  * Sets up the configuation for the model, and loads ACL models if they haven't been already
00048  *
00049  * @param mixed $config
00050  */
00051     function setup(&$model, $config = array()) {
00052         if (is_string($config)) {
00053             $config = array('type' => $config);
00054         }
00055         $this->settings[$model->alias] = array_merge(array('type' => 'requester'), (array)$config);
00056         $type = $this->__typeMaps[$this->settings[$model->alias]['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  */
00071     function node(&$model, $ref = null) {
00072         $type = $this->__typeMaps[strtolower($this->settings[$model->alias]['type'])];
00073         if (empty($ref)) {
00074             $ref = array('model' => $model->alias, 'foreign_key' => $model->id);
00075         }
00076         return $model->{$type}->node($ref);
00077     }
00078 /**
00079  * Creates a new ARO/ACO node bound to this record
00080  *
00081  * @param boolean $created True if this is a new record
00082  */
00083     function afterSave(&$model, $created) {
00084         if ($created) {
00085             $type = $this->__typeMaps[strtolower($this->settings[$model->alias]['type'])];
00086             $parent = $model->parentNode();
00087             if (!empty($parent)) {
00088                 $parent = $this->node($model, $parent);
00089             } else {
00090                 $parent = null;
00091             }
00092 
00093             $model->{$type}->create();
00094             $model->{$type}->save(array(
00095                 'parent_id'     => Set::extract($parent, "0.{$type}.id"),
00096                 'model'         => $model->alias,
00097                 'foreign_key'   => $model->id
00098             ));
00099         }
00100     }
00101 /**
00102  * Destroys the ARO/ACO node bound to the deleted record
00103  *
00104  */
00105     function afterDelete(&$model) {
00106         $type = $this->__typeMaps[strtolower($this->settings[$model->alias]['type'])];
00107         $node = Set::extract($this->node($model), "0.{$type}.id");
00108         if (!empty($node)) {
00109             $model->{$type}->delete($node);
00110         }
00111     }
00112 }
00113 
00114 ?>