|
3 | 3 | use Closure;
|
4 | 4 | use Illuminate\Contracts\Auth\Guard;
|
5 | 5 |
|
6 |
| -class AdminMiddleware { |
7 |
| - |
8 |
| - protected $auth; |
9 |
| - |
10 |
| - public function __construct(Guard $auth) { |
11 |
| - $this->auth = $auth; |
12 |
| - } |
13 |
| - |
14 |
| - /** |
15 |
| - * Handle an incoming request. |
16 |
| - * |
17 |
| - * @param \Illuminate\Http\Request $request |
18 |
| - * @param \Closure $next |
19 |
| - * @return mixed |
20 |
| - */ |
21 |
| - public function handle($request, Closure $next) |
22 |
| - { |
23 |
| - //We don't need to add a second middleware |
24 |
| - //this one can check whether the requester is a user, too |
25 |
| - if($this->auth->guest()){ |
26 |
| - return response('Not authorized', 403); |
27 |
| - } |
28 |
| - |
29 |
| - if($this->auth->user()->role != 'admin') { |
30 |
| - |
31 |
| - $this->auth->logout(); |
32 |
| - |
33 |
| - return response('Not authorized', 403); |
34 |
| - } |
35 |
| - |
36 |
| - return $next($request); |
37 |
| - } |
| 6 | +class AdminMiddleware |
| 7 | +{ |
| 8 | + |
| 9 | + protected $auth; |
| 10 | + |
| 11 | + public function __construct(Guard $auth) |
| 12 | + { |
| 13 | + $this->auth = $auth; |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Handle an incoming request. |
| 18 | + * |
| 19 | + * @param \Illuminate\Http\Request $request |
| 20 | + * @param \Closure $next |
| 21 | + * @return mixed |
| 22 | + */ |
| 23 | + public function handle($request, Closure $next) |
| 24 | + { |
| 25 | + //We don't need to add a second middleware |
| 26 | + //this one can check whether the requester is a user, too |
| 27 | + if ($this->auth->guest()) { |
| 28 | + return response('Not authorized', 403); |
| 29 | + } |
| 30 | + |
| 31 | + if ($this->auth->user()->role != 'admin') { |
| 32 | + |
| 33 | + $this->auth->logout(); |
| 34 | + |
| 35 | + return response('Not authorized', 403); |
| 36 | + } |
| 37 | + |
| 38 | + return $next($request); |
| 39 | + } |
38 | 40 |
|
39 | 41 | }
|
0 commit comments