v0.9.3
This commit is contained in:
commit
22f164046e
286 files changed
+19176
No files matched your search
@@ -0,0 +1,84 @@
|
||||
<?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; everyone else the simple face, unless something is stored for
|
||||
* the account: an administrator sets it on the accounts screen, an account with `manager.switch` (or `manager.full`)
|
||||
* sets it itself. What is stored wins; only the account's own switch needs the permission.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
// What is stored wins: an administrator sets it for an account, an account that may switch sets it itself.
|
||||
$stored = UserSetting::value($user, 'view');
|
||||
|
||||
if (in_array($stored, [self::FULL, self::SIMPLE], true)) {
|
||||
return $stored;
|
||||
}
|
||||
|
||||
return $this->hasFull($user) ? 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' => $simple ? ($item['simple_label'] ?? $item['label']) : $item['label'],
|
||||
'href' => isset($item['route']) ? route($item['route'], $item['params'] ?? [], false) : (string) $item['url'],
|
||||
// The simple face is a short flat list: headings are for the full face.
|
||||
'section' => $simple ? null : ($item['section'] ?? null),
|
||||
'hint' => $item['hint'] ?? 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');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user