<?php

namespace App\Http\Middleware;

use App\User;
use App\Models;
use App\Models\IPAccess;
use Closure;
use Illuminate\Support\Facades\Auth;

class Admin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $ip = $request->ip(); 
        $ifExist = IPAccess::Is_Webservice_Caller_Authorized($ip,'System User');				//return $next($request);
        if ($ifExist) {  
        //if ($ip === config('constants.admin.ip')) {            

            if (Auth::check()) {
                $user    = Auth::user();
                $userobj = User::with('role')->find($user->id);            
                foreach ($userobj->role as $roles) {
                    if ($roles->slug != "user") {
                        return $next($request);
                    }
                }
            }
        }
        return redirect('/');
    }
}
