Removing the last of actionMap from AuthComponent. Its been moved into the authorization objects.

Updating and adding tests for crud_authorize.
This commit is contained in:
mark_story 2011-01-05 23:18:07 -05:00
parent e11917ae94
commit 1696df7201
4 changed files with 62 additions and 56 deletions

View file

@ -237,20 +237,6 @@ class AuthComponent extends Component {
*/ */
public $allowedActions = array(); public $allowedActions = array();
/**
* Maps actions to CRUD operations. Used for controller-based validation ($validate = 'controller').
*
* @var array
* @see AuthComponent::mapActions()
*/
public $actionMap = array(
'index' => 'read',
'add' => 'create',
'edit' => 'update',
'view' => 'read',
'remove' => 'delete'
);
/** /**
* Request object * Request object
* *
@ -275,26 +261,8 @@ class AuthComponent extends Component {
$this->request = $controller->request; $this->request = $controller->request;
$this->params = $this->request; $this->params = $this->request;
$crud = array('create', 'read', 'update', 'delete');
$this->actionMap = array_merge($this->actionMap, array_combine($crud, $crud));
$this->_methods = $controller->methods; $this->_methods = $controller->methods;
$prefixes = Router::prefixes();
if (!empty($prefixes)) {
foreach ($prefixes as $prefix) {
$this->actionMap = array_merge($this->actionMap, array(
$prefix . '_index' => 'read',
$prefix . '_add' => 'create',
$prefix . '_edit' => 'update',
$prefix . '_view' => 'read',
$prefix . '_remove' => 'delete',
$prefix . '_create' => 'create',
$prefix . '_read' => 'read',
$prefix . '_update' => 'update',
$prefix . '_delete' => 'delete'
));
}
}
if (Configure::read('debug') > 0) { if (Configure::read('debug') > 0) {
App::import('Debugger'); App::import('Debugger');
Debugger::checkSecurityKeys(); Debugger::checkSecurityKeys();

View file

@ -31,6 +31,45 @@ App::import('Component', 'auth/base_authorize');
*/ */
class CrudAuthorize extends BaseAuthorize { class CrudAuthorize extends BaseAuthorize {
/**
* Sets up additional actionMap values that match the configured `Routing.prefixes`.
*
* @param Controller $controller The controller for this request.
* @param string $settings An array of settings. This class does not use any settings.
*/
public function __construct(Controller $controller, $settings = array()) {
parent::__construct($controller, $settings);
$this->_setPrefixMappings();
}
/**
* sets the crud mappings for prefix routes.
*
* @return void
*/
protected function _setPrefixMappings() {
$crud = array('create', 'read', 'update', 'delete');
$map = array_combine($crud, $crud);
$prefixes = Router::prefixes();
if (!empty($prefixes)) {
foreach ($prefixes as $prefix) {
$map = array_merge($map, array(
$prefix . '_index' => 'read',
$prefix . '_add' => 'create',
$prefix . '_edit' => 'update',
$prefix . '_view' => 'read',
$prefix . '_remove' => 'delete',
$prefix . '_create' => 'create',
$prefix . '_read' => 'read',
$prefix . '_update' => 'update',
$prefix . '_delete' => 'delete'
));
}
}
$this->mapActions($map);
}
/** /**
* Authorize a user using the mapped actions and the AclComponent. * Authorize a user using the mapped actions and the AclComponent.
* *
@ -39,7 +78,7 @@ class CrudAuthorize extends BaseAuthorize {
* @return boolean * @return boolean
*/ */
public function authorize($user, CakeRequest $request) { public function authorize($user, CakeRequest $request) {
if (!isset($this->_actionMap[$request->params['action']])) { if (!isset($this->settings['actionMap'][$request->params['action']])) {
trigger_error(__( trigger_error(__(
'CrudAuthorize::authorize() - Attempted access of un-mapped action "%1$s" in controller "%2$s"', 'CrudAuthorize::authorize() - Attempted access of un-mapped action "%1$s" in controller "%2$s"',
$request->action, $request->action,
@ -53,7 +92,7 @@ class CrudAuthorize extends BaseAuthorize {
return $Acl->check( return $Acl->check(
$user, $user,
$this->action($request, ':controller'), $this->action($request, ':controller'),
$this->_actionMap[$request->params['action']] $this->settings['actionMap'][$request->params['action']]
); );
} }
} }

View file

@ -1393,26 +1393,6 @@ class AuthTest extends CakeTestCase {
$this->assertNull($this->Controller->Session->read('Auth.redirect')); $this->assertNull($this->Controller->Session->read('Auth.redirect'));
} }
/**
* test the initialize callback and its interactions with Router::prefixes()
*
* @return void
*/
function testInitializeAndRoutingPrefixes() {
$restore = Configure::read('Routing');
Configure::write('Routing.prefixes', array('admin', 'super_user'));
Router::reload();
$this->Controller->Auth->initialize($this->Controller);
$this->assertTrue(isset($this->Controller->Auth->actionMap['delete']));
$this->assertTrue(isset($this->Controller->Auth->actionMap['view']));
$this->assertTrue(isset($this->Controller->Auth->actionMap['add']));
$this->assertTrue(isset($this->Controller->Auth->actionMap['admin_view']));
$this->assertTrue(isset($this->Controller->Auth->actionMap['super_user_delete']));
Configure::write('Routing', $restore);
}
/** /**
* test $settings in Controller::$components * test $settings in Controller::$components
* *

View file

@ -117,11 +117,14 @@ class CrudAuthorizeTest extends CakeTestCase {
function testMapActionsGet() { function testMapActionsGet() {
$result = $this->auth->mapActions(); $result = $this->auth->mapActions();
$expected = array( $expected = array(
'create' => 'create',
'read' => 'read',
'update' => 'update',
'delete' => 'delete',
'index' => 'read', 'index' => 'read',
'add' => 'create', 'add' => 'create',
'edit' => 'update', 'edit' => 'update',
'view' => 'read', 'view' => 'read',
'delete' => 'delete',
'remove' => 'delete' 'remove' => 'delete'
); );
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
@ -144,6 +147,9 @@ class CrudAuthorizeTest extends CakeTestCase {
$result = $this->auth->mapActions(); $result = $this->auth->mapActions();
$expected = array( $expected = array(
'add' => 'create',
'create' => 'create',
'read' => 'read',
'index' => 'read', 'index' => 'read',
'add' => 'create', 'add' => 'create',
'edit' => 'update', 'edit' => 'update',
@ -154,9 +160,22 @@ class CrudAuthorizeTest extends CakeTestCase {
'listing' => 'read', 'listing' => 'read',
'show' => 'read', 'show' => 'read',
'update' => 'update', 'update' => 'update',
'random' => 'custom' 'random' => 'custom',
); );
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/**
* test prefix routes getting auto mapped.
*
* @return void
*/
function testAutoPrefixMapActions() {
Configure::write('Routing.prefixes', array('admin', 'manager'));
Router::reload();
$auth = new CrudAuthorize($this->controller);
$this->assertTrue(isset($auth->settings['actionMap']['admin_index']));
}
} }