112 lines
3.4 KiB
PHP
112 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Utilities;
|
|
|
|
use App\Models\Auth\User;
|
|
|
|
class BusinessOperationPermissions
|
|
{
|
|
/**
|
|
* @return list<array{
|
|
* key:string,
|
|
* section:string,
|
|
* page:string,
|
|
* page_title:string,
|
|
* operation:string,
|
|
* title:string,
|
|
* description:string
|
|
* }>
|
|
*/
|
|
public static function definitions(): array
|
|
{
|
|
$definitions = [];
|
|
|
|
foreach (BusinessNavigation::all() as $section) {
|
|
foreach ($section['pages'] ?? [] as $page) {
|
|
$pages = [$page, ...($page['children'] ?? [])];
|
|
|
|
foreach ($pages as $entry) {
|
|
foreach ($entry['operations'] ?? [] as $operation) {
|
|
$sectionSlug = (string) $section['slug'];
|
|
$pageSlug = (string) $entry['slug'];
|
|
$operationSlug = (string) $operation['slug'];
|
|
$definitions[] = [
|
|
'key' => "{$sectionSlug}/{$pageSlug}/{$operationSlug}",
|
|
'section' => $sectionSlug,
|
|
'page' => $pageSlug,
|
|
'page_title' => (string) ($entry['title'] ?? $pageSlug),
|
|
'operation' => $operationSlug,
|
|
'title' => (string) ($operation['title'] ?? $operationSlug),
|
|
'description' => (string) ($operation['description'] ?? ''),
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $definitions;
|
|
}
|
|
|
|
/** @return array<string, list<array<string, string>>> */
|
|
public static function groupedBySection(): array
|
|
{
|
|
$grouped = [];
|
|
|
|
foreach (self::definitions() as $definition) {
|
|
$grouped[$definition['section']][] = $definition;
|
|
}
|
|
|
|
return $grouped;
|
|
}
|
|
|
|
public static function can(
|
|
string $section,
|
|
string $page,
|
|
string $operation,
|
|
?User $actor = null,
|
|
): bool {
|
|
$actor ??= user();
|
|
$definition = self::definition($section, $page, $operation);
|
|
|
|
if (
|
|
! $definition
|
|
|| ! $actor
|
|
|| ! $actor->enabled
|
|
|| ! $actor->companies()->where('companies.id', company_id())->exists()
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
$sectionConfig = collect(SidebarPermissions::navigationFor($actor))
|
|
->firstWhere('slug', $definition['section']);
|
|
$visiblePages = collect($sectionConfig['pages'] ?? [])
|
|
->flatMap(static fn (array $configuredPage): array => [
|
|
$configuredPage,
|
|
...($configuredPage['children'] ?? []),
|
|
]);
|
|
|
|
if (! $visiblePages->contains('slug', $definition['page'])) {
|
|
return false;
|
|
}
|
|
|
|
if (in_array(SystemAccess::roleName($actor), [SystemAccess::SUPER_ADMIN, SystemAccess::ADMIN], true)) {
|
|
return true;
|
|
}
|
|
|
|
$allowed = SidebarPermissions::allowedKeys($actor);
|
|
|
|
return $allowed === null || array_intersect([
|
|
$definition['key'],
|
|
$definition['section'],
|
|
], $allowed) !== [];
|
|
}
|
|
|
|
/** @return array<string, string>|null */
|
|
private static function definition(string $section, string $page, string $operation): ?array
|
|
{
|
|
$key = "{$section}/{$page}/{$operation}";
|
|
|
|
return collect(self::definitions())->firstWhere('key', $key);
|
|
}
|
|
}
|