Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m9s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m40s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
- Seiten-Plugin: Seitenbaum mit Adressen, Seitennavigation, Brotkrumen und optional Hauptnavigation; Sichtbarkeit vererbt sich auf Unterseiten - Pagebuilder (Block-Registry im Kern, Plugins tragen Blöcke bei): Abschnitt, Spalten, Tabs, Akkordeon, Box, Überschrift, Text (TipTap), Zitat, Schaltflächen, Symbolliste, Tabelle, Trennlinie, Abstand, Bild, Galerie, Video (erst nach Klick), Titelbild, Ansprechpartner, Downloads, Unterseiten, Neuigkeiten, Termine; Sichtbarkeit je Block; Bereinigung beim Speichern - Editor im Portal: Ziehen und Ablegen, Autospeichern als Entwurf, Rückgängig, Gerätevorschau, Versionen, Zeitsteuerung, Bearbeitungssperre, Freigabe anfordern - Rechte „inkl. Unterseiten“ werden im Rechte-Cache aufgelöst - Neuigkeiten: Schreiben im Portal, Freigabe-Workflow mit Benachrichtigungen - Bereich „Redaktion“, Benachrichtigungen im Portal - storage/.migrations-fingerprint nicht mehr ausliefern Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
92 lines
3.3 KiB
PHP
92 lines
3.3 KiB
PHP
<?php
|
||
|
||
namespace App\Support\Permissions;
|
||
|
||
use App\Models\Grant;
|
||
use App\Models\User;
|
||
use App\Support\Abilities;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* Löst die effektiven Rechte einer Person auf: Vereinigung aller Rollen (manuell
|
||
* und über IdP-Gruppen) – es gibt keine Verbote. Das Ergebnis wird je Person
|
||
* gecacht; jede Änderung an Rollen, Rechten oder Zuweisungen erhöht die
|
||
* Version und macht damit alle Caches auf einen Schlag ungültig.
|
||
*/
|
||
class Permissions
|
||
{
|
||
private const VERSION_KEY = 'permissions.version';
|
||
|
||
/** @var array<int, PermissionSet> */
|
||
private array $resolved = [];
|
||
|
||
public function for(User $user): PermissionSet
|
||
{
|
||
if (! $user->is_active) {
|
||
return new PermissionSet([], [], []);
|
||
}
|
||
|
||
return $this->resolved[$user->id] ??= PermissionSet::fromArray(Cache::rememberForever(
|
||
'permissions.user.'.$user->id.'.'.self::version(),
|
||
fn () => $this->resolve($user)->toArray(),
|
||
));
|
||
}
|
||
|
||
public static function flush(): void
|
||
{
|
||
Cache::forever(self::VERSION_KEY, self::version() + 1);
|
||
app(self::class)->resolved = [];
|
||
}
|
||
|
||
public static function version(): int
|
||
{
|
||
return (int) Cache::get(self::VERSION_KEY, 1);
|
||
}
|
||
|
||
private function resolve(User $user): PermissionSet
|
||
{
|
||
$roleIds = DB::table('role_user')->where('user_id', $user->id)->distinct()->pluck('role_id')->map(fn ($id) => (int) $id)->all();
|
||
$global = [];
|
||
$objects = [];
|
||
$inherited = [];
|
||
|
||
foreach (Grant::query()->whereIn('role_id', $roleIds)->get(['ability', 'resource_type', 'resource_id', 'include_children']) as $grant) {
|
||
if ($grant->resource_type === null) {
|
||
$global[$grant->ability] = true;
|
||
} elseif ($grant->resource_id === null) {
|
||
$objects[$grant->ability][$grant->resource_type] = true;
|
||
} elseif (($objects[$grant->ability][$grant->resource_type] ?? null) !== true) {
|
||
$objects[$grant->ability][$grant->resource_type][] = $grant->resource_id;
|
||
|
||
if ($grant->include_children) {
|
||
$inherited[$grant->resource_type][$grant->ability][] = $grant->resource_id;
|
||
}
|
||
}
|
||
}
|
||
|
||
// „Inkl. Unterobjekten“: Nachfahren einmalig auflösen – die Prüfung bleibt ein Array-Zugriff.
|
||
// Hierarchische Typen leeren den Rechte-Cache, wenn sich ihr Baum ändert.
|
||
$types = app(Abilities::class)->resourceTypes();
|
||
|
||
foreach ($inherited as $type => $byAbility) {
|
||
$descendants = $types[$type]['descendants'] ?? null;
|
||
|
||
foreach ($descendants ? $byAbility : [] as $ability => $ids) {
|
||
if (is_array($objects[$ability][$type] ?? null)) {
|
||
$objects[$ability][$type] = array_merge($objects[$ability][$type], array_map('intval', $descendants($ids)));
|
||
}
|
||
}
|
||
}
|
||
|
||
foreach ($objects as $ability => $types) {
|
||
foreach ($types as $type => $ids) {
|
||
if (is_array($ids)) {
|
||
$objects[$ability][$type] = array_values(array_unique($ids));
|
||
}
|
||
}
|
||
}
|
||
|
||
return new PermissionSet($roleIds, $global, $objects);
|
||
}
|
||
}
|