mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 10:36:16 +00:00
Refactoring authorization objects to also use settings, it makes them consistent with authenticate objects.
Making actionPath automatically pass into authentication objects. Adding tests.
This commit is contained in:
parent
e34fdde918
commit
0c7f9149ca
3 changed files with 48 additions and 20 deletions
|
@ -91,6 +91,16 @@ class AuthComponent extends Component {
|
||||||
*/
|
*/
|
||||||
protected $_authorizeObjects = array();
|
protected $_authorizeObjects = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A hash mapping legacy properties => to settings passed into Authorize objects.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @deprecated Will be removed in 2.1+
|
||||||
|
*/
|
||||||
|
protected $_authorizeLegacyMap = array(
|
||||||
|
'actionPath' => 'actionPath',
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of an optional view element to render when an Ajax request is made
|
* The name of an optional view element to render when an Ajax request is made
|
||||||
* with an invalid or expired session
|
* with an invalid or expired session
|
||||||
|
@ -499,6 +509,11 @@ class AuthComponent extends Component {
|
||||||
if (!method_exists($className, 'authorize')) {
|
if (!method_exists($className, 'authorize')) {
|
||||||
throw new CakeException(__('Authorization objects must implement an authorize method.'));
|
throw new CakeException(__('Authorization objects must implement an authorize method.'));
|
||||||
}
|
}
|
||||||
|
foreach ($this->_authorizeLegacyMap as $old => $new) {
|
||||||
|
if (empty($settings[$new]) && !empty($this->{$old})) {
|
||||||
|
$settings[$new] = $this->{$old};
|
||||||
|
}
|
||||||
|
}
|
||||||
$this->_authorizeObjects[] = new $className($this->_Collection->getController(), $settings);
|
$this->_authorizeObjects[] = new $className($this->_Collection->getController(), $settings);
|
||||||
}
|
}
|
||||||
return $this->_authorizeObjects;
|
return $this->_authorizeObjects;
|
||||||
|
|
|
@ -29,26 +29,24 @@ abstract class BaseAuthorize {
|
||||||
protected $_controller = null;
|
protected $_controller = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The path to ACO nodes that contains the nodes for controllers. Used as a prefix
|
* Settings for authorize objects.
|
||||||
* when calling $this->action();
|
|
||||||
*
|
*
|
||||||
* @var string
|
* - `actionPath` - The path to ACO nodes that contains the nodes for controllers. Used as a prefix
|
||||||
*/
|
* when calling $this->action();
|
||||||
public $actionPath = null;
|
* - `actionMap` - Action -> crud mappings. Used by authorization objects that want to map actions to CRUD roles.
|
||||||
|
|
||||||
/**
|
|
||||||
* Action -> crud mappings. Used by authorization objects that want to map actions to CRUD roles.
|
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
* @see CrudAuthorize
|
|
||||||
*/
|
*/
|
||||||
protected $_actionMap = array(
|
public $settings = array(
|
||||||
|
'actionPath' => null,
|
||||||
|
'actionMap' => array(
|
||||||
'index' => 'read',
|
'index' => 'read',
|
||||||
'add' => 'create',
|
'add' => 'create',
|
||||||
'edit' => 'update',
|
'edit' => 'update',
|
||||||
'view' => 'read',
|
'view' => 'read',
|
||||||
'delete' => 'delete',
|
'delete' => 'delete',
|
||||||
'remove' => 'delete'
|
'remove' => 'delete'
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,6 +57,7 @@ abstract class BaseAuthorize {
|
||||||
*/
|
*/
|
||||||
public function __construct(Controller $controller, $settings = array()) {
|
public function __construct(Controller $controller, $settings = array()) {
|
||||||
$this->controller($controller);
|
$this->controller($controller);
|
||||||
|
$this->settings = Set::merge($this->settings, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -99,7 +98,7 @@ abstract class BaseAuthorize {
|
||||||
return str_replace(
|
return str_replace(
|
||||||
array(':controller', ':action', ':plugin/'),
|
array(':controller', ':action', ':plugin/'),
|
||||||
array(Inflector::camelize($request['controller']), $request['action'], $plugin),
|
array(Inflector::camelize($request['controller']), $request['action'], $plugin),
|
||||||
$this->actionPath . $path
|
$this->settings['actionPath'] . $path
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,16 +110,16 @@ abstract class BaseAuthorize {
|
||||||
*/
|
*/
|
||||||
public function mapActions($map = array()) {
|
public function mapActions($map = array()) {
|
||||||
if (empty($map)) {
|
if (empty($map)) {
|
||||||
return $this->_actionMap;
|
return $this->settings['actionMap'];
|
||||||
}
|
}
|
||||||
$crud = array('create', 'read', 'update', 'delete');
|
$crud = array('create', 'read', 'update', 'delete');
|
||||||
foreach ($map as $action => $type) {
|
foreach ($map as $action => $type) {
|
||||||
if (in_array($action, $crud) && is_array($type)) {
|
if (in_array($action, $crud) && is_array($type)) {
|
||||||
foreach ($type as $typedAction) {
|
foreach ($type as $typedAction) {
|
||||||
$this->_actionMap[$typedAction] = $action;
|
$this->settings['actionMap'][$typedAction] = $action;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->_actionMap[$action] = $type;
|
$this->settings['actionMap'][$action] = $type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -775,6 +775,20 @@ class AuthTest extends CakeTestCase {
|
||||||
$this->Controller->Auth->identify($this->Controller->request);
|
$this->Controller->Auth->identify($this->Controller->request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that loadAuthorize merges in legacy authorize settings.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testLoadAuthorizeSettingsPass() {
|
||||||
|
$this->Controller->Auth->actionPath = 'controllers/';
|
||||||
|
|
||||||
|
$this->Controller->Auth->authorize = array('Actions');
|
||||||
|
$objects = $this->Controller->Auth->loadAuthorizeObjects();
|
||||||
|
$result = $objects[0];
|
||||||
|
$this->assertEquals($result->settings['actionPath'], 'controllers/');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test that loadAuthorize resets the loaded objects each time.
|
* test that loadAuthorize resets the loaded objects each time.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue