Skip to content

Handle multiple guards. #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
16 changes: 7 additions & 9 deletions src/Middleware/LaratrustAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LaratrustAbility extends LaratrustMiddleware
*/
public function handle($request, Closure $next, $roles, $permissions, $team = null, $options = '')
{
list($team, $validateAll, $guard) = $this->assignRealValuesTo($team, $options);
[$team, $validateAll, $guards] = $this->assignRealValuesTo($team, $options);

if (!is_array($roles)) {
$roles = explode(self::DELIMITER, $roles);
Expand All @@ -31,16 +31,14 @@ public function handle($request, Closure $next, $roles, $permissions, $team = nu
$permissions = explode(self::DELIMITER, $permissions);
}

if (
Auth::guard($guard)->guest()
|| !Auth::guard($guard)->user()
->ability($roles, $permissions, $team, [
foreach ($guards as $guard) {
if (!Auth::guard($guard)->guest() && Auth::guard($guard)->user()->ability($roles, $permissions, $team, [
'validate_all' => $validateAll
])
) {
return $this->unauthorized();
])) {
return $next($request);
}
}

return $next($request);
return $this->unauthorized();
}
}
26 changes: 15 additions & 11 deletions src/Middleware/LaratrustMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ class LaratrustMiddleware
*/
protected function authorization($type, $rolesPermissions, $team, $options)
{
list($team, $requireAll, $guard) = $this->assignRealValuesTo($team, $options);
[$team, $requireAll, $guards] = $this->assignRealValuesTo($team, $options);
$method = $type == 'roles' ? 'hasRole' : 'hasPermission';

if (!is_array($rolesPermissions)) {
$rolesPermissions = explode(self::DELIMITER, $rolesPermissions);
}

return !Auth::guard($guard)->guest()
&& Auth::guard($guard)->user()->$method($rolesPermissions, $team, $requireAll);
foreach ($guards as $guard) {
if (!Auth::guard($guard)->guest() && Auth::guard($guard)->user()->$method(
$rolesPermissions,
$team,
$requireAll
)) {
return true;
}
}
return false;
}

/**
Expand Down Expand Up @@ -70,28 +78,24 @@ protected function assignRealValuesTo($team, $options)
return [
(Str::contains($team, ['require_all', 'guard:']) ? null : $team),
(Str::contains($team, 'require_all') ?: Str::contains($options, 'require_all')),
(Str::contains($team, 'guard:') ? $this->extractGuard($team) : (
Str::contains($options, 'guard:')
? $this->extractGuard($options)
: Config::get('auth.defaults.guard')
)),
($this->extractGuards($team) ?: $this->extractGuards($options) ?: [Config::get('auth.defaults.guard')]),
];
}

/**
* Extract the guard type from the given string.
*
* @param string $string
* @return string
* @return array
*/
protected function extractGuard($string)
protected function extractGuards($string)
{
$options = Collection::make(explode('|', $string));

return $options->reject(function ($option) {
return strpos($option, 'guard:') === false;
})->map(function ($option) {
return explode(':', $option)[1];
})->first();
})->toArray();
}
}
2 changes: 1 addition & 1 deletion tests/Checkers/User/LaratrustUserCanCheckerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Laratrust\Tests\LaratrustTestCase;
use Laratrust\Tests\Models\Permission;

class LaratrustUserCanCheckerTestCase extends LaratrustTestCase
abstract class LaratrustUserCanCheckerTestCase extends LaratrustTestCase
{
protected $user;

Expand Down
53 changes: 53 additions & 0 deletions tests/Middleware/LaratrustPermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,59 @@ public function testHandle_IsLoggedInWithPermission_ShouldNotAbort()
}, 'users-create|users-update', 'TeamA', 'guard:api|require_all'));
}

public function testHandle_IsLoggedInWithPermissionAndMultipleGuards_ShouldNotAbort()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$guard2 = m::mock('Illuminate\Contracts\Auth\Guard');
$user = m::mock('Laratrust\Tests\Models\User')->makePartial();
$middleware = new LaratrustPermission($this->guard);

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
Auth::shouldReceive('guard')->with('api')->andReturn($this->guard);
Auth::shouldReceive('guard')->with('web')->andReturn($guard2);
$this->guard->shouldReceive('guest')->andReturn(true);
$guard2->shouldReceive('guest')->andReturn(false);
$guard2->shouldReceive('user')->andReturn($user);
$user->shouldReceive('hasPermission')
->with(
['users-create', 'users-update'],
m::anyOf(null, 'TeamA'),
m::anyOf(true, false)
)
->andReturn(true);

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$this->assertNull($middleware->handle($this->request, function () {
}, 'users-create|users-update'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'users-create|users-update', 'guard:api|guard:web'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'users-create|users-update', 'require_all'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'users-create|users-update', 'guard:api|guard:web|require_all'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'users-create|users-update', 'TeamA', 'require_all'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'users-create|users-update', 'TeamA', 'guard:api|guard:web|require_all'));
}

public function testHandle_IsLoggedInWithNoPermission_ShouldRedirectWithError()
{
/*
Expand Down
55 changes: 55 additions & 0 deletions tests/Middleware/MiddlewareLaratrustAbilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,59 @@ public function testHandle_IsLoggedInWithAbility_ShouldNotAbort()
$this->assertNull($middleware->handle($this->request, function () {
}, 'admin|user', 'edit-users|update-users', 'TeamA', 'require_all|guard:api'));
}


public function testHandle_IsLoggedInWithAbilityAndMultipleGuards_ShouldNotAbort()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$guard2 = m::mock('Illuminate\Contracts\Auth\Guard');
$user = m::mock('Laratrust\Tests\Models\User')->makePartial();
$middleware = new LaratrustAbility($this->guard);

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
Auth::shouldReceive('guard')->with('api')->andReturn($this->guard);
Auth::shouldReceive('guard')->with('web')->andReturn($guard2);
$this->guard->shouldReceive('guest')->andReturn(true);
$guard2->shouldReceive('guest')->andReturn(false);
$guard2->shouldReceive('user')->andReturn($user);
$user->shouldReceive('ability')
->with(
['admin', 'user'],
['edit-users', 'update-users'],
m::anyOf(null, 'TeamA'),
m::anyOf(['validate_all' => true], ['validate_all' => false])
)
->andReturn(true);

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$this->assertNull($middleware->handle($this->request, function () {
}, 'admin|user', 'edit-users|update-users'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'admin|user', 'edit-users|update-users', 'guard:api|guard:web'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'admin|user', 'edit-users|update-users', 'require_all'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'admin|user', 'edit-users|update-users', 'require_all|guard:api|guard:web'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'admin|user', 'edit-users|update-users', 'TeamA', 'require_all'));

$this->assertNull($middleware->handle($this->request, function () {
}, 'admin|user', 'edit-users|update-users', 'TeamA', 'require_all|guard:api|guard:web'));
}
}