184 lines
6.4 KiB
PHP
184 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Models\Brand;
|
|
use App\Models\Company;
|
|
use App\Models\Credit\Account;
|
|
use App\Models\Credit\CreditLog;
|
|
use App\Models\Dutyhour;
|
|
use App\Models\Information;
|
|
use App\Models\Invoice;
|
|
use App\Models\Label;
|
|
use App\Models\Package;
|
|
use App\Models\Project;
|
|
use App\Models\Site;
|
|
use App\Models\Ticket;
|
|
use App\Models\Ticket\TicketType;
|
|
use App\Models\User;
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
|
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Route as RoutingRoute;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
class RouteServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* The path to the "home" route for your application.
|
|
*
|
|
* This is used by Laravel authentication to redirect users after login.
|
|
*
|
|
* @var string
|
|
*/
|
|
public const HOME = '/';
|
|
|
|
/**
|
|
* Define your route model bindings, pattern filters, etc.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
$this->configureRateLimiting();
|
|
|
|
$this->cspReportRoute();
|
|
|
|
$this->routes(function () {
|
|
Route::middleware('web')
|
|
->namespace($this->namespace)
|
|
->group(base_path('routes/web-bo.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('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(): void
|
|
{
|
|
RateLimiter::for('api', function (Request $request) {
|
|
return Limit::perMinute(150)->by($request->user()?->id ?: app('clientIp'));
|
|
});
|
|
RateLimiter::for('web', function (Request $request) {
|
|
return Limit::perMinute(150)->by($request->user()?->id ?: app('clientIp'));
|
|
});
|
|
RateLimiter::for('credentials', function (Request $request) {
|
|
return Limit::perMinute(30)->by($request->user()?->id ?: app('clientIp'));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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('bo.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
|
|
*
|
|
* @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' => ''
|
|
]));
|
|
\Illuminate\Support\Facades\Log::warning(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');
|
|
}
|
|
}
|