Skip to content

Commit

Permalink
Merge pull request #39 from MGatner/update-filters
Browse files Browse the repository at this point in the history
Update filters, support silent operation, allow user config
  • Loading branch information
lonnieezell authored Apr 24, 2019
2 parents 2618d6d + 77fe96f commit 7970085
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/Authentication/AuthenticationBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public function error()
return $this->error;
}

/**
* Whether to continue instead of throwing exceptions,
* as defined in config.
*
* @return string
*/
public function silent()
{
return $this->config->silent;
}


/**
* Logs a user into the system.
Expand Down
7 changes: 7 additions & 0 deletions src/Config/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class Auth extends BaseConfig
//
public $rememberLength = 30 * DAY;

//--------------------------------------------------------------------
// Error handling
//--------------------------------------------------------------------
// If true, will continue instead of throwing exceptions.
//
public $silent = false;

//--------------------------------------------------------------------
// PASSWORD HASHING COST
//--------------------------------------------------------------------
Expand Down
12 changes: 10 additions & 2 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ public static function authentication(string $lib = 'local', Model $userModel=nu
{
return self::getSharedInstance('authentication', $lib, $userModel, $loginModel);
}

$config = config(Auth::class);

// prioritizes user config in app/Config if found
if (class_exists('\Config\Auth'))
{
$config = config('Config\\Auth');
}
else
{
$config = config(Auth::class);
}

$class = $config->authenticationLibs[$lib];

Expand Down
22 changes: 18 additions & 4 deletions src/Filters/PermissionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,32 @@ class PermissionFilter implements FilterInterface
*/
public function before(RequestInterface $request, $params = null)
{
if (empty($params))
{
return;
}

$authenticate = Services::authentication();

if (! $authenticate->check() || empty($params))

// if no user is logged in then send to the login form
if (! $authenticate->check())
{
return;
return redirect('login');
}

$authorize = Services::authorization();

if (! $authorize->hasPermission($params, $authenticate->id()))
{
throw new \RuntimeException('You do not have permission to view that page.');
if ($authenticate->silent())
{
$redirectURL = session('redirect_url') ?? '/';
unset($_SESSION['redirect_url']);
return redirect()->to($redirectURL)->with('error', lang('Auth.notEnoughPrivilege'));
}
else {
throw new \RuntimeException(lang('Auth.notEnoughPrivilege'));
}
}
}

Expand Down
25 changes: 19 additions & 6 deletions src/Filters/RoleFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,32 @@ class RoleFilter implements FilterInterface
*/
public function before(RequestInterface $request, $params = null)
{
if (empty($params))
{
return;
}

$authenticate = Services::authentication();

if (! $authenticate->check() || empty($params))

// if no user is logged in then send to the login form
if (! $authenticate->check())
{
return;
return redirect('login');
}

$user = $authenticate->user();
$authorize = Services::authorization();

if (! $authorize->inGroup($params, $user->id))
if (! $authorize->inGroup($params, $authenticate->id()))
{
throw new \RuntimeException('You do not have permission to view that page.');
if ($authenticate->silent())
{
$redirectURL = session('redirect_url') ?? '/';
unset($_SESSION['redirect_url']);
return redirect()->to($redirectURL)->with('error', lang('Auth.notEnoughPrivilege'));
}
else {
throw new \RuntimeException(lang('Auth.notEnoughPrivilege'));
}
}
}

Expand Down

0 comments on commit 7970085

Please sign in to comment.