<?php

namespace Sargilla\FlatLab\Middleware;

use App\User;
use Closure;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = User::all()->count();
        if (!($user == 1)) {
            if (!\Auth::user()->hasPermissionTo('admin-usuarios')) //If user does //not have this permission
            {
                abort('401');
            }
        }

        return $next($request);
    }
}
