From 1696df720135338910c0343a18da9d5dc8aa8638 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 5 Jan 2011 23:18:07 -0500 Subject: [PATCH] Removing the last of actionMap from AuthComponent. Its been moved into the authorization objects. Updating and adding tests for crud_authorize. --- cake/libs/controller/components/auth.php | 32 -------------- .../components/auth/crud_authorize.php | 43 ++++++++++++++++++- .../libs/controller/components/auth.test.php | 20 --------- .../components/auth/crud_authorize.test.php | 23 +++++++++- 4 files changed, 62 insertions(+), 56 deletions(-) diff --git a/cake/libs/controller/components/auth.php b/cake/libs/controller/components/auth.php index c18bea649..6feac21ec 100644 --- a/cake/libs/controller/components/auth.php +++ b/cake/libs/controller/components/auth.php @@ -237,20 +237,6 @@ class AuthComponent extends Component { */ 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 * @@ -275,26 +261,8 @@ class AuthComponent extends Component { $this->request = $controller->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; - $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) { App::import('Debugger'); Debugger::checkSecurityKeys(); diff --git a/cake/libs/controller/components/auth/crud_authorize.php b/cake/libs/controller/components/auth/crud_authorize.php index 5e6c9c85c..40844f4b1 100644 --- a/cake/libs/controller/components/auth/crud_authorize.php +++ b/cake/libs/controller/components/auth/crud_authorize.php @@ -31,6 +31,45 @@ App::import('Component', 'auth/base_authorize'); */ 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. * @@ -39,7 +78,7 @@ class CrudAuthorize extends BaseAuthorize { * @return boolean */ public function authorize($user, CakeRequest $request) { - if (!isset($this->_actionMap[$request->params['action']])) { + if (!isset($this->settings['actionMap'][$request->params['action']])) { trigger_error(__( 'CrudAuthorize::authorize() - Attempted access of un-mapped action "%1$s" in controller "%2$s"', $request->action, @@ -53,7 +92,7 @@ class CrudAuthorize extends BaseAuthorize { return $Acl->check( $user, $this->action($request, ':controller'), - $this->_actionMap[$request->params['action']] + $this->settings['actionMap'][$request->params['action']] ); } } \ No newline at end of file diff --git a/cake/tests/cases/libs/controller/components/auth.test.php b/cake/tests/cases/libs/controller/components/auth.test.php index c4dc81e0c..9db1ac1ca 100644 --- a/cake/tests/cases/libs/controller/components/auth.test.php +++ b/cake/tests/cases/libs/controller/components/auth.test.php @@ -1393,26 +1393,6 @@ class AuthTest extends CakeTestCase { $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 * diff --git a/cake/tests/cases/libs/controller/components/auth/crud_authorize.test.php b/cake/tests/cases/libs/controller/components/auth/crud_authorize.test.php index fbad73a77..beee53fb9 100644 --- a/cake/tests/cases/libs/controller/components/auth/crud_authorize.test.php +++ b/cake/tests/cases/libs/controller/components/auth/crud_authorize.test.php @@ -117,11 +117,14 @@ class CrudAuthorizeTest extends CakeTestCase { function testMapActionsGet() { $result = $this->auth->mapActions(); $expected = array( + 'create' => 'create', + 'read' => 'read', + 'update' => 'update', + 'delete' => 'delete', 'index' => 'read', 'add' => 'create', 'edit' => 'update', 'view' => 'read', - 'delete' => 'delete', 'remove' => 'delete' ); $this->assertEquals($expected, $result); @@ -144,6 +147,9 @@ class CrudAuthorizeTest extends CakeTestCase { $result = $this->auth->mapActions(); $expected = array( + 'add' => 'create', + 'create' => 'create', + 'read' => 'read', 'index' => 'read', 'add' => 'create', 'edit' => 'update', @@ -154,9 +160,22 @@ class CrudAuthorizeTest extends CakeTestCase { 'listing' => 'read', 'show' => 'read', 'update' => 'update', - 'random' => 'custom' + 'random' => 'custom', ); $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'])); + } + }