fix: 🎨 delete HandleInertiaRequests and define new props to share in the AppServiceProvider

This commit is contained in:
Alexis Gousseau 2026-05-19 16:43:41 +02:00
parent 397eeeed77
commit 89dd5e1db7
No known key found for this signature in database
GPG key ID: 7F3C786EE12DBB72
3 changed files with 37 additions and 66 deletions

View file

@ -1,65 +0,0 @@
<?php
namespace App\Http\Middlewares;
use App\Enums\NoticeTypeEnum;
use App\Models\GitlabProject;
use App\Models\InvoiceCompany;
use App\Models\InvoicePackage;
use App\Models\Notice;
use App\Models\Site;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Inertia\Middleware;
use Uwil\LaravelCommon\Helpers\InertiaHelper;
use Uwil\LaravelCommon\Support\InertiaSharedRegistry;
class HandleInertiaRequests extends Middleware
{
/**
* Define the props that are shared by default.
*
* @param \Illuminate\Http\Request $request
* @return array<string, mixed>
*/
public function share(Request $request): array
{
$props = [
...InertiaHelper::errorProps(parent::share($request)),
'auth' => [
'policies' => [
'notices' => [
'viewAny' => Gate::allows('viewAny', Notice::class),
'viewTrash' => Gate::allows('viewTrash', Notice::class),
'create' => Gate::allows('create', Notice::class),
],
'invoicePackages' => [
'viewAny' => Gate::allows('viewAny', InvoicePackage::class),
'viewTrash' => Gate::allows('viewTrash', InvoicePackage::class),
'create' => Gate::allows('create', InvoicePackage::class),
],
'invoiceCompanies' => [
'viewAny' => Gate::allows('viewAny', InvoiceCompany::class),
'viewTrash' => Gate::allows('viewTrash', InvoiceCompany::class),
'create' => Gate::allows('create', InvoiceCompany::class),
],
'gitlabProjects' => [
'viewAny' => Gate::allows('viewAny', GitlabProject::class),
'viewTrash' => Gate::allows('viewTrash', GitlabProject::class),
'create' => Gate::allows('create', GitlabProject::class),
],
'sites' => [
'viewAny' => Gate::allows('viewAny', Site::class),
'viewTrash' => Gate::allows('viewTrash', Site::class),
'create' => Gate::allows('create', Site::class),
],
],
],
'noticeTypeEnum' => NoticeTypeEnum::toArray(),
];
return array_replace_recursive(
$props,
InertiaSharedRegistry::resolve($request),
);
}
}

View file

@ -2,8 +2,16 @@
namespace App\Providers;
use App\Enums\NoticeTypeEnum;
use App\Models\GitlabProject;
use App\Models\InvoiceCompany;
use App\Models\InvoicePackage;
use App\Models\Notice;
use App\Models\Site;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Uwil\LaravelBackend\Support\UserRoleRegistry;
use Uwil\LaravelCommon\Support\InertiaSharedRegistry;
class AppServiceProvider extends ServiceProvider
{
@ -18,5 +26,33 @@ class AppServiceProvider extends ServiceProvider
UserRoleRegistry::add(3, 'project-manager', trans('enums.user-role.project-manager'));
UserRoleRegistry::add(4, 'developper', trans('enums.user-role.developper'));
UserRoleRegistry::add(5, 'client', trans('enums.user-role.client'));
// Set new props to share.
InertiaSharedRegistry::add('noticeTypeEnum', fn () => NoticeTypeEnum::toArray());
InertiaSharedRegistry::add('auth.policies.notices', fn () => [
'viewAny' => Gate::allows('viewAny', Notice::class),
'viewTrash' => Gate::allows('viewTrash', Notice::class),
'create' => Gate::allows('create', Notice::class),
]);
InertiaSharedRegistry::add('auth.policies.invoicePackages', fn () => [
'viewAny' => Gate::allows('viewAny', InvoicePackage::class),
'viewTrash' => Gate::allows('viewTrash', InvoicePackage::class),
'create' => Gate::allows('create', InvoicePackage::class),
]);
InertiaSharedRegistry::add('auth.policies.invoiceCompanies', fn () => [
'viewAny' => Gate::allows('viewAny', InvoiceCompany::class),
'viewTrash' => Gate::allows('viewTrash', InvoiceCompany::class),
'create' => Gate::allows('create', InvoiceCompany::class),
]);
InertiaSharedRegistry::add('auth.policies.gitlabProjects', fn () => [
'viewAny' => Gate::allows('viewAny', GitlabProject::class),
'viewTrash' => Gate::allows('viewTrash', GitlabProject::class),
'create' => Gate::allows('create', GitlabProject::class),
]);
InertiaSharedRegistry::add('auth.policies.sites', fn () => [
'viewAny' => Gate::allows('viewAny', Site::class),
'viewTrash' => Gate::allows('viewTrash', Site::class),
'create' => Gate::allows('create', Site::class),
]);
}
}

View file

@ -45,7 +45,7 @@ return Application::configure(basePath: dirname(__DIR__))
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\Uwil\LaravelI18n\Middlewares\ForceLocale::class,
\App\Http\Middlewares\HandleInertiaRequests::class,
\Uwil\LaravelCommon\Middlewares\HandleInertiaRequests::class,
\Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
\Uwil\LaravelCommon\Middlewares\SecureHeaders::class,
\Uwil\LaravelCommon\Middlewares\TrimStrings::class,