Skip to content

Commit

Permalink
Remove var args in favor of array syntax. Resolves #3422
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed May 5, 2014
1 parent 33dea13 commit 7456928
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
38 changes: 15 additions & 23 deletions src/Controller/Component/AuthComponent.php
Expand Up @@ -477,53 +477,45 @@ public function constructAuthorize() {
* Takes a list of actions in the current controller for which authentication is not required, or
* no parameters to allow all actions.
*
* You can use allow with either an array, or var args.
* You can use allow with either an array or a simple string.
*
* `$this->Auth->allow(array('edit', 'add'));` or
* `$this->Auth->allow('edit', 'add');` or
* `$this->Auth->allow('view');`
* `$this->Auth->allow(['edit', 'add']);`
* `$this->Auth->allow();` to allow all actions
*
* @param string|array $action,... Controller action name or array of actions
* @param string|array $actions Controller action name or array of actions
* @return void
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-public
*/
public function allow($action = null) {
$args = func_get_args();
if (empty($args) || $action === null) {
public function allow($actions = null) {
if ($actions === null) {
$this->allowedActions = $this->_methods;
return;
}
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
$this->allowedActions = array_merge($this->allowedActions, $args);
$this->allowedActions = array_merge($this->allowedActions, (array)$actions);
}

/**
* Removes items from the list of allowed/no authentication required actions.
*
* You can use deny with either an array, or var args.
* You can use deny with either an array or a simple string.
*
* `$this->Auth->deny(array('edit', 'add'));` or
* `$this->Auth->deny('edit', 'add');` or
* `$this->Auth->deny('view');`
* `$this->Auth->deny(['edit', 'add']);`
* `$this->Auth->deny();` to remove all items from the allowed list
*
* @param string|array $action,... Controller action name or array of actions
* @param string|array $actions Controller action name or array of actions
* @return void
* @see AuthComponent::allow()
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-require-authorization
*/
public function deny($action = null) {
$args = func_get_args();
if (empty($args) || $action === null) {
public function deny($actions = null) {
if ($actions === null) {
$this->allowedActions = array();
return;
}
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
foreach ($args as $arg) {
$i = array_search($arg, $this->allowedActions);
foreach ((array)$actions as $action) {
$i = array_search($action, $this->allowedActions);
if (is_int($i)) {
unset($this->allowedActions[$i]);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Controller/Component/SecurityComponent.php
Expand Up @@ -132,12 +132,12 @@ public function startup(Event $event) {
/**
* Sets the actions that require a request that is SSL-secured, or empty for all actions
*
* @param string|array $actions
* @return void
* @link http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#SecurityComponent::requireSecure
*/
public function requireSecure() {
$args = func_get_args();
$this->_requireMethod('Secure', $args);
public function requireSecure($actions) {
$this->_requireMethod('Secure', (array)$actions);
}

/**
Expand All @@ -147,12 +147,12 @@ public function requireSecure() {
* set in SecurityComponent::$allowedControllers and
* SecurityComponent::$allowedActions.
*
* @param string|array $actions
* @return void
* @link http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#SecurityComponent::requireAuth
*/
public function requireAuth() {
$args = func_get_args();
$this->_requireMethod('Auth', $args);
public function requireAuth($actions) {
$this->_requireMethod('Auth', (array)$actions);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Network/Request.php
Expand Up @@ -1022,9 +1022,9 @@ public function env($key, $value = null) {
*
* Example:
*
* $this->request->allowMethod('post', 'delete');
* $this->request->allowMethod('post');
* or
* $this->request->allowMethod(array('post', 'delete'));
* $this->request->allowMethod(['post', 'delete']);
*
* If the request would be GET, response header "Allow: POST, DELETE" will be set
* and a 405 error will be returned.
Expand All @@ -1034,9 +1034,7 @@ public function env($key, $value = null) {
* @throws \Cake\Error\MethodNotAllowedException
*/
public function allowMethod($methods) {
if (!is_array($methods)) {
$methods = func_get_args();
}
$methods = (array)$methods;
foreach ($methods as $method) {
if ($this->is($method)) {
return true;
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Controller/Component/AuthComponentTest.php
Expand Up @@ -379,7 +379,7 @@ public function testAllowDenyAll() {
$this->Controller->Auth->initialize($event);

$this->Controller->Auth->allow();
$this->Controller->Auth->deny('add', 'camelCase');
$this->Controller->Auth->deny(['add', 'camelCase']);

$this->Controller->request['action'] = 'delete';
$this->assertNull($this->Controller->Auth->startup($event));
Expand Down Expand Up @@ -439,7 +439,7 @@ public function testDenyWithCamelCaseMethods() {
$event = new Event('Controller.startup', $this->Controller);
$this->Controller->Auth->initialize($event);
$this->Controller->Auth->allow();
$this->Controller->Auth->deny('add', 'camelCase');
$this->Controller->Auth->deny(['add', 'camelCase']);

$url = '/auth_test/camelCase';
$this->Controller->request->addParams(Router::parse($url));
Expand Down Expand Up @@ -501,7 +501,7 @@ public function testAllowedActionsSetWithAllowMethod() {
$this->Controller->request->query['url'] = Router::normalize($url);
$event = new Event('Controller.initialize', $this->Controller);
$this->Controller->Auth->initialize($event);
$this->Controller->Auth->allow('action_name', 'anotherAction');
$this->Controller->Auth->allow(['action_name', 'anotherAction']);
$this->assertEquals(array('action_name', 'anotherAction'), $this->Controller->Auth->allowedActions);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -2222,10 +2222,10 @@ public function testAllowMethod() {
'REQUEST_METHOD' => 'PUT'
]]);

$this->assertTrue($request->allowMethod(array('put')));
$this->assertTrue($request->allowMethod('put'));

$request->env('REQUEST_METHOD', 'DELETE');
$this->assertTrue($request->allowMethod('post', 'delete'));
$this->assertTrue($request->allowMethod(['post', 'delete']));
}

/**
Expand All @@ -2240,7 +2240,7 @@ public function testAllowMethodException() {
]);

try {
$request->allowMethod('POST', 'DELETE');
$request->allowMethod(['POST', 'DELETE']);
$this->fail('An expected exception has not been raised.');
} catch (Error\MethodNotAllowedException $e) {
$this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
Expand Down

0 comments on commit 7456928

Please sign in to comment.