Last change
on this file since dfae77e was dfae77e, checked in by Igor Danilovski <igor_danilovski@…>, 2 years ago |
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Middleware;
|
---|
4 |
|
---|
5 | use App\Providers\RouteServiceProvider;
|
---|
6 | use Closure;
|
---|
7 | use Illuminate\Contracts\Foundation\Application;
|
---|
8 | use Illuminate\Http\JsonResponse;
|
---|
9 | use Illuminate\Http\RedirectResponse;
|
---|
10 | use Illuminate\Http\Request;
|
---|
11 | use Illuminate\Routing\Redirector;
|
---|
12 | use Illuminate\Support\Facades\Auth;
|
---|
13 | use Illuminate\Support\Facades\Storage;
|
---|
14 |
|
---|
15 | class UserRole
|
---|
16 | {
|
---|
17 | /**
|
---|
18 | * Handle an incoming request.
|
---|
19 | *
|
---|
20 | * @param Request $request
|
---|
21 | * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
---|
22 | * @return Application|JsonResponse|RedirectResponse|Redirector
|
---|
23 | */
|
---|
24 | public function handle(Request $request, Closure $next, ...$roles)
|
---|
25 | {
|
---|
26 | if ($request->expectsJson()) {
|
---|
27 | return response()->json(['error' => 'Unauthenticated.'], 401);
|
---|
28 | }
|
---|
29 |
|
---|
30 | if (!Auth::check()) {
|
---|
31 | return redirect(RouteServiceProvider::HOME);
|
---|
32 | }
|
---|
33 |
|
---|
34 | if (in_array(strtolower(Auth::user()->type), $roles)) {
|
---|
35 | return $next($request);
|
---|
36 | }
|
---|
37 |
|
---|
38 | return redirect('/login');
|
---|
39 | }
|
---|
40 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.