<commit_before><?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Protection
{
    /**
     * Handle an incoming request.
     *
     * @param $request
     * @param Closure $next
     * @param $productModuleNumber
     * @param null $failedRouteName
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function handle($request, Closure $next, $productModuleNumber, $failedRouteName)
    {
        //check if user authenticate
        if (!Auth::guard()->check()) {
            return redirect()->route('login');
        }

        if (!auth()->user()->hasAccess($productModuleNumber)){
            return redirect()->route($failedRouteName, [
                'dest' => url()->current(),
                'product_module_number' => $productModuleNumber,
            ]);
        }

        return $next($request);
    }
}
<commit_msg>Issue #29 Handle invalid NetLicensing credentials<commit_after><?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use NetLicensing\Context;
use NetLicensing\NetLicensingService;
use NetLicensing\RestException;

class Protection
{
    /**
     * Handle an incoming request.
     *
     * @param $request
     * @param Closure $next
     * @param $productModuleNumber
     * @param null $failedRouteName
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function handle($request, Closure $next, $productModuleNumber, $failedRouteName)
    {
        //check if user authenticate
        if (!Auth::guard()->check()) {
            return redirect()->route('login');
        }

        try {
            if (!auth()->user()->hasAccess($productModuleNumber)) {
                return redirect()->route($failedRouteName, [
                    'dest' => url()->current(),
                    'product_module_number' => $productModuleNumber,
                ]);
            }
        } catch (\Exception $e) {
            if ($e instanceof RestException) {

                $wiki = 'Check out our wiki https://github.com/Labs64/laravel-boilerplate/wiki/NetLicensing-connection-error';

                if (NetLicensingService::getInstance()->lastCurlInfo()->httpStatusCode == 401) {
                    throw new RestException($e->getMessage() . ' ' . $wiki);
                }

                switch (env('LABS64_NETLICENSING_SECURITY_MODE')) {
                    case Context::BASIC_AUTHENTICATION:
                        throw new RestException('Missing or invalid Username/Password ' . $wiki);
                        break;
                    case Context::APIKEY_IDENTIFICATION:
                        throw new RestException('Missing or invalid API Key ' . $wiki);
                        break;
                }
            }
        }

        return $next($request);
    }
}

{'fraction_non_alphanumeric': 0.12167300380228137, 'fraction_numerical': 0.0031109574835810577, 'mean_word_length': 2.0527426160337554, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4020887', 'n_tokens_mistral': 772, 'n_tokens_neox': 743, 'n_words': 155}