Merge pull request #2804 from ADmad/2.5-cakerequest

Renamed CakeRequest::onlyAllow() to CakeRequest::allowMethod().
This commit is contained in:
José Lorenzo Rodríguez 2014-02-11 11:20:11 +01:00
commit 0e46b5c827
6 changed files with 42 additions and 15 deletions

View file

@ -392,6 +392,11 @@ class UpgradeShell extends AppShell {
'/(\$this->action\b(?!\())/', '/(\$this->action\b(?!\())/',
'$this->request->action' '$this->request->action'
), ),
array(
'$this->request->onlyAllow() -> $this->request->allowMethod()',
'/\$this->request->onlyAllow\(/',
'$this->request->allowMethod('
)
); );
$this->_filesRegexpUpdate($patterns); $this->_filesRegexpUpdate($patterns);
} }

View file

@ -135,7 +135,7 @@
if (!$this-><?php echo $currentModelName; ?>->exists()) { if (!$this-><?php echo $currentModelName; ?>->exists()) {
throw new NotFoundException(__('Invalid <?php echo strtolower($singularHumanName); ?>')); throw new NotFoundException(__('Invalid <?php echo strtolower($singularHumanName); ?>'));
} }
$this->request->onlyAllow('post', 'delete'); $this->request->allowMethod('post', 'delete');
if ($this-><?php echo $currentModelName; ?>->delete()) { if ($this-><?php echo $currentModelName; ?>->delete()) {
<?php if ($wannaUseSession): ?> <?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been deleted.')); $this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been deleted.'));

View file

@ -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. * a 405 error will be shown and the required "Allow" response header will be set.
* *
* Example: * Example:
* *
* $this->request->onlyAllow('post', 'delete'); * $this->request->allowMethod('post', 'delete');
* or * 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 * 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 * @return boolean true
* @throws MethodNotAllowedException * @throws MethodNotAllowedException
*/ */
public function onlyAllow($methods) { public function allowMethod($methods) {
if (!is_array($methods)) { if (!is_array($methods)) {
$methods = func_get_args(); $methods = func_get_args();
} }
@ -940,6 +940,22 @@ class CakeRequest implements ArrayAccess {
throw $e; 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. * Read data from php://input, mocked in tests.
* *

View file

@ -2221,38 +2221,44 @@ XML;
} }
/** /**
* Test onlyAllow method * Test allowMethod method
* *
* @return void * @return void
*/ */
public function testOnlyAllow() { public function testAllowMethod() {
$_SERVER['REQUEST_METHOD'] = 'PUT'; $_SERVER['REQUEST_METHOD'] = 'PUT';
$request = new CakeRequest('/posts/edit/1'); $request = new CakeRequest('/posts/edit/1');
$this->assertTrue($request->allowMethod(array('put')));
// BC check
$this->assertTrue($request->onlyAllow(array('put'))); $this->assertTrue($request->onlyAllow(array('put')));
$_SERVER['REQUEST_METHOD'] = 'DELETE'; $_SERVER['REQUEST_METHOD'] = 'DELETE';
$this->assertTrue($request->allowMethod('post', 'delete'));
// BC check
$this->assertTrue($request->onlyAllow('post', 'delete')); $this->assertTrue($request->onlyAllow('post', 'delete'));
} }
/** /**
* Test onlyAllow throwing exception * Test allowMethod throwing exception
* *
* @return void * @return void
*/ */
public function testOnlyAllowException() { public function testAllowMethodException() {
$_SERVER['REQUEST_METHOD'] = 'PUT'; $_SERVER['REQUEST_METHOD'] = 'PUT';
$request = new CakeRequest('/posts/edit/1'); $request = new CakeRequest('/posts/edit/1');
try { try {
$request->onlyAllow('POST', 'DELETE'); $request->allowMethod('POST', 'DELETE');
$this->fail('An expected exception has not been raised.'); $this->fail('An expected exception has not been raised.');
} catch (MethodNotAllowedException $e) { } catch (MethodNotAllowedException $e) {
$this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader()); $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
} }
$this->setExpectedException('MethodNotAllowedException'); $this->setExpectedException('MethodNotAllowedException');
$request->onlyAllow('POST'); $request->allowMethod('POST');
} }
/** /**

View file

@ -81,7 +81,7 @@
if (!$this->BakeArticle->exists()) { if (!$this->BakeArticle->exists()) {
throw new NotFoundException(__('Invalid bake article')); throw new NotFoundException(__('Invalid bake article'));
} }
$this->request->onlyAllow('post', 'delete'); $this->request->allowMethod('post', 'delete');
if ($this->BakeArticle->delete()) { if ($this->BakeArticle->delete()) {
$this->Session->setFlash(__('The bake article has been deleted.')); $this->Session->setFlash(__('The bake article has been deleted.'));
} else { } else {

View file

@ -75,7 +75,7 @@
if (!$this->BakeArticle->exists()) { if (!$this->BakeArticle->exists()) {
throw new NotFoundException(__('Invalid bake article')); throw new NotFoundException(__('Invalid bake article'));
} }
$this->request->onlyAllow('post', 'delete'); $this->request->allowMethod('post', 'delete');
if ($this->BakeArticle->delete()) { if ($this->BakeArticle->delete()) {
return $this->flash(__('The bake article has been deleted.'), array('action' => 'index')); return $this->flash(__('The bake article has been deleted.'), array('action' => 'index'));
} else { } else {