db_acl.php

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

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