Rollen ausschließen: bei Inhalten (Zielgruppen) und für ganze Module
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m45s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m25s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m21s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m49s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m59s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m21s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m30s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-26 19:30:11 +00:00
parent 19193b850b
commit 1877a0922f
113 changed files with 699 additions and 428 deletions

View file

@ -4,6 +4,7 @@ namespace App\Support;
use App\Models\User;
use App\Plugins\Hooks;
use App\Plugins\PluginAccess;
use Closure;
use Illuminate\Http\Request;
@ -131,6 +132,7 @@ class Navigation
$items = collect($this->items)
->filter(fn (array $item) => $item['ability'] === null || ($user?->can($item['ability']) ?? false))
->filter(fn (array $item) => $item['visible'] === null || ($user !== null && ($item['visible'])($user)))
->filter(fn (array $item, string $key) => app(PluginAccess::class)->allows($key, $user))
->map(fn (array $item, string $key) => [
'key' => $key,
'label' => $item['label'],
@ -139,7 +141,11 @@ class Navigation
'sort' => $item['sort'],
]);
foreach ($this->providers as $provider) {
foreach ($this->providers as $key => $provider) {
if (! app(PluginAccess::class)->allows($key, $user)) {
continue;
}
$items = $items->merge($provider($request, $user));
}
@ -226,6 +232,7 @@ class Navigation
$item = $this->items[$key] ?? null;
if (! $item
|| ! app(PluginAccess::class)->allows($key, $user)
|| ($item['ability'] !== null && ! ($user?->can($item['ability']) ?? false))
|| ($item['visible'] !== null && ($user === null || ! ($item['visible'])($user)))) {
return null;

View file

@ -10,8 +10,9 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
/**
* Zielgruppen: Ein Objekt ohne Regeln sehen alle. Mit Regeln sieht es, wer
* mindestens eine erfüllt (Rolle, Abteilung oder Standort). Die Prüfung ist eine
* indizierte SQL-Bedingung – auch bei tausenden Personen schnell.
* mindestens eine erfüllt (Rolle, Abteilung oder Standort). Ausgeschlossene Rollen
* („exclude_role“) sehen es nie. Die Prüfung ist eine indizierte SQL-Bedingung –
* auch bei tausenden Personen schnell.
*/
class Audiences
{
@ -26,6 +27,7 @@ class Audiences
$query->where(function (Builder $q) use ($type, $column, $user) {
$q->whereNotExists(fn (QueryBuilder $sub) => $sub->from('audiences')
->where('audiences.resource_type', $type)
->whereIn('audiences.rule_type', Audience::INCLUDE_TYPES)
->whereColumn('audiences.resource_id', $column));
if ($user) {
@ -35,21 +37,23 @@ class Audiences
->where(fn (QueryBuilder $rules) => self::matching($rules, $user)));
}
});
if ($user) {
$query->whereNotExists(fn (QueryBuilder $sub) => $sub->from('audiences')
->where('audiences.resource_type', $type)
->whereColumn('audiences.resource_id', $column)
->where('audiences.rule_type', 'exclude_role')
->whereIn('audiences.rule_value', array_map('strval', $user->permissions()->roleIds) ?: ['-']));
}
}
public static function allows(string $type, int $id, ?User $user): bool
{
$rules = Audience::query()->where('resource_type', $type)->where('resource_id', $id);
if (! (clone $rules)->exists()) {
return true;
}
return $user !== null && $rules->where(fn ($q) => self::matching($q->getQuery(), $user))->exists();
return self::matches(self::get($type, $id), $user);
}
/**
* @param array{role?: list<int|string>, department?: list<string>, location?: list<string>} $rules
* @param array{role?: list<int|string>, department?: list<string>, location?: list<string>, exclude_role?: list<int|string>} $rules
*/
public static function sync(string $type, int $id, array $rules): void
{
@ -63,11 +67,11 @@ class Audiences
}
/**
* @return array{role: list<string>, department: list<string>, location: list<string>}
* @return array{role: list<string>, department: list<string>, location: list<string>, exclude_role: list<string>}
*/
public static function get(string $type, int $id): array
{
$rules = ['role' => [], 'department' => [], 'location' => []];
$rules = ['role' => [], 'department' => [], 'location' => [], 'exclude_role' => []];
foreach (Audience::query()->where('resource_type', $type)->where('resource_id', $id)->get() as $audience) {
$rules[$audience->rule_type][] = $audience->rule_value;
@ -79,14 +83,14 @@ class Audiences
/**
* Alle Regeln eines Typs auf einmal – für gecachte Strukturen wie den Seitenbaum.
*
* @return array<int, array{role: list<string>, department: list<string>, location: list<string>}> ID => Regeln
* @return array<int, array{role: list<string>, department: list<string>, location: list<string>, exclude_role: list<string>}> ID => Regeln
*/
public static function allOfType(string $type): array
{
$result = [];
foreach (Audience::query()->where('resource_type', $type)->get(['resource_id', 'rule_type', 'rule_value']) as $audience) {
$result[$audience->resource_id] ??= ['role' => [], 'department' => [], 'location' => []];
$result[$audience->resource_id] ??= ['role' => [], 'department' => [], 'location' => [], 'exclude_role' => []];
$result[$audience->resource_id][$audience->rule_type][] = $audience->rule_value;
}
@ -96,13 +100,19 @@ class Audiences
/**
* Dieselbe Prüfung wie constrain()/allows(), aber in PHP auf bereits geladenen Regeln.
*
* @param array{role?: list<int|string>, department?: list<string>, location?: list<string>}|null $rules
* @param array{role?: list<int|string>, department?: list<string>, location?: list<string>, exclude_role?: list<int|string>}|null $rules
*/
public static function matches(?array $rules, ?User $user): bool
{
$rules = array_filter((array) $rules);
$include = array_intersect_key($rules, array_flip(Audience::INCLUDE_TYPES));
$roleIds = $user ? array_map('strval', $user->permissions()->roleIds) : [];
if ($rules === []) {
if (array_intersect(array_map('strval', $rules['exclude_role'] ?? []), $roleIds) !== []) {
return false;
}
if ($include === []) {
return true;
}
@ -110,11 +120,9 @@ class Audiences
return false;
}
$roleIds = array_map('strval', $user->permissions()->roleIds);
return array_intersect(array_map('strval', $rules['role'] ?? []), $roleIds) !== []
|| (filled($user->department) && in_array($user->department, $rules['department'] ?? [], true))
|| (filled($user->location) && in_array($user->location, $rules['location'] ?? [], true));
return array_intersect(array_map('strval', $include['role'] ?? []), $roleIds) !== []
|| (filled($user->department) && in_array($user->department, $include['department'] ?? [], true))
|| (filled($user->location) && in_array($user->location, $include['location'] ?? [], true));
}
private static function matching(QueryBuilder $rules, User $user): void