configureRateLimiting(); $this->cspReportRoute(); $this->routes(function () { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); }); Route::bind('account', function ($value, RoutingRoute $route) { /** @var \App\Models\User|null */ if (!($user = \auth()->user())) { abort(401); } $this->assertClientHasBrands($user); // * If route has brand parameter /** @var \App\Models\Brand|null */ if ($brand = $route->parameter('brand')) { return Account::where('id', \intval($value))->whereHas('brand', function ($q) use ($brand) { $q->where('id', $brand->id); })->firstOrFail(); } return Account::where('id', \intval($value))->firstOrFail(); }); Route::bind('brand', function ($value) { /** @var \App\Models\User|null */ if (!($user = \auth()->user())) { abort(401); } $this->assertClientHasBrands($user); return Account::where('id', \intval($value))->firstOrFail(); }); Route::model('credit_log', function ($value) { /** @var \App\Models\User|null */ if (!($user = \auth()->user())) { abort(401); } $this->assertClientHasBrands($user); return CreditLog::where('id', \intval($value))->firstOrFail(); }); Route::model('brand', Brand::class); Route::model('label', Label::class); Route::model('marker', Marker::class); Route::model('package', Package::class); Route::model('dutyhour', Dutyhour::class); Route::model('information', Information::class); Route::model('company', Company::class); Route::model('invoice', Invoice::class); Route::model('file', File::class); Route::model('log', Log::class); Route::model('user', User::class); Route::model('site', Site::class); Route::model('project', Project::class); Route::model('ticket_type', TicketType::class); Route::bind('ticket', function ($value) { return Ticket::where('id', $value)->has('site')->firstOrFail(); }); } /** * Configure the rate limiters for the application. * * @return void */ protected function configureRateLimiting() { RateLimiter::for('credentials', function (Request $request) { return Limit::perMinute(30)->by(optional($request->user())->id ?: $request->ip()); }); RateLimiter::for('api', function (Request $request) { return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip()); }); } /** * Assert client user has brands * or return to tickets.index with warning * * @param \App\Models\User $user * @return void */ private function assertClientHasBrands(User $user): void { if (!$user->isAdmin()) { $userBrandsAccounts = $user->brands()->each(function (Brand $brand) { $brand->load('accounts'); })->pluck('accounts')->flatten(); if (!$userBrandsAccounts->count()) { \redirect()->route('ticket.index')->withErrors(['warning' => trans( // phpcs:ignore Generic.Files.LineLength.TooLong 'Pour utiliser cette fonctionnalité vous devez appartenir à une marque, merci d\'en faire la demande à :mail', ['mail' => collect(config('mail.contactsList.serviceweb'))->implode(' ')] )])->throwResponse(); } } } /** * * Register CSP report URI * * Put this function inside RouteServiceProvider, * and call it in RouteServiceProvider::boot * * ! Use this in conjonction with kwaadpepper/exception-handler * * @return void */ private function cspReportRoute() { // * CSP REPORT URI, Please add /csp-report to VerifyCsrfToken $except middleware \Illuminate\Support\Facades\Route::post('/csp-report', function () { $isCspRequest = request()->header('Content-Type') === 'application/csp-report'; if (!$isCspRequest or !request()->json('csp-report') or !is_array(request()->json('csp-report'))) { abort(401); } // * Flatten if needed $cspReport = collect(request()->json('csp-report')) ->flatMap(function ($v, $k) { return [$k => is_array($v) ? collect($v)->flatten()->implode(', ') : $v]; })->all(); // * Filter array keys $cspReport = collect(array_intersect_key($cspReport, [ 'blocked-uri' => '', 'document-uri' => '', 'original-policy' => '', 'referrer' => '', 'violated-directive' => '' ])); report(new \Illuminate\Http\Client\HttpClientException(sprintf( "CSP REPORT for %s : \n %s", $cspReport->get('document-uri'), $cspReport->implode("\n") ))); })->middleware('throttle:5,2') ->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class) ->name('cspReportUri'); } }