Skip to content

Commit 717cdfc

Browse files
committed
Added option to set the guard in the middlewares
- Fix #186 - Added the option to set the guard to use in the middleware. - Drop support to 5.1 because the guard system doesn't work as >=5.2.
1 parent 3ba32b5 commit 717cdfc

10 files changed

+268
-222
lines changed

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
],
1212
"require": {
1313
"php": ">=5.5.9",
14-
"illuminate/cache": "~5.1",
15-
"illuminate/console": "~5.1",
16-
"illuminate/database": "~5.1",
17-
"illuminate/support": "^5.1.40",
14+
"illuminate/auth": "~5.2",
15+
"illuminate/cache": "~5.2",
16+
"illuminate/console": "~5.2",
17+
"illuminate/database": "~5.2",
18+
"illuminate/support": "^5.2",
1819
"kkszymanowski/traitor": "^0.2.0"
1920
},
2021
"require-dev": {

src/Laratrust/Middleware/LaratrustAbility.php

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,25 @@
1111
*/
1212

1313
use Closure;
14-
use Illuminate\Contracts\Auth\Guard;
15-
use Illuminate\Support\Facades\Config;
14+
use Illuminate\Support\Facades\Auth;
1615

17-
class LaratrustAbility
16+
class LaratrustAbility extends LaratrustMiddleware
1817
{
19-
const DELIMITER = '|';
20-
21-
protected $auth;
22-
23-
/**
24-
* Creates a new instance of the middleware.
25-
*
26-
* @param Guard $auth
27-
*/
28-
public function __construct(Guard $auth)
29-
{
30-
$this->auth = $auth;
31-
}
3218

3319
/**
34-
* Handle an incoming request.
20+
* Handle incoming request.
3521
*
3622
* @param \Illuminate\Http\Request $request
37-
* @param Closure $next
38-
* @param $roles
39-
* @param $permissions
40-
* @param bool $validateAll
23+
* @param Closure $next
24+
* @param string $roles
25+
* @param string $permissions
26+
* @param string|null $team
27+
* @param string|null $options
4128
* @return mixed
4229
*/
43-
public function handle($request, Closure $next, $roles, $permissions, $team = null, $validateAll = false)
30+
public function handle($request, Closure $next, $roles, $permissions, $team = null, $options = '')
4431
{
45-
list($team, $validateAll) = $this->assignRealValuesTo($team, $validateAll);
32+
list($team, $validateAll, $guard) = $this->assignRealValuesTo($team, $options);
4633

4734
if (!is_array($roles)) {
4835
$roles = explode(self::DELIMITER, $roles);
@@ -52,29 +39,16 @@ public function handle($request, Closure $next, $roles, $permissions, $team = nu
5239
$permissions = explode(self::DELIMITER, $permissions);
5340
}
5441

55-
if ($this->auth->guest() ||
56-
!$request->user()->ability($roles, $permissions, $team, [ 'validate_all' => $validateAll ])) {
57-
return call_user_func(
58-
Config::get('laratrust.middleware.handling', 'abort'),
59-
Config::get('laratrust.middleware.params', '403')
60-
);
42+
if (
43+
Auth::guard($guard)->guest()
44+
|| !Auth::guard($guard)->user()
45+
->ability($roles, $permissions, $team, [
46+
'validate_all' => $validateAll
47+
])
48+
) {
49+
return $this->unauthorized();
6150
}
6251

6352
return $next($request);
6453
}
65-
66-
/**
67-
* Assing the real values to the team and requireAllOrOptions parameters.
68-
*
69-
* @param mixed $team
70-
* @param mixed $requireAllOrOptions
71-
* @return array
72-
*/
73-
private function assignRealValuesTo($team, $requireAllOrOptions)
74-
{
75-
return [
76-
($team == 'require_all' ? null : $team),
77-
($team == 'require_all' ? true : ($requireAllOrOptions== 'require_all' ? true : false)),
78-
];
79-
}
8054
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Laratrust\Middleware;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Support\Collection;
7+
use Illuminate\Support\Facades\Auth;
8+
use Illuminate\Support\Facades\Config;
9+
10+
class LaratrustMiddleware
11+
{
12+
const DELIMITER = '|';
13+
14+
/**
15+
* Check if the request has authorization to continue.
16+
*
17+
* @param string $type
18+
* @param string $rolesPermissions
19+
* @param string|null $team
20+
* @param string|null $options
21+
* @return boolean
22+
*/
23+
protected function authorization($type, $rolesPermissions, $team, $options)
24+
{
25+
list($team, $requireAll, $guard) = $this->assignRealValuesTo($team, $options);
26+
$method = $type == 'roles' ? 'hasRole' : 'hasPermission';
27+
28+
if (!is_array($rolesPermissions)) {
29+
$rolesPermissions = explode(self::DELIMITER, $rolesPermissions);
30+
}
31+
32+
return !Auth::guard($guard)->guest()
33+
&& Auth::guard($guard)->user()->$method($rolesPermissions, $team, $requireAll);
34+
}
35+
36+
/**
37+
* The request is unauthorized, so it handles the aborting/redirecting.
38+
*
39+
* @return \Illuminate\Http\Response
40+
*/
41+
protected function unauthorized()
42+
{
43+
return call_user_func(
44+
Config::get('laratrust.middleware.handling', 'abort'),
45+
Config::get('laratrust.middleware.params', '403')
46+
);
47+
}
48+
49+
/**
50+
* Generate an array with the real values of the parameters given to the middleware.
51+
*
52+
* @param string $team
53+
* @param string $options
54+
* @return array
55+
*/
56+
protected function assignRealValuesTo($team, $options)
57+
{
58+
return [
59+
(Str::contains($team, ['require_all', 'guard:']) ? null : $team),
60+
(Str::contains($team, 'require_all') ?: Str::contains($options, 'require_all')),
61+
(Str::contains($team, 'guard:') ? $this->extractGuard($team) : (
62+
Str::contains($options, 'guard:')
63+
? $this->extractGuard($options)
64+
: Config::get('auth.defaults.guard')
65+
)),
66+
];
67+
}
68+
69+
/**
70+
* Extract the guard type from the given string.
71+
*
72+
* @param string $string
73+
* @return string
74+
*/
75+
protected function extractGuard($string)
76+
{
77+
$options = Collection::make(explode('|', $string));
78+
79+
return $options->reject(function ($option) {
80+
return strpos($option, 'guard:') === false;
81+
})->map(function ($option) {
82+
return explode(':', $option)[1];
83+
})->first();
84+
}
85+
}

src/Laratrust/Middleware/LaratrustPermission.php

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,25 @@
1111
*/
1212

1313
use Closure;
14-
use Illuminate\Contracts\Auth\Guard;
15-
use Illuminate\Support\Facades\Config;
1614

17-
class LaratrustPermission
15+
class LaratrustPermission extends LaratrustMiddleware
1816
{
19-
const DELIMITER = '|';
20-
21-
protected $auth;
22-
2317
/**
24-
* Creates a new instance of the middleware.
25-
*
26-
* @param Guard $auth
27-
*/
28-
public function __construct(Guard $auth)
29-
{
30-
$this->auth = $auth;
31-
}
32-
33-
/**
34-
* Handle an incoming request.
18+
* Handle incoming request.
3519
*
3620
* @param \Illuminate\Http\Request $request
37-
* @param Closure $next
38-
* @param $permissions
21+
* @param Closure $next
22+
* @param string $permissions
23+
* @param string|null $team
24+
* @param string|null $options
3925
* @return mixed
4026
*/
41-
public function handle($request, Closure $next, $permissions, $team = null, $requireAll = false)
27+
public function handle($request, Closure $next, $permissions, $team = null, $options = '')
4228
{
43-
list($team, $requireAll) = $this->assignRealValuesTo($team, $requireAll);
44-
45-
if (!is_array($permissions)) {
46-
$permissions = explode(self::DELIMITER, $permissions);
47-
}
48-
49-
if ($this->auth->guest() || !$request->user()->hasPermission($permissions, $team, $requireAll)) {
50-
return call_user_func(
51-
Config::get('laratrust.middleware.handling', 'abort'),
52-
Config::get('laratrust.middleware.params', '403')
53-
);
29+
if (!$this->authorization('permissions', $permissions, $team, $options)) {
30+
return $this->unauthorized();
5431
}
5532

5633
return $next($request);
5734
}
58-
59-
/**
60-
* Assing the real values to the team and requireAllOrOptions parameters.
61-
*
62-
* @param mixed $team
63-
* @param mixed $requireAllOrOptions
64-
* @return array
65-
*/
66-
private function assignRealValuesTo($team, $requireAllOrOptions)
67-
{
68-
return [
69-
($team == 'require_all' ? null : $team),
70-
($team == 'require_all' ? true : ($requireAllOrOptions== 'require_all' ? true : false)),
71-
];
72-
}
7335
}

src/Laratrust/Middleware/LaratrustRole.php

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,25 @@
1111
*/
1212

1313
use Closure;
14-
use Illuminate\Contracts\Auth\Guard;
15-
use Illuminate\Support\Facades\Config;
1614

17-
class LaratrustRole
15+
class LaratrustRole extends LaratrustMiddleware
1816
{
19-
const DELIMITER = '|';
20-
21-
protected $auth;
22-
2317
/**
24-
* Creates a new instance of the middleware.
25-
*
26-
* @param Guard $auth
27-
*/
28-
public function __construct(Guard $auth)
29-
{
30-
$this->auth = $auth;
31-
}
32-
33-
/**
34-
* Handle an incoming request.
18+
* Handle incoming request.
3519
*
3620
* @param \Illuminate\Http\Request $request
37-
* @param Closure $next
38-
* @param $roles
21+
* @param Closure $next
22+
* @param string $roles
23+
* @param string|null $team
24+
* @param string|null $options
3925
* @return mixed
4026
*/
41-
public function handle($request, Closure $next, $roles, $team = null, $requireAll = false)
27+
public function handle($request, Closure $next, $roles, $team = null, $options = '')
4228
{
43-
list($team, $requireAll) = $this->assignRealValuesTo($team, $requireAll);
44-
45-
if (!is_array($roles)) {
46-
$roles = explode(self::DELIMITER, $roles);
47-
}
48-
49-
if ($this->auth->guest() || !$request->user()->hasRole($roles, $team, $requireAll)) {
50-
return call_user_func(
51-
Config::get('laratrust.middleware.handling', 'abort'),
52-
Config::get('laratrust.middleware.params', '403')
53-
);
29+
if (!$this->authorization('roles', $roles, $team, $options)) {
30+
return $this->unauthorized();
5431
}
5532

5633
return $next($request);
5734
}
58-
59-
/**
60-
* Assing the real values to the team and requireAllOrOptions parameters.
61-
*
62-
* @param mixed $team
63-
* @param mixed $requireAllOrOptions
64-
* @return array
65-
*/
66-
private function assignRealValuesTo($team, $requireAllOrOptions)
67-
{
68-
return [
69-
($team == 'require_all' ? null : $team),
70-
($team == 'require_all' ? true : ($requireAllOrOptions== 'require_all' ? true : false)),
71-
];
72-
}
7335
}

0 commit comments

Comments
 (0)