Skip to content

Commit

Permalink
Added the option in the middleware to abort or redirect
Browse files Browse the repository at this point in the history
Solves #21
  • Loading branch information
santigarcor committed Aug 28, 2016
1 parent 53b4521 commit f34f223
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Laratrust/Middleware/LaratrustAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Config;

class LaratrustAbility
{
Expand Down Expand Up @@ -55,7 +56,7 @@ public function handle($request, Closure $next, $roles, $permissions, $validateA

if ($this->auth->guest() ||
!$request->user()->ability($roles, $permissions, [ 'validate_all' => $validateAll ])) {
abort(403);
return call_user_func(Config::get('laratrust.middleware_handling'), Config::get('middleware_params'));
}

return $next($request);
Expand Down
3 changes: 2 additions & 1 deletion src/Laratrust/Middleware/LaratrustPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Config;

class LaratrustPermission
{
Expand Down Expand Up @@ -44,7 +45,7 @@ public function handle($request, Closure $next, $permissions)
}

if ($this->auth->guest() || !$request->user()->can($permissions)) {
abort(403);
return call_user_func(Config::get('laratrust.middleware_handling'), Config::get('middleware_params'));
}

return $next($request);
Expand Down
3 changes: 2 additions & 1 deletion src/Laratrust/Middleware/LaratrustRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Config;

class LaratrustRole
{
Expand Down Expand Up @@ -44,7 +45,7 @@ public function handle($request, Closure $next, $roles)
}

if ($this->auth->guest() || !$request->user()->hasRole($roles)) {
abort(403);
return call_user_func(Config::get('laratrust.middleware_handling'), Config::get('middleware_params'));
}

return $next($request);
Expand Down
15 changes: 15 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,19 @@
|--------------------------------------------------------------------------
*/
'permission_foreign_key' => 'permission_id',

/*
|--------------------------------------------------------------------------
| Method to be called in the middleware return case
| Available: abort|redirect
|--------------------------------------------------------------------------
*/
'middleware_handling' => 'abort',

/*
|--------------------------------------------------------------------------
| Parameter passed to the middleware_handling method
|--------------------------------------------------------------------------
*/
'middleware_params' => '403',
];
15 changes: 14 additions & 1 deletion tests/Middleware/LaratrustAbilityTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Config;
use Laratrust\Middleware\LaratrustAbility;
use Mockery as m;

Expand All @@ -24,6 +25,10 @@ public function testHandle_IsGuestWithNoAbility_ShouldAbort403()
*/
$guard->shouldReceive('guest')->andReturn(true);
$request->user()->shouldReceive('ability')->andReturn(false);
Config::shouldReceive('get')->once()->with('laratrust.middleware_handling')
->andReturn('abort');
Config::shouldReceive('get')->once()->with('middleware_params')
->andReturn('403');

$middleware->handle($request, function () {}, null, null, true);

Expand Down Expand Up @@ -54,6 +59,10 @@ public function testHandle_IsGuestWithAbility_ShouldAbort403()
*/
$guard->shouldReceive('guest')->andReturn(true);
$request->user()->shouldReceive('ability')->andReturn(true);
Config::shouldReceive('get')->once()->with('laratrust.middleware_handling')
->andReturn('abort');
Config::shouldReceive('get')->once()->with('middleware_params')
->andReturn('403');

$middleware->handle($request, function () {}, null, null);

Expand Down Expand Up @@ -84,6 +93,10 @@ public function testHandle_IsLoggedInWithNoAbility_ShouldAbort403()
*/
$guard->shouldReceive('guest')->andReturn(false);
$request->user()->shouldReceive('ability')->andReturn(false);
Config::shouldReceive('get')->once()->with('laratrust.middleware_handling')
->andReturn('abort');
Config::shouldReceive('get')->once()->with('middleware_params')
->andReturn('403');

$middleware->handle($request, function () {}, null, null);

Expand Down Expand Up @@ -113,7 +126,7 @@ public function testHandle_IsLoggedInWithAbility_ShouldNotAbort()
|------------------------------------------------------------
*/
$guard->shouldReceive('guest')->andReturn(false);
$request->user()->shouldReceive('ability')->andReturn(true);
$request->user()->shouldReceive('ability')->andReturn(true);;

$middleware->handle($request, function () {}, null, null);

Expand Down
13 changes: 13 additions & 0 deletions tests/Middleware/LaratrustPermissionTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Config;
use Laratrust\Middleware\LaratrustPermission;
use Mockery as m;

Expand All @@ -24,6 +25,10 @@ public function testHandle_IsGuestWithNoPermission_ShouldAbort403()
*/
$guard->shouldReceive('guest')->andReturn(true);
$request->user()->shouldReceive('can')->andReturn(false);
Config::shouldReceive('get')->once()->with('laratrust.middleware_handling')
->andReturn('abort');
Config::shouldReceive('get')->once()->with('middleware_params')
->andReturn('403');

$middleware->handle($request, function () {}, null, null, true);

Expand Down Expand Up @@ -54,6 +59,10 @@ public function testHandle_IsGuestWithPermission_ShouldAbort403()
*/
$guard->shouldReceive('guest')->andReturn(true);
$request->user()->shouldReceive('can')->andReturn(true);
Config::shouldReceive('get')->once()->with('laratrust.middleware_handling')
->andReturn('abort');
Config::shouldReceive('get')->once()->with('middleware_params')
->andReturn('403');

$middleware->handle($request, function () {}, null, null);

Expand Down Expand Up @@ -84,6 +93,10 @@ public function testHandle_IsLoggedInWithNoPermission_ShouldAbort403()
*/
$guard->shouldReceive('guest')->andReturn(false);
$request->user()->shouldReceive('can')->andReturn(false);
Config::shouldReceive('get')->once()->with('laratrust.middleware_handling')
->andReturn('abort');
Config::shouldReceive('get')->once()->with('middleware_params')
->andReturn('403');

$middleware->handle($request, function () {}, null, null);

Expand Down
14 changes: 14 additions & 0 deletions tests/Middleware/LaratrustRoleTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Config;
use Laratrust\Middleware\LaratrustRole;
use Mockery as m;

Expand All @@ -25,6 +26,11 @@ public function testHandle_IsGuestWithMismatchingRole_ShouldAbort403()
$guard->shouldReceive('guest')->andReturn(true);
$request->user()->shouldReceive('hasRole')->andReturn(false);

Config::shouldReceive('get')->once()->with('laratrust.middleware_handling')
->andReturn('abort');
Config::shouldReceive('get')->once()->with('middleware_params')
->andReturn('403');

$middleware->handle($request, function () {}, null, null, true);

/*
Expand Down Expand Up @@ -54,6 +60,10 @@ public function testHandle_IsGuestWithMatchingRole_ShouldAbort403()
*/
$guard->shouldReceive('guest')->andReturn(true);
$request->user()->shouldReceive('hasRole')->andReturn(true);
Config::shouldReceive('get')->once()->with('laratrust.middleware_handling')
->andReturn('abort');
Config::shouldReceive('get')->once()->with('middleware_params')
->andReturn('403');

$middleware->handle($request, function () {}, null, null);

Expand Down Expand Up @@ -84,6 +94,10 @@ public function testHandle_IsLoggedInWithMismatchRole_ShouldAbort403()
*/
$guard->shouldReceive('guest')->andReturn(false);
$request->user()->shouldReceive('hasRole')->andReturn(false);
Config::shouldReceive('get')->once()->with('laratrust.middleware_handling')
->andReturn('abort');
Config::shouldReceive('get')->once()->with('middleware_params')
->andReturn('403');

$middleware->handle($request, function () {}, null, null);

Expand Down
41 changes: 37 additions & 4 deletions tests/Middleware/MiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
<?php

use Illuminate\Support\Facades\Config;
use Mockery as m;

abstract class MiddlewareTest extends PHPUnit_Framework_TestCase
{
public static $abortCode = null;

public function setUp()
{
parent::setUp();

$app = m::mock('app')->shouldReceive('instance')->getMock();

$this->facadeMocks['config'] = m::mock('config');

Config::setFacadeApplication($app);
Config::swap($this->facadeMocks['config']);
}

public static function setupBeforeClass()
{
if (! function_exists('abort')) {
/**
* Mimicks Laravel5's abort() helper function.
*
* Instead of calling \Illuminate\Foundation\Application::abort(), this function keeps track of
* the last abort called, so the abort can be retrieved for test assertions.
*
* @see https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/helpers.php#L7-L23
* Instead of calling \Illuminate\Foundation\Application::abort(),
* this function keeps track of the last abort called,
* so the abort can be retrieved for test assertions.
*
* @param int $code
* @param string $message
Expand All @@ -27,6 +39,27 @@ function abort($code, $message = '', array $headers = [])
MiddlewareTest::$abortCode = $code;
}
}

if (! function_exists('redirect')) {
/**
* Mimicks Laravel5's redirect() helper function.
*
* This function keeps track of the last abort called,
* so the abort can be retrieved for test assertions.
*
* @see https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/helpers.php
*
* @param string $to
* @param int $status
* @param array $headers
* @param bool $secure
* @return void
*/
function redirect($to = null, $status = 302, $headers = [], $secure = null)
{
MiddlewareTest::$abortCode = $url;
}
}
}

public function tearDown()
Expand Down

0 comments on commit f34f223

Please sign in to comment.