Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdminMiddleware
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
0.00% covered (danger)
0.00%
0 / 1
 handle
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
1<?php
2
3namespace App\Http\Middleware;
4
5use Closure;
6use Illuminate\Http\Request;
7use Illuminate\Support\Facades\Auth;
8
9/**
10 * Blocks any logged-in user whose `users.role` is not "1" (admin) from
11 * reaching admin-only routes. Previously the whole admin panel (products,
12 * purchases, sales, employees, reports, etc.) only checked "auth" — i.e.
13 * "is anyone logged in" — with no check that the user is actually an admin.
14 * Since registration is open to the public, any newly registered account
15 * could browse and edit everything in /admin/*.
16 *
17 * users.role values (see database/migrations/2014_10_12_000000_create_users_table.php):
18 * 1 = admin, 2 = user, 3 = customer.
19 */
20class AdminMiddleware
21{
22    public function handle(Request $request, Closure $next)
23    {
24        $user = Auth::user();
25
26        if (! $user || (string) $user->role !== '1') {
27            abort(403, 'You are not authorized to access the admin panel.');
28        }
29
30        return $next($request);
31    }
32}