| 1 | <?php |
||
| 2 | |||
| 3 | namespace Werk365\JwtAuthRoles\Middlewares; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use Illuminate\Support\Facades\Auth; |
||
| 7 | use Werk365\JwtAuthRoles\Exceptions\AuthException; |
||
| 8 | |||
| 9 | class RoleMiddleware |
||
| 10 | { |
||
| 11 | public function handle($request, Closure $next, $role) |
||
| 12 | { |
||
| 13 | $roles = is_array($role) |
||
| 14 | ? $role |
||
| 15 | : explode('|', $role); |
||
| 16 | $user_roles = array_map('strtolower', Auth::user()->roles); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 17 | $same = (array_intersect($roles, $user_roles)); |
||
| 18 | |||
| 19 | if (empty($same)) { |
||
| 20 | throw AuthException::auth('401', 'User does not have right roles'); |
||
| 21 | } |
||
| 22 | |||
| 23 | return $next($request); |
||
| 24 | } |
||
| 25 | } |
||
| 26 |