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:
mark_story 2011-01-05 00:01:40 -05:00
parent e34fdde918
commit 0c7f9149ca
3 changed files with 48 additions and 20 deletions

View file

@ -91,6 +91,16 @@ class AuthComponent extends Component {
*/
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
* with an invalid or expired session
@ -499,6 +509,11 @@ class AuthComponent extends Component {
if (!method_exists($className, 'authorize')) {
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);
}
return $this->_authorizeObjects;

View file

@ -29,26 +29,24 @@ abstract class BaseAuthorize {
protected $_controller = null;
/**
* The path to ACO nodes that contains the nodes for controllers. Used as a prefix
* when calling $this->action();
* Settings for authorize objects.
*
* @var string
*/
public $actionPath = null;
/**
* Action -> crud mappings. Used by authorization objects that want to map actions to CRUD roles.
* - `actionPath` - The path to ACO nodes that contains the nodes for controllers. Used as a prefix
* when calling $this->action();
* - `actionMap` - Action -> crud mappings. Used by authorization objects that want to map actions to CRUD roles.
*
* @var array
* @see CrudAuthorize
*/
protected $_actionMap = array(
'index' => 'read',
'add' => 'create',
'edit' => 'update',
'view' => 'read',
'delete' => 'delete',
'remove' => 'delete'
public $settings = array(
'actionPath' => null,
'actionMap' => array(
'index' => 'read',
'add' => 'create',
'edit' => 'update',
'view' => 'read',
'delete' => 'delete',
'remove' => 'delete'
)
);
/**
@ -59,6 +57,7 @@ abstract class BaseAuthorize {
*/
public function __construct(Controller $controller, $settings = array()) {
$this->controller($controller);
$this->settings = Set::merge($this->settings, $settings);
}
/**
@ -99,7 +98,7 @@ abstract class BaseAuthorize {
return str_replace(
array(':controller', ':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()) {
if (empty($map)) {
return $this->_actionMap;
return $this->settings['actionMap'];
}
$crud = array('create', 'read', 'update', 'delete');
foreach ($map as $action => $type) {
if (in_array($action, $crud) && is_array($type)) {
foreach ($type as $typedAction) {
$this->_actionMap[$typedAction] = $action;
$this->settings['actionMap'][$typedAction] = $action;
}
} else {
$this->_actionMap[$action] = $type;
$this->settings['actionMap'][$action] = $type;
}
}
}

View file

@ -775,6 +775,20 @@ class AuthTest extends CakeTestCase {
$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.
*