From 9d58121205ef885f87797c77486c5de7658caedb Mon Sep 17 00:00:00 2001 From: nate Date: Thu, 13 Dec 2007 07:03:59 +0000 Subject: [PATCH] Removing trailing slash from normalized URLs in AuthComponent, moving AuthComponent::_normalizeURL() to Router::normalize(), refactoring (Ticket #3042) git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6145 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/controller/components/auth.php | 34 +++---------------- cake/libs/router.php | 26 ++++++++++++++ cake/tests/cases/dispatcher.test.php | 2 +- .../libs/controller/components/auth.test.php | 6 ++-- cake/tests/cases/libs/router.test.php | 16 +++++++++ 5 files changed, 51 insertions(+), 33 deletions(-) diff --git a/cake/libs/controller/components/auth.php b/cake/libs/controller/components/auth.php index ae761ee12..b3e2481da 100644 --- a/cake/libs/controller/components/auth.php +++ b/cake/libs/controller/components/auth.php @@ -282,8 +282,8 @@ class AuthComponent extends Object { $url = $controller->params['url']['url']; } - $this->loginAction = $this->_normalizeURL($this->loginAction); - if ($this->loginAction == $this->_normalizeURL($url)) { + $this->loginAction = Router::normalize($this->loginAction); + if ($this->loginAction == Router::normalize($url)) { if (empty($controller->data) || !isset($controller->data[$this->userModel])) { if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) { $this->Session->write('Auth.redirect', $controller->referer()); @@ -574,7 +574,7 @@ class AuthComponent extends Object { $this->Session->del($this->sessionKey); $this->Session->del('Auth.redirect'); $this->_loggedIn = false; - return $this->_normalizeURL($this->logoutRedirect); + return Router::normalize($this->logoutRedirect); } /** * Get the current user from the session. @@ -614,13 +614,13 @@ class AuthComponent extends Object { $redir = $this->Session->read('Auth.redirect'); $this->Session->delete('Auth.redirect'); - if ($this->_normalizeURL($redir) == $this->loginAction) { + if (Router::normalize($redir) == $this->loginAction) { $redir = $this->loginRedirect; } } else { $redir = $this->loginRedirect; } - return $this->_normalizeURL($redir); + return Router::normalize($redir); } /** * Validates a user against an abstract object. @@ -794,29 +794,5 @@ class AuthComponent extends Object { $this->Session->del('Auth.redirect'); } } -/** - * Normalizes a URL - * - * @param string $url URL to normalize - * @return string Normalized URL - * @access protected - */ - function _normalizeURL($url = '/') { - if (is_array($url)) { - $url = Router::url($url); - } - - $paths = Router::getPaths(); - if (!empty($paths['base']) && stristr($url, $paths['base'])) { - $url = str_replace($paths['base'], '', $url); - } - - $url = '/' . $url . '/'; - - while (strpos($url, '//') !== false) { - $url = str_replace('//', '/', $url); - } - return $url; - } } ?> \ No newline at end of file diff --git a/cake/libs/router.php b/cake/libs/router.php index b6e21a44d..2a7395ddd 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -572,6 +572,9 @@ class Router extends Object { if ($current) { return $_this->__paths[count($_this->__paths) - 1]; } + if (!isset($_this->__paths[0])) { + return array('base' => null); + } return $_this->__paths[0]; } /** @@ -1010,6 +1013,29 @@ class Router extends Object { } return $out; } +/** + * Normalizes a URL for purposes of comparison + * + * @param mixed $url URL to normalize + * @return string Normalized URL + * @access public + */ + function normalize($url = '/') { + if (is_array($url)) { + $url = Router::url($url); + } + $paths = Router::getPaths(); + + if (!empty($paths['base']) && stristr($url, $paths['base'])) { + $url = str_replace($paths['base'], '', $url); + } + $url = '/' . $url; + + while (strpos($url, '//') !== false) { + $url = str_replace('//', '/', $url); + } + return preg_replace('/(\/$)/', '', $url); + } /** * Returns the route matching the current request URL. * diff --git a/cake/tests/cases/dispatcher.test.php b/cake/tests/cases/dispatcher.test.php index ae1666652..5c312e198 100644 --- a/cake/tests/cases/dispatcher.test.php +++ b/cake/tests/cases/dispatcher.test.php @@ -639,7 +639,7 @@ class DispatcherTest extends UnitTestCase { $expected = 'add'; $this->assertIdentical($controller->action, $expected); - $expected = array('param'=>'value', 'param2'=>'value2'); + $expected = array('param' => 'value', 'param2' => 'value2'); $this->assertEqual($controller->params['named'], $expected); diff --git a/cake/tests/cases/libs/controller/components/auth.test.php b/cake/tests/cases/libs/controller/components/auth.test.php index d0d326569..6261c99cf 100644 --- a/cake/tests/cases/libs/controller/components/auth.test.php +++ b/cake/tests/cases/libs/controller/components/auth.test.php @@ -270,7 +270,7 @@ class AuthTest extends CakeTestCase { $this->Controller->Auth->userModel = 'AuthUser'; $this->Controller->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'welcome'); $this->Controller->Auth->startup($this->Controller); - $expected = $this->Controller->Auth->_normalizeURL($this->Controller->Auth->loginRedirect); + $expected = Router::normalize($this->Controller->Auth->loginRedirect); $this->assertEqual($expected, $this->Controller->Auth->redirect()); $this->Controller->Session->del('Auth'); @@ -280,7 +280,7 @@ class AuthTest extends CakeTestCase { $this->Controller->Auth->userModel = 'AuthUser'; $this->Controller->Auth->loginRedirect = null; $this->Controller->Auth->startup($this->Controller); - $expected = $this->Controller->Auth->_normalizeURL('admin/'); + $expected = Router::normalize('admin/'); $this->assertTrue($this->Controller->Session->check('Message.auth')); $this->assertEqual($expected, $this->Controller->Auth->redirect()); @@ -299,7 +299,7 @@ class AuthTest extends CakeTestCase { $this->Controller->Auth->userModel = 'AuthUser'; $this->Controller->Auth->loginRedirect = false; $this->Controller->Auth->startup($this->Controller); - $expected = $this->Controller->Auth->_normalizeURL('admin'); + $expected = Router::normalize('admin'); $this->assertEqual($expected, $this->Controller->Auth->redirect()); $_SERVER['HTTP_REFERER'] = $backup; diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index 5038db260..69894b675 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -123,6 +123,22 @@ class RouterTest extends UnitTestCase { $this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add')); } + function testUrlNormalization() { + $expected = '/users/logout'; + + $result = $this->router->normalize('/users/logout/'); + $this->assertEqual($result, $expected); + + $result = $this->router->normalize('//users//logout//'); + $this->assertEqual($result, $expected); + + $result = $this->router->normalize('users/logout'); + $this->assertEqual($result, $expected); + + $result = $this->router->normalize(array('controller' => 'users', 'action' => 'logout')); + $this->assertEqual($result, $expected); + } + function testUrlGeneration() { $this->router->reload(); extract($this->router->getNamedExpressions());