<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use App\Models\Enseignant_Privilege;

class SessionPrivilegeMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (count(Enseignant_Privilege::where(function ($query) {
                $query->where('id_Enseignant', '=', Auth::user()->id_Enseignant)->where('id_Privilege', '=', '1');
            })->orWhere(function ($query) {
                $query->where('id_Enseignant', '=', Auth::user()->id_Enseignant)->where('id_Privilege', '=', '2');
            })->get()) > 0
        )
            return $next($request);
        else
            return redirect('/');
    }
}
