PR CI / ledger (pull_request) Successful in 4s
PR CI / flow (pull_request) Successful in 4s
PR CI / ui (pull_request) Successful in 16s
PR CI / test (lowest) (pull_request) Successful in 44s
PR CI / test (stable) (pull_request) Successful in 49s
PR CI / security (pull_request) Successful in 50s
PR CI / pr-ci (pull_request) Successful in 2s
PR CI / release-automerge (pull_request) Successful in 4s
85 lines
2.8 KiB
PHP
85 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Support;
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
use IriksIT\Manager\Models\UserSetting;
|
|
|
|
/**
|
|
* The menu one account sees, and the face of the manager it looks at: the full one, or (when config
|
|
* `simple_view` is on) the simple one with only the items marked `simple`. An account with the permission
|
|
* `manager.full` (and a super admin) gets the full face; one with `manager.switch` starts in the simple
|
|
* face and may change; everyone else always gets the simple face. What it chose is remembered per account.
|
|
*/
|
|
final class Menu
|
|
{
|
|
public const FULL = 'full';
|
|
|
|
public const SIMPLE = 'simple';
|
|
|
|
public function __construct(private readonly ModuleRegistry $modules) {}
|
|
|
|
public function view(?object $user): string
|
|
{
|
|
if ($user === null || ! config('manager.simple_view')) {
|
|
return self::FULL;
|
|
}
|
|
|
|
$full = $this->hasFull($user);
|
|
|
|
if ($this->maySwitch($user)) {
|
|
$chosen = $user->getKey() === null ? null : UserSetting::query()->where('user_id', $user->getKey())->value('view');
|
|
|
|
if (in_array($chosen, [self::FULL, self::SIMPLE], true)) {
|
|
return $chosen;
|
|
}
|
|
}
|
|
|
|
return $full ? self::FULL : self::SIMPLE;
|
|
}
|
|
|
|
public function maySwitch(?object $user): bool
|
|
{
|
|
return $user !== null && config('manager.simple_view') && ($this->hasFull($user) || Gate::forUser($user)->allows('manager.switch'));
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $allowed the permissions this account has
|
|
* @return list<array{label: string, href: string, section?: string, icon?: string, new_tab?: bool}>
|
|
*/
|
|
public function for(?object $user, array $allowed): array
|
|
{
|
|
if ($user === null) {
|
|
return [];
|
|
}
|
|
|
|
$simple = $this->view($user) === self::SIMPLE;
|
|
$items = [];
|
|
|
|
foreach ($this->modules->navigation() as $item) {
|
|
if (($item['permission'] ?? null) !== null && ! in_array($item['permission'], $allowed, true)) {
|
|
continue;
|
|
}
|
|
|
|
if ($simple && ! $item['simple']) {
|
|
continue;
|
|
}
|
|
|
|
$items[] = array_filter([
|
|
'label' => $item['label'],
|
|
'href' => isset($item['route']) ? route($item['route'], $item['params'] ?? [], false) : (string) $item['url'],
|
|
'section' => $item['section'] ?? null,
|
|
'icon' => $item['icon'] ?? null,
|
|
'new_tab' => ($item['new_tab'] ?? false) ?: null,
|
|
], fn ($value) => $value !== null);
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
private function hasFull(object $user): bool
|
|
{
|
|
return (method_exists($user, 'isManagerSuperAdmin') && $user->isManagerSuperAdmin()) || Gate::forUser($user)->allows('manager.full');
|
|
}
|
|
}
|