Making AuthComponent::mapActions() delegate to the authorize objects. Adding tests.

This commit is contained in:
mark_story 2011-01-04 23:12:37 -05:00
parent e96b20b93e
commit f21970c533
2 changed files with 24 additions and 32 deletions

View file

@ -511,28 +511,6 @@ class AuthComponent extends Component {
return $this->_authorizeObjects; return $this->_authorizeObjects;
} }
/**
* Get authorization type
*
* @param string $auth Type of authorization
* @return array Associative array with: type, object
* @access private
*/
function __authType($auth = null) {
if ($auth == null) {
$auth = $this->authorize;
}
$object = null;
if (is_array($auth)) {
$type = key($auth);
$object = $auth[$type];
} else {
$type = $auth;
return compact('type');
}
return compact('type', 'object');
}
/** /**
* Takes a list of actions in the current controller for which authentication is not required, or * Takes a list of actions in the current controller for which authentication is not required, or
* no parameters to allow all actions. * no parameters to allow all actions.
@ -580,22 +558,20 @@ class AuthComponent extends Component {
} }
/** /**
* Maps action names to CRUD operations. Used for controller-based authentication. * Maps action names to CRUD operations. Used for controller-based authentication. Make sure
* to configure the authorize property before calling this method. As it delegates $map to all the
* attached authorize objects.
* *
* @param array $map Actions to map * @param array $map Actions to map
* @return void * @return void
* @link http://book.cakephp.org/view/1260/mapActions * @link http://book.cakephp.org/view/1260/mapActions
*/ */
public function mapActions($map = array()) { public function mapActions($map = array()) {
$crud = array('create', 'read', 'update', 'delete'); if (empty($this->_authorizeObjects)) {
foreach ($map as $action => $type) { $this->loadAuthorizeObjects();
if (in_array($action, $crud) && is_array($type)) { }
foreach ($type as $typedAction) { foreach ($this->_authorizeObjects as $auth) {
$this->actionMap[$typedAction] = $action; $auth->mapActions($map);
}
} else {
$this->actionMap[$action] = $type;
}
} }
} }

View file

@ -1470,4 +1470,20 @@ class AuthTest extends CakeTestCase {
$this->assertNull($this->Controller->Session->read('Auth.AuthUser')); $this->assertNull($this->Controller->Session->read('Auth.AuthUser'));
$this->assertNull($this->Controller->Session->read('Auth.redirect')); $this->assertNull($this->Controller->Session->read('Auth.redirect'));
} }
/**
* test mapActions loading and delegating to authorize objects.
*
* @return void
*/
function testMapActionsDelegation() {
$this->getMock('BaseAuthorize', array('authorize'), array(), 'MapActionMockAuthorize', false);
$this->Controller->Auth->authorize = array('MapActionMock');
$mock = $this->Controller->Auth->loadAuthorizeObjects();
$mock[0]->expects($this->once())
->method('mapActions')
->with(array('create' => array('my_action')));
$this->Controller->Auth->mapActions(array('create' => array('my_action')));
}
} }