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
This commit is contained in:
nate 2007-12-13 07:03:59 +00:00
parent 6c719eca1a
commit 9d58121205
5 changed files with 51 additions and 33 deletions

View file

@ -282,8 +282,8 @@ class AuthComponent extends Object {
$url = $controller->params['url']['url']; $url = $controller->params['url']['url'];
} }
$this->loginAction = $this->_normalizeURL($this->loginAction); $this->loginAction = Router::normalize($this->loginAction);
if ($this->loginAction == $this->_normalizeURL($url)) { if ($this->loginAction == Router::normalize($url)) {
if (empty($controller->data) || !isset($controller->data[$this->userModel])) { if (empty($controller->data) || !isset($controller->data[$this->userModel])) {
if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) { if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) {
$this->Session->write('Auth.redirect', $controller->referer()); $this->Session->write('Auth.redirect', $controller->referer());
@ -574,7 +574,7 @@ class AuthComponent extends Object {
$this->Session->del($this->sessionKey); $this->Session->del($this->sessionKey);
$this->Session->del('Auth.redirect'); $this->Session->del('Auth.redirect');
$this->_loggedIn = false; $this->_loggedIn = false;
return $this->_normalizeURL($this->logoutRedirect); return Router::normalize($this->logoutRedirect);
} }
/** /**
* Get the current user from the session. * Get the current user from the session.
@ -614,13 +614,13 @@ class AuthComponent extends Object {
$redir = $this->Session->read('Auth.redirect'); $redir = $this->Session->read('Auth.redirect');
$this->Session->delete('Auth.redirect'); $this->Session->delete('Auth.redirect');
if ($this->_normalizeURL($redir) == $this->loginAction) { if (Router::normalize($redir) == $this->loginAction) {
$redir = $this->loginRedirect; $redir = $this->loginRedirect;
} }
} else { } else {
$redir = $this->loginRedirect; $redir = $this->loginRedirect;
} }
return $this->_normalizeURL($redir); return Router::normalize($redir);
} }
/** /**
* Validates a user against an abstract object. * Validates a user against an abstract object.
@ -794,29 +794,5 @@ class AuthComponent extends Object {
$this->Session->del('Auth.redirect'); $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;
}
} }
?> ?>

View file

@ -572,6 +572,9 @@ class Router extends Object {
if ($current) { if ($current) {
return $_this->__paths[count($_this->__paths) - 1]; return $_this->__paths[count($_this->__paths) - 1];
} }
if (!isset($_this->__paths[0])) {
return array('base' => null);
}
return $_this->__paths[0]; return $_this->__paths[0];
} }
/** /**
@ -1010,6 +1013,29 @@ class Router extends Object {
} }
return $out; 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. * Returns the route matching the current request URL.
* *

View file

@ -639,7 +639,7 @@ class DispatcherTest extends UnitTestCase {
$expected = 'add'; $expected = 'add';
$this->assertIdentical($controller->action, $expected); $this->assertIdentical($controller->action, $expected);
$expected = array('param'=>'value', 'param2'=>'value2'); $expected = array('param' => 'value', 'param2' => 'value2');
$this->assertEqual($controller->params['named'], $expected); $this->assertEqual($controller->params['named'], $expected);

View file

@ -270,7 +270,7 @@ class AuthTest extends CakeTestCase {
$this->Controller->Auth->userModel = 'AuthUser'; $this->Controller->Auth->userModel = 'AuthUser';
$this->Controller->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'welcome'); $this->Controller->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'welcome');
$this->Controller->Auth->startup($this->Controller); $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->assertEqual($expected, $this->Controller->Auth->redirect());
$this->Controller->Session->del('Auth'); $this->Controller->Session->del('Auth');
@ -280,7 +280,7 @@ class AuthTest extends CakeTestCase {
$this->Controller->Auth->userModel = 'AuthUser'; $this->Controller->Auth->userModel = 'AuthUser';
$this->Controller->Auth->loginRedirect = null; $this->Controller->Auth->loginRedirect = null;
$this->Controller->Auth->startup($this->Controller); $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->assertTrue($this->Controller->Session->check('Message.auth'));
$this->assertEqual($expected, $this->Controller->Auth->redirect()); $this->assertEqual($expected, $this->Controller->Auth->redirect());
@ -299,7 +299,7 @@ class AuthTest extends CakeTestCase {
$this->Controller->Auth->userModel = 'AuthUser'; $this->Controller->Auth->userModel = 'AuthUser';
$this->Controller->Auth->loginRedirect = false; $this->Controller->Auth->loginRedirect = false;
$this->Controller->Auth->startup($this->Controller); $this->Controller->Auth->startup($this->Controller);
$expected = $this->Controller->Auth->_normalizeURL('admin'); $expected = Router::normalize('admin');
$this->assertEqual($expected, $this->Controller->Auth->redirect()); $this->assertEqual($expected, $this->Controller->Auth->redirect());
$_SERVER['HTTP_REFERER'] = $backup; $_SERVER['HTTP_REFERER'] = $backup;

View file

@ -123,6 +123,22 @@ class RouterTest extends UnitTestCase {
$this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add')); $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() { function testUrlGeneration() {
$this->router->reload(); $this->router->reload();
extract($this->router->getNamedExpressions()); extract($this->router->getNamedExpressions());