Conditions | 7 |
Paths | 15 |
Total Lines | 23 |
Lines | 23 |
Ratio | 100 % |
Changes | 0 |
1 | <?php |
||
11 | public function handle($request, Closure $next, $role) |
||
12 | { |
||
13 | if (is_string($role)) { // sample : 'support|super-admin@admin' , here admin is guard name and support , super-admin are role . | Notice: guard is optional. |
||
14 | $parsed = explode('@', $role); |
||
15 | $guard = isset($parsed[1]) |
||
16 | ? $parsed[1] |
||
17 | : null; |
||
18 | $roles = explode('|', $parsed[0]); |
||
19 | } elseif (is_array($role)) { |
||
20 | $guard = isset($role['guard']) ? |
||
21 | $role['guard'] : null; |
||
22 | $roles = $role['role']; |
||
23 | } |
||
24 | if (auth($guard)->guest()) { |
||
25 | throw UnauthorizedException::notLoggedIn(); |
||
26 | } |
||
27 | |||
28 | if (!auth($guard)->user()->hasAnyRole($roles)) { |
||
29 | throw UnauthorizedException::forRoles($roles); |
||
30 | } |
||
31 | |||
32 | return $next($request); |
||
33 | } |
||
34 | } |
||
35 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.