2010-12-22 04:31:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-12-22 04:31:03 +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
|
2010-12-22 04:31:03 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-12-22 04:31:03 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model
|
2010-12-22 04:31:03 +00:00
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2010-12-22 04:31:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permissions linking AROs with ACOs
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Model
|
2010-12-22 04:31:03 +00:00
|
|
|
*/
|
|
|
|
class Permission extends AppModel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Explicitly disable in-memory query caching
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $cacheQueries = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override default table name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $useTable = 'aros_acos';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permissions link AROs with ACOs
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $belongsTo = array('Aro', 'Aco');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No behaviors for this model
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $actsAs = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor, used to tell this model to use the
|
|
|
|
* database configured for ACL
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function __construct() {
|
2010-12-22 04:31:03 +00:00
|
|
|
$config = Configure::read('Acl.database');
|
|
|
|
if (!empty($config)) {
|
|
|
|
$this->useDbConfig = $config;
|
|
|
|
}
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
|
2012-02-21 14:21:15 +00:00
|
|
|
/**
|
|
|
|
* Checks if the given $aro has access to action $action in $aco
|
|
|
|
*
|
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
|
|
|
* @param string $action Action (defaults to *)
|
|
|
|
* @return boolean Success (true if ARO has access to action in ACO, false otherwise)
|
|
|
|
*/
|
2013-08-20 22:05:53 +00:00
|
|
|
public function check($aro, $aco, $action = '*') {
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$aro || !$aco) {
|
2012-02-21 14:21:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$permKeys = $this->getAcoKeys($this->schema());
|
|
|
|
$aroPath = $this->Aro->node($aro);
|
|
|
|
$acoPath = $this->Aco->node($aco);
|
|
|
|
|
2014-02-05 18:25:14 +00:00
|
|
|
if (!$aroPath) {
|
2013-08-20 15:48:56 +00:00
|
|
|
trigger_error(__d('cake_dev',
|
2014-02-05 18:25:14 +00:00
|
|
|
"%s - Failed ARO node lookup in permissions check. Node references:\nAro: %s\nAco: %s",
|
2013-08-20 22:05:53 +00:00
|
|
|
'DbAcl::check()',
|
2013-08-20 15:48:56 +00:00
|
|
|
print_r($aro, true),
|
|
|
|
print_r($aco, true)),
|
|
|
|
E_USER_WARNING
|
|
|
|
);
|
2012-02-21 14:21:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$acoPath) {
|
2013-08-20 15:48:56 +00:00
|
|
|
trigger_error(__d('cake_dev',
|
2013-08-20 22:05:53 +00:00
|
|
|
"%s - Failed ACO node lookup in permissions check. Node references:\nAro: %s\nAco: %s",
|
|
|
|
'DbAcl::check()',
|
2013-08-20 15:48:56 +00:00
|
|
|
print_r($aro, true),
|
|
|
|
print_r($aco, true)),
|
|
|
|
E_USER_WARNING
|
|
|
|
);
|
2012-02-21 14:21:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($action !== '*' && !in_array('_' . $action, $permKeys)) {
|
2013-08-20 22:05:53 +00:00
|
|
|
trigger_error(__d('cake_dev', "ACO permissions key %s does not exist in %s", $action, 'DbAcl::check()'), E_USER_NOTICE);
|
2012-02-21 14:21:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$inherited = array();
|
2012-04-04 23:33:57 +00:00
|
|
|
$acoIDs = Hash::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
|
2012-02-21 14:21:15 +00:00
|
|
|
|
|
|
|
$count = count($aroPath);
|
2012-04-03 03:23:53 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2012-02-21 14:21:15 +00:00
|
|
|
$permAlias = $this->alias;
|
|
|
|
|
|
|
|
$perms = $this->find('all', array(
|
|
|
|
'conditions' => array(
|
|
|
|
"{$permAlias}.aro_id" => $aroPath[$i][$this->Aro->alias]['id'],
|
|
|
|
"{$permAlias}.aco_id" => $acoIDs
|
|
|
|
),
|
|
|
|
'order' => array($this->Aco->alias . '.lft' => 'desc'),
|
|
|
|
'recursive' => 0
|
|
|
|
));
|
|
|
|
|
|
|
|
if (empty($perms)) {
|
|
|
|
continue;
|
2013-07-02 23:14:41 +00:00
|
|
|
}
|
|
|
|
$perms = Hash::extract($perms, '{n}.' . $this->alias);
|
|
|
|
foreach ($perms as $perm) {
|
|
|
|
if ($action === '*') {
|
|
|
|
|
|
|
|
foreach ($permKeys as $key) {
|
|
|
|
if (!empty($perm)) {
|
|
|
|
if ($perm[$key] == -1) {
|
|
|
|
return false;
|
|
|
|
} elseif ($perm[$key] == 1) {
|
|
|
|
$inherited[$key] = 1;
|
2012-02-21 14:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 23:14:41 +00:00
|
|
|
}
|
2012-02-21 14:21:15 +00:00
|
|
|
|
2013-07-02 23:14:41 +00:00
|
|
|
if (count($inherited) === count($permKeys)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch ($perm['_' . $action]) {
|
|
|
|
case -1:
|
|
|
|
return false;
|
|
|
|
case 0:
|
|
|
|
continue;
|
|
|
|
case 1:
|
2012-02-21 14:21:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow $aro to have access to action $actions in $aco
|
|
|
|
*
|
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2013-05-25 01:47:10 +00:00
|
|
|
* @param string $actions Action (defaults to *) Invalid permissions will result in an exception
|
2012-02-21 14:21:15 +00:00
|
|
|
* @param integer $value Value to indicate access type (1 to give access, -1 to deny, 0 to inherit)
|
|
|
|
* @return boolean Success
|
2013-05-25 01:47:10 +00:00
|
|
|
* @throws AclException on Invalid permission key.
|
2012-02-21 14:21:15 +00:00
|
|
|
*/
|
2013-08-20 22:05:53 +00:00
|
|
|
public function allow($aro, $aco, $actions = '*', $value = 1) {
|
2012-02-21 14:21:15 +00:00
|
|
|
$perms = $this->getAclLink($aro, $aco);
|
|
|
|
$permKeys = $this->getAcoKeys($this->schema());
|
|
|
|
$save = array();
|
|
|
|
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$perms) {
|
2013-08-20 22:05:53 +00:00
|
|
|
trigger_error(__d('cake_dev', '%s - Invalid node', 'DbAcl::allow()'), E_USER_WARNING);
|
2012-02-21 14:21:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isset($perms[0])) {
|
|
|
|
$save = $perms[0][$this->alias];
|
|
|
|
}
|
|
|
|
|
2013-08-20 22:05:53 +00:00
|
|
|
if ($actions === '*') {
|
2012-02-21 14:21:15 +00:00
|
|
|
$save = array_combine($permKeys, array_pad(array(), count($permKeys), $value));
|
|
|
|
} else {
|
|
|
|
if (!is_array($actions)) {
|
|
|
|
$actions = array('_' . $actions);
|
|
|
|
}
|
2013-05-25 01:47:10 +00:00
|
|
|
foreach ($actions as $action) {
|
|
|
|
if ($action{0} !== '_') {
|
|
|
|
$action = '_' . $action;
|
|
|
|
}
|
|
|
|
if (!in_array($action, $permKeys, true)) {
|
|
|
|
throw new AclException(__d('cake_dev', 'Invalid permission key "%s"', $action));
|
2012-02-21 14:21:15 +00:00
|
|
|
}
|
2013-05-25 01:47:10 +00:00
|
|
|
$save[$action] = $value;
|
2012-02-21 14:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
list($save['aro_id'], $save['aco_id']) = array($perms['aro'], $perms['aco']);
|
|
|
|
|
2012-09-21 22:30:43 +00:00
|
|
|
if ($perms['link'] && !empty($perms['link'])) {
|
2012-02-21 14:21:15 +00:00
|
|
|
$save['id'] = $perms['link'][0][$this->alias]['id'];
|
|
|
|
} else {
|
|
|
|
unset($save['id']);
|
|
|
|
$this->id = null;
|
|
|
|
}
|
|
|
|
return ($this->save($save) !== false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of access-control links between the given Aro and Aco
|
|
|
|
*
|
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
|
|
|
* @return array Indexed array with: 'aro', 'aco' and 'link'
|
|
|
|
*/
|
|
|
|
public function getAclLink($aro, $aco) {
|
|
|
|
$obj = array();
|
|
|
|
$obj['Aro'] = $this->Aro->node($aro);
|
|
|
|
$obj['Aco'] = $this->Aco->node($aco);
|
|
|
|
|
|
|
|
if (empty($obj['Aro']) || empty($obj['Aco'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-07 18:42:25 +00:00
|
|
|
$aro = Hash::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id');
|
|
|
|
$aco = Hash::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id');
|
|
|
|
$aro = current($aro);
|
|
|
|
$aco = current($aco);
|
2012-02-21 14:21:15 +00:00
|
|
|
|
|
|
|
return array(
|
2012-04-07 18:42:25 +00:00
|
|
|
'aro' => $aro,
|
|
|
|
'aco' => $aco,
|
2012-02-21 14:21:15 +00:00
|
|
|
'link' => $this->find('all', array('conditions' => array(
|
2012-04-07 18:42:25 +00:00
|
|
|
$this->alias . '.aro_id' => $aro,
|
|
|
|
$this->alias . '.aco_id' => $aco
|
2012-02-21 14:21:15 +00:00
|
|
|
)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the crud type keys
|
|
|
|
*
|
|
|
|
* @param array $keys Permission schema
|
|
|
|
* @return array permission keys
|
|
|
|
*/
|
|
|
|
public function getAcoKeys($keys) {
|
|
|
|
$newKeys = array();
|
|
|
|
$keys = array_keys($keys);
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
if (!in_array($key, array('id', 'aro_id', 'aco_id'))) {
|
|
|
|
$newKeys[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $newKeys;
|
|
|
|
}
|
2012-03-04 19:18:04 +00:00
|
|
|
}
|