Skip to content

Commit

Permalink
Refactors code for clarity, updates doc block.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremykendall committed May 15, 2013
1 parent e0e0edf commit fb71177
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
24 changes: 11 additions & 13 deletions library/Fa/Middleware/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion library/Fa/Middleware/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit fb71177

Please sign in to comment.