2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Access Control List factory class.
|
|
|
|
*
|
|
|
|
* Permissions system.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
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)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* 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
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-22 04:35:46 +00:00
|
|
|
App::uses('Component', 'Controller');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Access Control List factory class.
|
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* Uses a strategy pattern to allow custom ACL implementations to be used with the same component interface.
|
|
|
|
* You can define by changing `Configure::write('Acl.classname', 'DbAcl');` in your core.php. Concrete ACL
|
|
|
|
* implementations should extend `AclBase` and implement the methods it defines.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-04 19:12:42 +00:00
|
|
|
class AclComponent extends Component {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Instance of an ACL class
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @var AclInterface
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_Instance = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-04 22:41:08 +00:00
|
|
|
/**
|
|
|
|
* Aro object.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $Aro;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Aco object
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $Aco;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:17:53 +00:00
|
|
|
* Constructor. Will return an instance of the correct ACL class as defined in `Configure::read('Acl.classname')`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-29 04:06:43 +00:00
|
|
|
* @param ComponentCollection $collection
|
|
|
|
* @param array $settings
|
2010-12-12 00:01:07 +00:00
|
|
|
* @throws CakeException when Acl.classname could not be loaded.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-04 21:09:44 +00:00
|
|
|
public function __construct(ComponentCollection $collection, $settings = array()) {
|
|
|
|
parent::__construct($collection, $settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
$name = Inflector::camelize(strtolower(Configure::read('Acl.classname')));
|
|
|
|
if (!class_exists($name)) {
|
|
|
|
if (App::import('Component', $name)) {
|
2009-11-16 00:55:20 +00:00
|
|
|
list($plugin, $name) = pluginSplit($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
$name .= 'Component';
|
|
|
|
} else {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'Could not find %s.', $name));
|
2010-04-24 03:03:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->adapter($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets or gets the Adapter object currently in the AclComponent.
|
|
|
|
*
|
|
|
|
* `$this->Acl->adapter();` will get the current adapter class while
|
|
|
|
* `$this->Acl->adapter($obj);` will set the adapter class
|
|
|
|
*
|
|
|
|
* Will call the initialize method on the adapter if setting a new one.
|
|
|
|
*
|
|
|
|
* @param mixed $adapter Instance of AclBase or a string name of the class to use. (optional)
|
|
|
|
* @return mixed either null, or instance of AclBase
|
2010-12-12 00:01:07 +00:00
|
|
|
* @throws CakeException when the given class is not an AclBase
|
2010-04-24 03:03:51 +00:00
|
|
|
*/
|
|
|
|
public function adapter($adapter = null) {
|
|
|
|
if ($adapter) {
|
|
|
|
if (is_string($adapter)) {
|
|
|
|
$adapter = new $adapter();
|
|
|
|
}
|
2010-04-24 03:52:36 +00:00
|
|
|
if (!$adapter instanceof AclInterface) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'AclComponent adapters must implement AclInterface'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-24 03:03:51 +00:00
|
|
|
$this->_Instance = $adapter;
|
|
|
|
$this->_Instance->initialize($this);
|
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-04-24 03:03:51 +00:00
|
|
|
return $this->_Instance;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:17:53 +00:00
|
|
|
* Pass-thru function for ACL check instance. Check methods
|
|
|
|
* are used to check whether or not an ARO can access an ACO
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function check($aro, $aco, $action = "*") {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->_Instance->check($aro, $aco, $action);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:17:53 +00:00
|
|
|
* Pass-thru function for ACL allow instance. Allow methods
|
|
|
|
* are used to grant an ARO access to an ACO.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function allow($aro, $aco, $action = "*") {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->_Instance->allow($aro, $aco, $action);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:17:53 +00:00
|
|
|
* Pass-thru function for ACL deny instance. Deny methods
|
|
|
|
* are used to remove permission from an ARO to access an ACO.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function deny($aro, $aco, $action = "*") {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->_Instance->deny($aro, $aco, $action);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:17:53 +00:00
|
|
|
* Pass-thru function for ACL inherit instance. Inherit methods
|
|
|
|
* modify the permission for an ARO to be that of its parent object.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function inherit($aro, $aco, $action = "*") {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->_Instance->inherit($aro, $aco, $action);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:17:53 +00:00
|
|
|
* Pass-thru function for ACL grant instance. An alias for AclComponent::allow()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
|
|
|
* @return boolean Success
|
2010-09-09 02:49:00 +00:00
|
|
|
* @deprecated
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function grant($aro, $aco, $action = "*") {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'AclComponent::grant() is deprecated, use allow() instead'), E_USER_WARNING);
|
2010-09-09 02:48:21 +00:00
|
|
|
return $this->_Instance->allow($aro, $aco, $action);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:17:53 +00:00
|
|
|
* Pass-thru function for ACL grant instance. An alias for AclComponent::deny()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
|
|
|
* @return boolean Success
|
2010-09-09 02:49:00 +00:00
|
|
|
* @deprecated
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function revoke($aro, $aco, $action = "*") {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'AclComponent::revoke() is deprecated, use deny() instead'), E_USER_WARNING);
|
2010-09-09 02:48:21 +00:00
|
|
|
return $this->_Instance->deny($aro, $aco, $action);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-04-24 03:52:36 +00:00
|
|
|
* Access Control List interface.
|
|
|
|
* Implementing classes are used by AclComponent to perform ACL checks in Cake.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 03:52:36 +00:00
|
|
|
interface AclInterface {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Empty method to be overridden in subclasses
|
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
|
|
|
*/
|
2010-04-24 03:52:36 +00:00
|
|
|
public function check($aro, $aco, $action = "*");
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-04-24 03:52:36 +00:00
|
|
|
* Allow methods are used to grant an ARO access to an 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
|
|
|
|
*/
|
|
|
|
public function allow($aro, $aco, $action = "*");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deny methods are used to remove permission from an ARO to access an 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
|
|
|
|
*/
|
|
|
|
public function deny($aro, $aco, $action = "*");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inherit methods modify the permission for an ARO to be that of its parent object.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-04-24 03:52:36 +00:00
|
|
|
* @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
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 03:52:36 +00:00
|
|
|
public function inherit($aro, $aco, $action = "*");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization method for the Acl implementation
|
|
|
|
*
|
|
|
|
* @param AclComponent $component
|
|
|
|
*/
|
|
|
|
public function initialize($component);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-04-17 11:13:02 +00:00
|
|
|
* DbAcl implements an ACL control system in the database. ARO's and ACO's are
|
|
|
|
* structured into trees and a linking table is used to define permissions. You
|
2010-03-28 16:17:53 +00:00
|
|
|
* can install the schema for DbAcl with the Schema Shell.
|
|
|
|
*
|
|
|
|
* `$aco` and `$aro` parameters can be slash delimited paths to tree nodes.
|
|
|
|
*
|
|
|
|
* eg. `controllers/Users/edit`
|
|
|
|
*
|
|
|
|
* Would point to a tree structure like
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* controllers
|
|
|
|
* Users
|
|
|
|
* edit
|
|
|
|
* }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 03:52:36 +00:00
|
|
|
class DbAcl extends Object implements AclInterface {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
*/
|
2011-08-19 02:16:16 +00:00
|
|
|
public function __construct() {
|
2008-05-30 11:40:08 +00:00
|
|
|
parent::__construct();
|
2011-01-28 06:36:30 +00:00
|
|
|
App::uses('AclNode', 'Model');
|
2010-05-23 00:20:41 +00:00
|
|
|
$this->Aro = ClassRegistry::init(array('class' => 'Aro', 'alias' => 'Aro'));
|
|
|
|
$this->Aco = ClassRegistry::init(array('class' => 'Aco', 'alias' => 'Aco'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:17:53 +00:00
|
|
|
* Initializes the containing component and sets the Aro/Aco objects to it.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param AclComponent $component
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 03:03:51 +00:00
|
|
|
public function initialize($component) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$component->Aro = $this->Aro;
|
|
|
|
$component->Aco = $this->Aco;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Checks if the given $aro has access to action $action in $aco
|
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
|
|
|
* @return boolean Success (true if ARO has access to action in ACO, false otherwise)
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html#checking-permissions-the-acl-component
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function check($aro, $aco, $action = "*") {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($aro == null || $aco == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
|
|
|
|
$aroPath = $this->Aro->node($aro);
|
2008-07-31 23:50:32 +00:00
|
|
|
$acoPath = $this->Aco->node($aco);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-07-31 23:50:32 +00:00
|
|
|
if (empty($aroPath) || empty($acoPath)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', "DbAcl::check() - Failed ARO/ACO node lookup in permissions check. Node references:\nAro: ") . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-07-31 23:50:32 +00:00
|
|
|
|
|
|
|
if ($acoPath == null || $acoPath == array()) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', "DbAcl::check() - Failed ACO node lookup in permissions check. Node references:\nAro: ") . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($action != '*' && !in_array('_' . $action, $permKeys)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', "ACO permissions key %s does not exist in DbAcl::check()", $action), E_USER_NOTICE);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$inherited = array();
|
2008-07-31 23:50:32 +00:00
|
|
|
$acoIDs = Set::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-09-18 03:09:19 +00:00
|
|
|
$count = count($aroPath);
|
|
|
|
for ($i = 0 ; $i < $count; $i++) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$permAlias = $this->Aro->Permission->alias;
|
|
|
|
|
|
|
|
$perms = $this->Aro->Permission->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;
|
|
|
|
} else {
|
|
|
|
$perms = Set::extract($perms, '{n}.' . $this->Aro->Permission->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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($inherited) === count($permKeys)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2008-10-23 00:10:44 +00:00
|
|
|
switch ($perm['_' . $action]) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case -1:
|
|
|
|
return false;
|
|
|
|
case 0:
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Allow $aro to have access to action $actions in $aco
|
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $actions Action (defaults to *)
|
|
|
|
* @param integer $value Value to indicate access type (1 to give access, -1 to deny, 0 to inherit)
|
|
|
|
* @return boolean Success
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html#assigning-permissions
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function allow($aro, $aco, $actions = "*", $value = 1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$perms = $this->getAclLink($aro, $aco);
|
|
|
|
$permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
|
|
|
|
$save = array();
|
|
|
|
|
|
|
|
if ($perms == false) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'DbAcl::allow() - Invalid node'), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isset($perms[0])) {
|
|
|
|
$save = $perms[0][$this->Aro->Permission->alias];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($actions == "*") {
|
|
|
|
$permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
|
|
|
|
$save = array_combine($permKeys, array_pad(array(), count($permKeys), $value));
|
|
|
|
} else {
|
|
|
|
if (!is_array($actions)) {
|
|
|
|
$actions = array('_' . $actions);
|
|
|
|
}
|
|
|
|
if (is_array($actions)) {
|
|
|
|
foreach ($actions as $action) {
|
|
|
|
if ($action{0} != '_') {
|
|
|
|
$action = '_' . $action;
|
|
|
|
}
|
|
|
|
if (in_array($action, $permKeys)) {
|
|
|
|
$save[$action] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list($save['aro_id'], $save['aco_id']) = array($perms['aro'], $perms['aco']);
|
|
|
|
|
2009-11-21 20:14:21 +00:00
|
|
|
if ($perms['link'] != null && !empty($perms['link'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$save['id'] = $perms['link'][0][$this->Aro->Permission->alias]['id'];
|
|
|
|
} else {
|
|
|
|
unset($save['id']);
|
2008-05-31 04:31:30 +00:00
|
|
|
$this->Aro->Permission->id = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return ($this->Aro->Permission->save($save) !== false);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Deny access for $aro to action $action in $aco
|
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2011-07-30 20:48:37 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean Success
|
2011-10-15 15:13:26 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html#assigning-permissions
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function deny($aro, $aco, $action = "*") {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->allow($aro, $aco, $action, -1);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Let access for $aro to action $action in $aco be inherited
|
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2011-07-30 20:48:37 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function inherit($aro, $aco, $action = "*") {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->allow($aro, $aco, $action, 0);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Allow $aro to have access to action $actions in $aco
|
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2011-07-30 20:48:37 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean Success
|
|
|
|
* @see allow()
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function grant($aro, $aco, $action = "*") {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->allow($aro, $aco, $action);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Deny access for $aro to action $action in $aco
|
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2011-07-30 20:48:37 +00:00
|
|
|
* @param string $action Action (defaults to *)
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean Success
|
|
|
|
* @see deny()
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function revoke($aro, $aco, $action = "*") {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->deny($aro, $aco, $action);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get an array of access-control links between the given Aro and Aco
|
|
|
|
*
|
2010-03-28 16:17:53 +00:00
|
|
|
* @param string $aro ARO The requesting object identifier.
|
|
|
|
* @param string $aco ACO The controlled object identifier.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array Indexed array with: 'aro', 'aco' and 'link'
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function getAclLink($aro, $aco) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$obj = array();
|
|
|
|
$obj['Aro'] = $this->Aro->node($aro);
|
|
|
|
$obj['Aco'] = $this->Aco->node($aco);
|
|
|
|
|
|
|
|
if (empty($obj['Aro']) || empty($obj['Aco'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
2011-12-16 06:52:07 +00:00
|
|
|
'aro' => Set::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id'),
|
|
|
|
'aco' => Set::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id'),
|
2008-05-30 11:40:08 +00:00
|
|
|
'link' => $this->Aro->Permission->find('all', array('conditions' => array(
|
2011-12-16 06:52:07 +00:00
|
|
|
$this->Aro->Permission->alias . '.aro_id' => Set::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id'),
|
|
|
|
$this->Aro->Permission->alias . '.aco_id' => Set::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id')
|
2008-05-30 11:40:08 +00:00
|
|
|
)))
|
|
|
|
);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get the keys used in an ACO
|
|
|
|
*
|
|
|
|
* @param array $keys Permission model info
|
|
|
|
* @return array ACO keys
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _getAcoKeys($keys) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$newKeys = array();
|
|
|
|
$keys = array_keys($keys);
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
if (!in_array($key, array('id', 'aro_id', 'aco_id'))) {
|
|
|
|
$newKeys[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $newKeys;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-04-17 11:13:02 +00:00
|
|
|
* IniAcl implements an access control system using an INI file. An example
|
2010-03-28 16:20:02 +00:00
|
|
|
* of the ini file used can be found in /config/acl.ini.php.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 03:52:36 +00:00
|
|
|
class IniAcl extends Object implements AclInterface {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Array with configuration, parsed from ini file
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $config = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-11-29 02:22:10 +00:00
|
|
|
/**
|
|
|
|
* The Set::classicExtract() path to the user/aro identifier in the
|
|
|
|
* acl.ini file. This path will be used to extract the string
|
|
|
|
* representation of a user used in the ini file.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $userPath = 'User.username';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-04-24 03:03:51 +00:00
|
|
|
* Initialize method
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-04-17 11:13:02 +00:00
|
|
|
* @param AclBase $component
|
2010-04-24 03:03:51 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 03:03:51 +00:00
|
|
|
public function initialize($component) {
|
2011-04-17 11:13:02 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-04-24 03:52:36 +00:00
|
|
|
/**
|
|
|
|
* No op method, allow cannot be done with IniAcl
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function allow($aro, $aco, $action = "*") {
|
2011-04-17 11:13:02 +00:00
|
|
|
|
2010-04-24 03:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No op method, deny cannot be done with IniAcl
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function deny($aro, $aco, $action = "*") {
|
2011-04-17 11:13:02 +00:00
|
|
|
|
2010-04-24 03:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No op method, inherit cannot be done with IniAcl
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function inherit($aro, $aco, $action = "*") {
|
2011-04-17 11:13:02 +00:00
|
|
|
|
2010-04-24 03:52:36 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-04-17 11:13:02 +00:00
|
|
|
* Main ACL check function. Checks to see if the ARO (access request object) has access to the
|
|
|
|
* ACO (access control object).Looks at the acl.ini.php file for permissions
|
2010-03-28 16:20:02 +00:00
|
|
|
* (see instructions in /config/acl.ini.php).
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $aro ARO
|
|
|
|
* @param string $aco ACO
|
|
|
|
* @param string $aco_action Action
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function check($aro, $aco, $aco_action = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->config == null) {
|
2011-04-17 11:13:02 +00:00
|
|
|
$this->config = $this->readConfigFile(APP . 'Config' . DS . 'acl.ini.php');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$aclConfig = $this->config;
|
2011-04-17 11:13:02 +00:00
|
|
|
|
2010-11-29 02:22:10 +00:00
|
|
|
if (is_array($aro)) {
|
|
|
|
$aro = Set::classicExtract($aro, $this->userPath);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (isset($aclConfig[$aro]['deny'])) {
|
|
|
|
$userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));
|
|
|
|
|
|
|
|
if (array_search($aco, $userDenies)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($aclConfig[$aro]['allow'])) {
|
|
|
|
$userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));
|
|
|
|
|
|
|
|
if (array_search($aco, $userAllows)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($aclConfig[$aro]['groups'])) {
|
|
|
|
$userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));
|
|
|
|
|
|
|
|
foreach ($userGroups as $group) {
|
|
|
|
if (array_key_exists($group, $aclConfig)) {
|
|
|
|
if (isset($aclConfig[$group]['deny'])) {
|
2010-11-28 05:40:41 +00:00
|
|
|
$groupDenies = $this->arrayTrim(explode(",", $aclConfig[$group]['deny']));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (array_search($aco, $groupDenies)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($aclConfig[$group]['allow'])) {
|
|
|
|
$groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));
|
|
|
|
|
|
|
|
if (array_search($aco, $groupAllows)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Parses an INI file and returns an array that reflects the INI file's section structure. Double-quote friendly.
|
|
|
|
*
|
2010-11-29 01:46:04 +00:00
|
|
|
* @param string $filename File
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array INI section structure
|
|
|
|
*/
|
2010-11-29 01:46:04 +00:00
|
|
|
public function readConfigFile($filename) {
|
2010-12-09 03:45:18 +00:00
|
|
|
App::uses('IniReader', 'Configure');
|
2010-12-04 19:47:47 +00:00
|
|
|
$iniFile = new IniReader(dirname($filename) . DS);
|
|
|
|
return $iniFile->read(basename($filename));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Removes trailing spaces on all array elements (to prepare for searching)
|
|
|
|
*
|
|
|
|
* @param array $array Array to trim
|
|
|
|
* @return array Trimmed array
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function arrayTrim($array) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$array[$key] = trim($value);
|
|
|
|
}
|
|
|
|
array_unshift($array, "");
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
}
|