HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/inzigd.komma.pro/app/Http/Middleware/PreventCustomersReachingBackend.php
<?php

namespace App\Http\Middleware;

use App\Komma\Users\Models\Role;
use Closure;
use Illuminate\Contracts\Auth\Guard;

/**
 * Class PreventCustomersReachingBackend
 *
 * Prevents customers from reaching the kms.
 * If they try they will be redirected to /
 *
 * @package App\Http\Middleware
 */
class PreventCustomersReachingBackend
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->check()) {
            $pathParts = explode('/', $request->path()); //Path example: "kms/users"
            if(reset($pathParts) == 'kms' && !$this->auth->user()->role->isAtLeast(Role::Admin))
                return redirect()->to('/');
            //TODO GO TO UNAUTHORIZED PAGE
        }

        return $next($request);
    }
}