cakephp2-php8/lib/Cake/Model/AclNode.php

185 lines
5.2 KiB
PHP
Raw Normal View History

<?php
/**
*
2010-10-03 16:38:58 +00:00
* PHP 5
*
2009-11-06 06:46:59 +00:00
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
2011-05-29 21:31:39 +00:00
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
2011-05-29 21:31:39 +00:00
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
2009-11-06 06:00:11 +00:00
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model
* @since CakePHP(tm) v 0.2.9
2009-11-06 06:51:51 +00:00
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Load Model and AppModel
*/
App::uses('AppModel', 'Model');
/**
* ACL Node
*
*
* @package Cake.Model
*/
class AclNode extends AppModel {
/**
* Explicitly disable in-memory query caching for ACL models
*
* @var boolean
*/
public $cacheQueries = false;
/**
* ACL models use the Tree behavior
*
* @var array
*/
public $actsAs = array('Tree' => array('type' => 'nested'));
/**
* Constructor
*
*/
public function __construct() {
$config = Configure::read('Acl.database');
if (isset($config)) {
$this->useDbConfig = $config;
}
parent::__construct();
}
/**
* Retrieves the Aro/Aco node for this model
*
* @param mixed $ref Array with 'model' and 'foreign_key', model object, or string value
* @return array Node found in database
*/
public function node($ref = null) {
$db = $this->getDataSource();
$type = $this->alias;
$result = null;
if (!empty($this->useTable)) {
$table = $this->useTable;
} else {
$table = Inflector::pluralize(Inflector::underscore($type));
}
if (empty($ref)) {
return null;
} elseif (is_string($ref)) {
$path = explode('/', $ref);
$start = $path[0];
unset($path[0]);
$queryData = array(
'conditions' => array(
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
$db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
'joins' => array(array(
'table' => $table,
'alias' => "{$type}0",
'type' => 'LEFT',
'conditions' => array("{$type}0.alias" => $start)
)),
'order' => $db->name("{$type}.lft") . ' DESC'
);
foreach ($path as $i => $alias) {
$j = $i - 1;
$queryData['joins'][] = array(
'table' => $table,
'alias' => "{$type}{$i}",
'type' => 'LEFT',
'conditions' => array(
$db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft"),
$db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght"),
$db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string'),
$db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id")
)
);
$queryData['conditions'] = array('or' => array(
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
);
}
$result = $db->read($this, $queryData, -1);
$path = array_values($path);
if (
!isset($result[0][$type]) ||
(!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
(empty($path) && $result[0][$type]['alias'] != $start)
) {
return false;
}
} elseif (is_object($ref) && is_a($ref, 'Model')) {
$ref = array('model' => $ref->alias, 'foreign_key' => $ref->id);
} elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
$name = key($ref);
2010-07-06 02:19:22 +00:00
$model = ClassRegistry::init(array('class' => $name, 'alias' => $name));
if (empty($model)) {
trigger_error(__d('cake_dev', "Model class '%s' not found in AclNode::node() when trying to bind %s object", $type, $this->alias), E_USER_WARNING);
return null;
}
$tmpRef = null;
if (method_exists($model, 'bindNode')) {
$tmpRef = $model->bindNode($ref);
}
if (empty($tmpRef)) {
$ref = array('model' => $name, 'foreign_key' => $ref[$name][$model->primaryKey]);
} else {
if (is_string($tmpRef)) {
return $this->node($tmpRef);
}
$ref = $tmpRef;
}
}
if (is_array($ref)) {
if (is_array(current($ref)) && is_string(key($ref))) {
$name = key($ref);
$ref = current($ref);
}
foreach ($ref as $key => $val) {
if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
unset($ref[$key]);
$ref["{$type}0.{$key}"] = $val;
}
}
$queryData = array(
'conditions' => $ref,
'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
'joins' => array(array(
'table' => $table,
'alias' => "{$type}0",
'type' => 'LEFT',
'conditions' => array(
$db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
$db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
)
)),
'order' => $db->name("{$type}.lft") . ' DESC'
);
$result = $db->read($this, $queryData, -1);
if (!$result) {
trigger_error(__d('cake_dev', "AclNode::node() - Couldn't find %s node identified by \"%s\"", $type, print_r($ref, true)), E_USER_WARNING);
}
}
return $result;
}
}