Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| HasRole | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Middleware; |
| 4 | |
| 5 | use Closure; |
| 6 | use Illuminate\Contracts\Auth\Factory as Auth; |
| 7 | use Illuminate\Support\Facades\Gate; |
| 8 | |
| 9 | use Core\Models\Papel; |
| 10 | |
| 11 | class HasRole |
| 12 | { |
| 13 | public function __construct(protected Auth $auth){} |
| 14 | |
| 15 | public function handle($request, Closure $next, ...$roles) |
| 16 | { |
| 17 | try { |
| 18 | foreach($roles as $role) { |
| 19 | if(Gate::check('papel', Papel::$$role)) { |
| 20 | return $next($request); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | throw new \Exception(); |
| 25 | } catch (\Exception $e) { |
| 26 | return response()->json('Não autorizado', 401); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 |