2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-02-10 22:38:40 +00:00
|
|
|
App::uses('Model', 'Model');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* ACL Node
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-10 22:38:40 +00:00
|
|
|
class AclNode extends Model {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Explicitly disable in-memory query caching for ACL models
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $cacheQueries = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* ACL models use the Tree behavior
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-10-15 18:52:41 +00:00
|
|
|
public $actsAs = array('Tree' => array('type' => 'nested'));
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
2016-11-22 01:51:12 +00:00
|
|
|
*
|
|
|
|
* @param bool|int|string|array $id Set this ID for this model on startup,
|
|
|
|
* can also be an array of options, see above.
|
|
|
|
* @param string $table Name of database table to use.
|
|
|
|
* @param string $ds DataSource connection name.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2016-11-20 02:30:18 +00:00
|
|
|
public function __construct($id = false, $table = null, $ds = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$config = Configure::read('Acl.database');
|
2008-10-23 00:10:44 +00:00
|
|
|
if (isset($config)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->useDbConfig = $config;
|
|
|
|
}
|
2016-11-20 02:30:18 +00:00
|
|
|
parent::__construct($id, $table, $ds);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the Aro/Aco node for this model
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Node found in database
|
2012-03-04 19:18:04 +00:00
|
|
|
* @throws CakeException when binding to a model that doesn't exist.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function node($ref = null) {
|
2011-02-23 05:52:23 +00:00
|
|
|
$db = $this->getDataSource();
|
2008-05-30 11:40:08 +00:00
|
|
|
$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(
|
2011-10-07 00:34:23 +00:00
|
|
|
'table' => $table,
|
2008-05-30 11:40:08 +00:00
|
|
|
'alias' => "{$type}0",
|
2013-08-07 16:50:02 +00:00
|
|
|
'type' => 'INNER',
|
2008-05-30 11:40:08 +00:00
|
|
|
'conditions' => array("{$type}0.alias" => $start)
|
|
|
|
)),
|
|
|
|
'order' => $db->name("{$type}.lft") . ' DESC'
|
|
|
|
);
|
2014-06-28 02:58:34 +00:00
|
|
|
|
|
|
|
$conditionsAfterJoin = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
foreach ($path as $i => $alias) {
|
|
|
|
$j = $i - 1;
|
|
|
|
|
|
|
|
$queryData['joins'][] = array(
|
2011-10-07 00:34:23 +00:00
|
|
|
'table' => $table,
|
2008-05-30 11:40:08 +00:00
|
|
|
'alias' => "{$type}{$i}",
|
2013-08-07 16:50:02 +00:00
|
|
|
'type' => 'INNER',
|
2008-05-30 11:40:08 +00:00
|
|
|
'conditions' => array(
|
2016-12-29 04:18:30 +00:00
|
|
|
"{$type}{$i}.alias" => $alias
|
2008-05-30 11:40:08 +00:00
|
|
|
)
|
|
|
|
);
|
2014-06-28 02:58:34 +00:00
|
|
|
|
|
|
|
// it will be better if this conditions will performs after join operation
|
|
|
|
$conditionsAfterJoin[] = $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id");
|
|
|
|
$conditionsAfterJoin[] = $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght");
|
|
|
|
$conditionsAfterJoin[] = $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft");
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$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"))
|
|
|
|
);
|
|
|
|
}
|
2014-06-28 02:58:34 +00:00
|
|
|
$queryData['conditions'] = array_merge($queryData['conditions'], $conditionsAfterJoin);
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $db->read($this, $queryData, -1);
|
|
|
|
$path = array_values($path);
|
|
|
|
|
2014-12-09 02:17:35 +00:00
|
|
|
if (!isset($result[0][$type]) ||
|
2008-05-30 11:40:08 +00:00
|
|
|
(!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
|
|
|
|
(empty($path) && $result[0][$type]['alias'] != $start)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-24 20:57:51 +00:00
|
|
|
} elseif (is_object($ref) && $ref instanceof Model) {
|
2012-01-11 10:39:06 +00:00
|
|
|
$ref = array('model' => $ref->name, 'foreign_key' => $ref->id);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
|
|
|
|
$name = key($ref);
|
2012-12-20 12:47:03 +00:00
|
|
|
list(, $alias) = pluginSplit($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-01-11 10:39:06 +00:00
|
|
|
$model = ClassRegistry::init(array('class' => $name, 'alias' => $alias));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (empty($model)) {
|
2012-02-20 14:41:57 +00:00
|
|
|
throw new CakeException('cake_dev', "Model class '%s' not found in AclNode::node() when trying to bind %s object", $type, $this->alias);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$tmpRef = null;
|
|
|
|
if (method_exists($model, 'bindNode')) {
|
|
|
|
$tmpRef = $model->bindNode($ref);
|
|
|
|
}
|
|
|
|
if (empty($tmpRef)) {
|
2012-01-11 10:39:06 +00:00
|
|
|
$ref = array('model' => $alias, 'foreign_key' => $ref[$name][$model->primaryKey]);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
if (is_string($tmpRef)) {
|
|
|
|
return $this->node($tmpRef);
|
|
|
|
}
|
|
|
|
$ref = $tmpRef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_array($ref)) {
|
2008-05-31 12:36:38 +00:00
|
|
|
if (is_array(current($ref)) && is_string(key($ref))) {
|
|
|
|
$name = key($ref);
|
|
|
|
$ref = current($ref);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($ref as $key => $val) {
|
2008-05-31 12:36:38 +00:00
|
|
|
if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($ref[$key]);
|
|
|
|
$ref["{$type}0.{$key}"] = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$queryData = array(
|
2008-05-31 12:36:38 +00:00
|
|
|
'conditions' => $ref,
|
2008-05-30 11:40:08 +00:00
|
|
|
'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
|
|
|
|
'joins' => array(array(
|
2011-10-07 00:34:23 +00:00
|
|
|
'table' => $table,
|
2008-05-30 11:40:08 +00:00
|
|
|
'alias' => "{$type}0",
|
2013-08-13 17:40:59 +00:00
|
|
|
'type' => 'INNER',
|
2008-05-30 11:40:08 +00:00
|
|
|
'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) {
|
2012-02-20 14:41:57 +00:00
|
|
|
throw new CakeException(__d('cake_dev', "AclNode::node() - Couldn't find %s node identified by \"%s\"", $type, print_r($ref, true)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|