. */ declare(strict_types=1); namespace FireflyIII\Http\Middleware; use Closure; use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; /** * Class IsAdmin. */ class IsAdmin { /** * Handle an incoming request. Must be admin. * * @param Request $request * @param Closure $next * @param string|null $guard * * @return mixed */ public function handle(Request $request, Closure $next, $guard = null) { if (Auth::guard($guard)->guest()) { if ($request->ajax()) { return response('Unauthorized.', 401); } return response()->redirectTo(route('login')); } /** @var User $user */ $user = auth()->user(); /** @var UserRepositoryInterface $repository */ $repository = app(UserRepositoryInterface::class); if (!$repository->hasRole($user, 'owner')) { return response()->redirectTo(route('home')); } return $next($request); } }