From fb711771ed9b7a8b1c745685a9b1534bee55dafe Mon Sep 17 00:00:00 2001 From: Jeremy Kendall Date: Wed, 15 May 2013 11:04:02 -0500 Subject: [PATCH] Refactors code for clarity, updates doc block. --- library/Fa/Middleware/Authentication.php | 24 +++++++++++------------- library/Fa/Middleware/Navigation.php | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/library/Fa/Middleware/Authentication.php b/library/Fa/Middleware/Authentication.php index 9e78d50..513b317 100644 --- a/library/Fa/Middleware/Authentication.php +++ b/library/Fa/Middleware/Authentication.php @@ -54,25 +54,23 @@ public function __construct(AuthenticationService $auth, array $config) public function call() { $app = $this->app; - $auth = $this->auth; $req = $app->request(); + $auth = $this->auth; $config = $this->config; - $this->app->hook('slim.before.router', function () use ($app, $auth, $req, $config) { - $securedUrls = isset($config['security.urls']) ? $config['security.urls'] : array(); - foreach ($securedUrls as $surl) { - $patternAsRegex = $surl['path']; - $patternAsRegex = '@^' . $patternAsRegex . '$@'; - if (preg_match($patternAsRegex, $req->getPathInfo())) { - if (!$auth->hasIdentity()) { - if ($req->getPath() !== $config['login.url']) { - $app->redirect($config['login.url']); - } - } + $checkAuth = function () use ($app, $auth, $req, $config) { + $securedUrls = isset($config['security.urls']) ? $config['security.urls'] : array(); + foreach ($securedUrls as $url) { + $urlPattern = '@^' . $url['path'] . '$@'; + if (preg_match($urlPattern, $req->getPathInfo()) && !$auth->hasIdentity()) { + if ($req->getPath() !== $config['login.url']) { + $app->redirect($config['login.url']); } } } - ); + }; + + $this->app->hook('slim.before.router', $checkAuth); $this->next->call(); } diff --git a/library/Fa/Middleware/Navigation.php b/library/Fa/Middleware/Navigation.php index 3544116..5a35e05 100644 --- a/library/Fa/Middleware/Navigation.php +++ b/library/Fa/Middleware/Navigation.php @@ -40,7 +40,7 @@ public function __construct(AuthenticationService $auth) /** * Constructs array of navigation items and appends them to the view. Navigation - * items differ if user is authenticated or not. Uses 'slim.before.router'. + * items differ if user is authenticated or not. */ public function call() {