db_acl.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: cake_2libs_2model_2db__acl_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * This is core configuration file.
00005  *
00006  * Use it to configure core behaviour ofCake.
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-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 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs.model
00023  * @since           CakePHP(tm) v 0.2.9
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  * Set database config if not defined.
00031  */
00032 /**
00033  * Load Model and AppModel
00034  */
00035 App::import('Model', 'App');
00036 /**
00037  * Short description for file.
00038  *
00039  * Long description for file
00040  *
00041  *
00042  * @package     cake
00043  * @subpackage  cake.cake.libs.model
00044  */
00045 class AclNode extends AppModel {
00046 /**
00047  * Explicitly disable in-memory query caching for ACL models
00048  *
00049  * @var boolean
00050  * @access public
00051  */
00052     var $cacheQueries = false;
00053 /**
00054  * ACL models use the Tree behavior
00055  *
00056  * @var array
00057  * @access public
00058  */
00059     var $actsAs = array('Tree' => 'nested');
00060 /**
00061  * Constructor
00062  *
00063  */
00064     function __construct() {
00065         $config = Configure::read('Acl.database');
00066         if(isset($config)) {
00067             $this->useDbConfig = $config;
00068         }
00069         parent::__construct();
00070     }
00071 /**
00072  * Retrieves the Aro/Aco node for this model
00073  *
00074  * @param mixed $ref Array with 'model' and 'foreign_key', model object, or string value
00075  * @return array Node found in database
00076  * @access public
00077  */
00078     function node($ref = null) {
00079         $db =& ConnectionManager::getDataSource($this->useDbConfig);
00080         $type = $this->alias;
00081         $result = null;
00082 
00083         if (!empty($this->useTable)) {
00084             $table = $this->useTable;
00085         } else {
00086             $table = Inflector::pluralize(Inflector::underscore($type));
00087         }
00088 
00089         if (empty($ref)) {
00090             return null;
00091         } elseif (is_string($ref)) {
00092             $path = explode('/', $ref);
00093             $start = $path[0];
00094             unset($path[0]);
00095 
00096             $queryData = array(
00097                 'conditions' => array(
00098                     $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
00099                     $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
00100                 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
00101                 'joins' => array(array(
00102                     'table' => $db->fullTableName($this),
00103                     'alias' => "{$type}0",
00104                     'type' => 'LEFT',
00105                     'conditions' => array("{$type}0.alias" => $start)
00106                 )),
00107                 'order' => $db->name("{$type}.lft") . ' DESC'
00108             );
00109 
00110             foreach ($path as $i => $alias) {
00111                 $j = $i - 1;
00112 
00113                 $queryData['joins'][] = array(
00114                     'table' => $db->fullTableName($this),
00115                     'alias' => "{$type}{$i}",
00116                     'type'  => 'LEFT',
00117                     'conditions' => array(
00118                         $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
00119                         $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
00120                         $db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string')
00121                     )
00122                 );
00123 
00124                 $queryData['conditions'] = array('or' => array(
00125                     $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
00126                     $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
00127                 );
00128             }
00129             $result = $db->read($this, $queryData, -1);
00130             $path = array_values($path);
00131 
00132             if (
00133                 !isset($result[0][$type]) ||
00134                 (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
00135                 (empty($path) && $result[0][$type]['alias'] != $start)
00136             ) {
00137                 return false;
00138             }
00139         } elseif (is_object($ref) && is_a($ref, 'Model')) {
00140             $ref = array('model' => $ref->alias, 'foreign_key' => $ref->id);
00141         } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
00142             $name = key($ref);
00143 
00144             if (PHP5) {
00145                 $model = ClassRegistry::init(array('class' => $name, 'alias' => $name));
00146             } else {
00147                 $model =& ClassRegistry::init(array('class' => $name, 'alias' => $name));
00148             }
00149 
00150             if (empty($model)) {
00151                 trigger_error("Model class '$name' not found in AclNode::node() when trying to bind {$this->alias} object", E_USER_WARNING);
00152                 return null;
00153             }
00154 
00155             $tmpRef = null;
00156             if (method_exists($model, 'bindNode')) {
00157                 $tmpRef = $model->bindNode($ref);
00158             }
00159             if (empty($tmpRef)) {
00160                 $ref = array('model' => $name, 'foreign_key' => $ref[$name][$model->primaryKey]);
00161             } else {
00162                 if (is_string($tmpRef)) {
00163                     return $this->node($tmpRef);
00164                 }
00165                 $ref = $tmpRef;
00166             }
00167         }
00168         if (is_array($ref)) {
00169             if (is_array(current($ref)) && is_string(key($ref))) {
00170                 $name = key($ref);
00171                 $ref = current($ref);
00172             }
00173             foreach ($ref as $key => $val) {
00174                 if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
00175                     unset($ref[$key]);
00176                     $ref["{$type}0.{$key}"] = $val;
00177                 }
00178             }
00179             $queryData = array(
00180                 'conditions' => $ref,
00181                 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
00182                 'joins' => array(array(
00183                     'table' => $db->fullTableName($table),
00184                     'alias' => "{$type}0",
00185                     'type' => 'LEFT',
00186                     'conditions' => array(
00187                         $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
00188                         $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
00189                     )
00190                 )),
00191                 'order' => $db->name("{$type}.lft") . ' DESC'
00192             );
00193             $result = $db->read($this, $queryData, -1);
00194 
00195             if (!$result) {
00196                 trigger_error("AclNode::node() - Couldn't find {$type} node identified by \"" . print_r($ref, true) . "\"", E_USER_WARNING);
00197             }
00198         }
00199         return $result;
00200     }
00201 }
00202 /**
00203  * Access Control Object
00204  *
00205  * @package cake
00206  * @subpackage cake.cake.libs.model
00207  */
00208 class Aco extends AclNode {
00209 /**
00210  * Model name
00211  *
00212  * @var string
00213  * @access public
00214  */
00215     var $name = 'Aco';
00216 /**
00217  * Binds to ARO nodes through permissions settings
00218  *
00219  * @var array
00220  * @access public
00221  */
00222     var $hasAndBelongsToMany = array('Aro' => array('with' => 'Permission'));
00223 }
00224 /**
00225  * Action for Access Control Object
00226  *
00227  * @package     cake
00228  * @subpackage  cake.cake.libs.model
00229  */
00230 class AcoAction extends AppModel {
00231 /**
00232  * Model name
00233  *
00234  * @var string
00235  * @access public
00236  */
00237     var $name = 'AcoAction';
00238 /**
00239  * ACO Actions belong to ACOs
00240  *
00241  * @var array
00242  * @access public
00243  */
00244     var $belongsTo = array('Aco');
00245 }
00246 /**
00247  * Access Request Object
00248  *
00249  * @package cake
00250  * @subpackage cake.cake.libs.model
00251  */
00252 class Aro extends AclNode {
00253 /**
00254  * Model name
00255  *
00256  * @var string
00257  * @access public
00258  */
00259     var $name = 'Aro';
00260 /**
00261  * AROs are linked to ACOs by means of Permission
00262  *
00263  * @var array
00264  * @access public
00265  */
00266     var $hasAndBelongsToMany = array('Aco' => array('with' => 'Permission'));
00267 }
00268 /**
00269  * Permissions linking AROs with ACOs
00270  *
00271  * @package cake
00272  * @subpackage cake.cake.libs.model
00273  */
00274 class Permission extends AppModel {
00275 /**
00276  * Model name
00277  *
00278  * @var string
00279  * @access public
00280  */
00281     var $name = 'Permission';
00282 /**
00283  * Explicitly disable in-memory query caching
00284  *
00285  * @var boolean
00286  * @access public
00287  */
00288     var $cacheQueries = false;
00289 /**
00290  * Override default table name
00291  *
00292  * @var string
00293  * @access public
00294  */
00295     var $useTable = 'aros_acos';
00296 /**
00297  * Permissions link AROs with ACOs
00298  *
00299  * @var array
00300  * @access public
00301  */
00302     var $belongsTo = array('Aro', 'Aco');
00303 /**
00304  * No behaviors for this model
00305  *
00306  * @var array
00307  * @access public
00308  */
00309     var $actsAs = null;
00310 /**
00311  * Constructor, used to tell this model to use the
00312  * database configured for ACL
00313  */
00314     function __construct() {
00315         $config = Configure::read('Acl.database');
00316         if (!empty($config)) {
00317             $this->useDbConfig = $config;
00318         }
00319         parent::__construct();
00320     }
00321 }
00322 ?>