From bea30e62cbca0a8f143fcc4932c3f2d1da036bca Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 10 Feb 2014 17:23:20 +0530 Subject: [PATCH 1/3] Renamed CakeRequest::onlyAllow() to CakeRequest::allowMethod(). Existing name is unintuitive and it's not easily apparent what the method does. Closes #2803 --- lib/Cake/Network/CakeRequest.php | 28 +++++++++++++++---- .../Test/Case/Network/CakeRequestTest.php | 18 ++++++++---- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index 27055662f..20f60843a 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -909,23 +909,23 @@ class CakeRequest implements ArrayAccess { } /** - * Only allow certain HTTP request methods, if the request method does not match + * Allow only certain HTTP request methods. If the request method does not match * a 405 error will be shown and the required "Allow" response header will be set. * * Example: * - * $this->request->onlyAllow('post', 'delete'); + * $this->request->allowMethod('post', 'delete'); * or - * $this->request->onlyAllow(array('post', 'delete')); + * $this->request->allowMethod(array('post', 'delete')); * * If the request would be GET, response header "Allow: POST, DELETE" will be set - * and a 405 error will be returned + * and a 405 error will be returned. * - * @param string|array $methods Allowed HTTP request methods + * @param string|array $methods Allowed HTTP request methods. * @return boolean true * @throws MethodNotAllowedException */ - public function onlyAllow($methods) { + public function allowMethod($methods) { if (!is_array($methods)) { $methods = func_get_args(); } @@ -940,6 +940,22 @@ class CakeRequest implements ArrayAccess { throw $e; } +/** + * Alias of CakeRequest::allowMethod() for backwards compatibility. + * + * @see CakeRequest::allowMethod() + * @deprecated 2.5 Use CakeRequest::allowMethod() instead. + * @param string|array $methods Allowed HTTP request methods. + * @return boolean true + * @throws MethodNotAllowedException + */ + public function onlyAllow($methods) { + if (!is_array($methods)) { + $methods = func_get_args(); + } + return $this->allowMethod($methods); + } + /** * Read data from php://input, mocked in tests. * diff --git a/lib/Cake/Test/Case/Network/CakeRequestTest.php b/lib/Cake/Test/Case/Network/CakeRequestTest.php index 4882b66d7..32517f241 100644 --- a/lib/Cake/Test/Case/Network/CakeRequestTest.php +++ b/lib/Cake/Test/Case/Network/CakeRequestTest.php @@ -2221,38 +2221,44 @@ XML; } /** - * Test onlyAllow method + * Test allowMethod method * * @return void */ - public function testOnlyAllow() { + public function testAllowMethod() { $_SERVER['REQUEST_METHOD'] = 'PUT'; $request = new CakeRequest('/posts/edit/1'); + $this->assertTrue($request->allowMethod(array('put'))); + + // BC check $this->assertTrue($request->onlyAllow(array('put'))); $_SERVER['REQUEST_METHOD'] = 'DELETE'; + $this->assertTrue($request->allowMethod('post', 'delete')); + + // BC check $this->assertTrue($request->onlyAllow('post', 'delete')); } /** - * Test onlyAllow throwing exception + * Test allowMethod throwing exception * * @return void */ - public function testOnlyAllowException() { + public function testAllowMethodException() { $_SERVER['REQUEST_METHOD'] = 'PUT'; $request = new CakeRequest('/posts/edit/1'); try { - $request->onlyAllow('POST', 'DELETE'); + $request->allowMethod('POST', 'DELETE'); $this->fail('An expected exception has not been raised.'); } catch (MethodNotAllowedException $e) { $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader()); } $this->setExpectedException('MethodNotAllowedException'); - $request->onlyAllow('POST'); + $request->allowMethod('POST'); } /** From 1da79080ac6869237342c575dc40339b8c866c3e Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 10 Feb 2014 17:56:57 +0530 Subject: [PATCH 2/3] Updated bake templates to use CakeRequest::allowMethod() --- .../Console/Templates/default/actions/controller_actions.ctp | 2 +- lib/Cake/Test/bake_compare/Controller/ActionsUsingSessions.ctp | 2 +- lib/Cake/Test/bake_compare/Controller/ActionsWithNoSessions.ctp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Console/Templates/default/actions/controller_actions.ctp b/lib/Cake/Console/Templates/default/actions/controller_actions.ctp index 7ce64dbcc..b6b89a993 100644 --- a/lib/Cake/Console/Templates/default/actions/controller_actions.ctp +++ b/lib/Cake/Console/Templates/default/actions/controller_actions.ctp @@ -135,7 +135,7 @@ if (!$this->->exists()) { throw new NotFoundException(__('Invalid ')); } - $this->request->onlyAllow('post', 'delete'); + $this->request->allowMethod('post', 'delete'); if ($this->->delete()) { $this->Session->setFlash(__('The has been deleted.')); diff --git a/lib/Cake/Test/bake_compare/Controller/ActionsUsingSessions.ctp b/lib/Cake/Test/bake_compare/Controller/ActionsUsingSessions.ctp index 58bc8cf98..5cd6a4dee 100644 --- a/lib/Cake/Test/bake_compare/Controller/ActionsUsingSessions.ctp +++ b/lib/Cake/Test/bake_compare/Controller/ActionsUsingSessions.ctp @@ -81,7 +81,7 @@ if (!$this->BakeArticle->exists()) { throw new NotFoundException(__('Invalid bake article')); } - $this->request->onlyAllow('post', 'delete'); + $this->request->allowMethod('post', 'delete'); if ($this->BakeArticle->delete()) { $this->Session->setFlash(__('The bake article has been deleted.')); } else { diff --git a/lib/Cake/Test/bake_compare/Controller/ActionsWithNoSessions.ctp b/lib/Cake/Test/bake_compare/Controller/ActionsWithNoSessions.ctp index d72105d72..d3f5b19d5 100644 --- a/lib/Cake/Test/bake_compare/Controller/ActionsWithNoSessions.ctp +++ b/lib/Cake/Test/bake_compare/Controller/ActionsWithNoSessions.ctp @@ -75,7 +75,7 @@ if (!$this->BakeArticle->exists()) { throw new NotFoundException(__('Invalid bake article')); } - $this->request->onlyAllow('post', 'delete'); + $this->request->allowMethod('post', 'delete'); if ($this->BakeArticle->delete()) { return $this->flash(__('The bake article has been deleted.'), array('action' => 'index')); } else { From d838e7b56f0797d8401f4b62a0100f3fdc56ad90 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 10 Feb 2014 18:14:25 +0530 Subject: [PATCH 3/3] Updated upgrade shell to change CakeRequest::onlyAllow() usages. --- lib/Cake/Console/Command/UpgradeShell.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Cake/Console/Command/UpgradeShell.php b/lib/Cake/Console/Command/UpgradeShell.php index 699bcf4e1..0242f79af 100644 --- a/lib/Cake/Console/Command/UpgradeShell.php +++ b/lib/Cake/Console/Command/UpgradeShell.php @@ -392,6 +392,11 @@ class UpgradeShell extends AppShell { '/(\$this->action\b(?!\())/', '$this->request->action' ), + array( + '$this->request->onlyAllow() -> $this->request->allowMethod()', + '/\$this->request->onlyAllow\(/', + '$this->request->allowMethod(' + ) ); $this->_filesRegexpUpdate($patterns); }