143 lines
4.2 KiB
PHP
143 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\User;
|
|
use App\Support\Locale;
|
|
use App\Support\Sidebar\Item;
|
|
use App\Support\Translations;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Middleware;
|
|
use Vanguard\Plugins\Plugin;
|
|
use Vanguard\Plugins\Vanguard;
|
|
|
|
class HandleInertiaRequests extends Middleware
|
|
{
|
|
/**
|
|
* The root template loaded on the first page visit.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $rootView = 'app';
|
|
|
|
/**
|
|
* Application settings that the whole UI needs.
|
|
*
|
|
* Page specific settings (throttling, notification toggles, ...) are passed
|
|
* as page props by the controller that owns them rather than shared here.
|
|
*/
|
|
private const SHARED_SETTINGS = [
|
|
'app_name',
|
|
'reg_enabled',
|
|
'forgot_password',
|
|
'remember_me',
|
|
'tos',
|
|
'registration.captcha.enabled',
|
|
'2fa.enabled',
|
|
'password-change.enabled',
|
|
];
|
|
|
|
/**
|
|
* Define the props that are shared by default.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function share(Request $request): array
|
|
{
|
|
/** @var User|null $user */
|
|
$user = $request->user();
|
|
|
|
return array_merge(parent::share($request), [
|
|
'auth' => [
|
|
'user' => $user ? $this->serializeUser($user) : null,
|
|
'permissions' => fn () => $user
|
|
? $user->role->cachedPermissions()->pluck('name')->all()
|
|
: [],
|
|
'impersonating' => app('impersonate')->isImpersonating(),
|
|
],
|
|
|
|
'settings' => $this->settings(),
|
|
|
|
'config' => [
|
|
// Drives the "Active Sessions" UI, which only works on the
|
|
// database session driver.
|
|
'databaseSessions' => config('session.driver') === 'database',
|
|
'dateFormat' => config('app.date_format'),
|
|
'dateTimeFormat' => config('app.date_time_format'),
|
|
],
|
|
|
|
'flash' => [
|
|
'success' => $request->session()->get('success'),
|
|
'error' => $request->session()->get('error'),
|
|
],
|
|
|
|
'locale' => app()->getLocale(),
|
|
'locales' => collect(Locale::AVAILABLE_LOCALES)
|
|
->map(fn (string $locale) => [
|
|
'code' => $locale,
|
|
'flag' => Locale::flagUrl($locale),
|
|
])->all(),
|
|
'translations' => fn () => Translations::catalog(),
|
|
|
|
'vanguard' => [
|
|
'sidebar' => fn () => $user ? $this->sidebar($user) : [],
|
|
'slots' => fn () => [
|
|
'navbar:items' => Vanguard::resolveSlot('navbar:items'),
|
|
'navbar:dropdown' => Vanguard::resolveSlot('navbar:dropdown'),
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* The authenticated user, including the presenter derived fields that the
|
|
* Blade layer used to read through $user->present().
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function serializeUser(User $user): array
|
|
{
|
|
return [
|
|
'id' => $user->id,
|
|
'email' => $user->email,
|
|
'username' => $user->username,
|
|
'first_name' => $user->first_name,
|
|
'last_name' => $user->last_name,
|
|
'status' => $user->status,
|
|
'name' => $user->present()->name,
|
|
'nameOrEmail' => $user->present()->nameOrEmail,
|
|
'avatar' => $user->present()->avatar,
|
|
'lastLogin' => $user->present()->lastLogin,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function settings(): array
|
|
{
|
|
$settings = [];
|
|
|
|
foreach (self::SHARED_SETTINGS as $key) {
|
|
$settings[$key] = setting($key);
|
|
}
|
|
|
|
return $settings;
|
|
}
|
|
|
|
/**
|
|
* The authorised sidebar tree contributed by every registered plugin.
|
|
*
|
|
* @return array<int, mixed>
|
|
*/
|
|
private function sidebar(User $user): array
|
|
{
|
|
return collect(Vanguard::availablePlugins())
|
|
->map(fn (Plugin $plugin) => $plugin->sidebar())
|
|
->filter(fn (?Item $item) => $item !== null && $item->authorize($user))
|
|
->map(fn (Item $item) => $item->toArray($user))
|
|
->values()
|
|
->all();
|
|
}
|
|
}
|