Rollen, Rechte und Zielgruppen
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m30s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m28s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m36s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 1m45s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 1m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 1m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 1m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 54s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m30s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m28s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m36s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 1m45s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 1m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 1m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 1m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 54s
- Rollen mit globalen Rechten und Rechten für einzelne Objekte (z. B. „Beiträge schreiben in Kategorie MAV-Info“); Rollen sind additiv, Zuweisungen merken sich ihre Herkunft (manuell oder später Identity-Provider-Gruppe) - Systemrolle „Administration“ ersetzt users.is_admin (Migration übernimmt bestehende Administratoren); nicht löschbar, man kann sie sich nicht selbst entziehen - Rechte je Person vorberechnet und gecacht, Versionszähler invalidiert bei Änderungen - Gate-Anbindung: registrierte Rechte entscheidet das Rechtesystem, Objektregeln die Policies; Admin-Seiten und Plugin-Ressourcen hinter passenden Rechten - Redaktion nach Kategorie: Schreiben und Veröffentlichen getrennt, Liste im Admin auf eigene Kategorien beschränkt, ohne Veröffentlichungsrecht bleibt ein Beitrag Entwurf - Zielgruppen (Rolle/Abteilung/Standort) für Kategorien und Termine als indizierte SQL-Bedingung in Listen, Detailseiten, Kacheln und .ics-Export - Admin: Rollen-Verwaltung, „Rechte prüfen“, Redaktion und Sichtbarkeit direkt an Kategorie bzw. Termin - Kern-Ressourcen explizit registriert, damit nach Updates liegengebliebene alte Dateien nicht doppelt geladen werden - docs/PLUGINS.md für Plugin-Entwicklung; Demo-Rolle „MAV“ Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
28ef1e4a20
commit
9ddc35a62c
112 changed files with 2408 additions and 267 deletions
|
|
@ -19,7 +19,6 @@ class Abilities
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
$this->define('admin.access', 'Administrationsbereich öffnen', 'System');
|
||||
$this->define('users.manage', 'Benutzer verwalten', 'System');
|
||||
$this->define('roles.manage', 'Rollen und Rechte verwalten', 'System');
|
||||
$this->define('plugins.manage', 'Plugins verwalten und installieren', 'System');
|
||||
|
|
|
|||
93
app/Support/Permissions/Audiences.php
Normal file
93
app/Support/Permissions/Audiences.php
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\Permissions;
|
||||
|
||||
use App\Models\Audience;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
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.
|
||||
*/
|
||||
class Audiences
|
||||
{
|
||||
/**
|
||||
* Schränkt eine Abfrage auf Objekte ein, die die Person sehen darf.
|
||||
*
|
||||
* @param Builder<Model> $query
|
||||
* @param string $column Spalte mit der ID des Objekts, dessen Zielgruppe gilt
|
||||
*/
|
||||
public static function constrain(Builder $query, string $type, string $column, ?User $user): void
|
||||
{
|
||||
$query->where(function (Builder $q) use ($type, $column, $user) {
|
||||
$q->whereNotExists(fn (QueryBuilder $sub) => $sub->from('audiences')
|
||||
->where('audiences.resource_type', $type)
|
||||
->whereColumn('audiences.resource_id', $column));
|
||||
|
||||
if ($user) {
|
||||
$q->orWhereExists(fn (QueryBuilder $sub) => $sub->from('audiences')
|
||||
->where('audiences.resource_type', $type)
|
||||
->whereColumn('audiences.resource_id', $column)
|
||||
->where(fn (QueryBuilder $rules) => self::matching($rules, $user)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{role?: list<int|string>, department?: list<string>, location?: list<string>} $rules
|
||||
*/
|
||||
public static function sync(string $type, int $id, array $rules): void
|
||||
{
|
||||
Audience::query()->where('resource_type', $type)->where('resource_id', $id)->delete();
|
||||
|
||||
foreach (array_keys(Audience::TYPES) as $ruleType) {
|
||||
foreach (array_unique(array_filter((array) ($rules[$ruleType] ?? []), fn ($v) => $v !== null && $v !== '')) as $value) {
|
||||
Audience::query()->create(['resource_type' => $type, 'resource_id' => $id, 'rule_type' => $ruleType, 'rule_value' => (string) $value]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{role: list<string>, department: list<string>, location: list<string>}
|
||||
*/
|
||||
public static function get(string $type, int $id): array
|
||||
{
|
||||
$rules = ['role' => [], 'department' => [], 'location' => []];
|
||||
|
||||
foreach (Audience::query()->where('resource_type', $type)->where('resource_id', $id)->get() as $audience) {
|
||||
$rules[$audience->rule_type][] = $audience->rule_value;
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
private static function matching(QueryBuilder $rules, User $user): void
|
||||
{
|
||||
$roleIds = array_map('strval', $user->permissions()->roleIds);
|
||||
|
||||
$rules->where(fn (QueryBuilder $q) => $q->where('audiences.rule_type', 'role')->whereIn('audiences.rule_value', $roleIds ?: ['-']));
|
||||
|
||||
if (filled($user->department)) {
|
||||
$rules->orWhere(fn (QueryBuilder $q) => $q->where('audiences.rule_type', 'department')->where('audiences.rule_value', $user->department));
|
||||
}
|
||||
|
||||
if (filled($user->location)) {
|
||||
$rules->orWhere(fn (QueryBuilder $q) => $q->where('audiences.rule_type', 'location')->where('audiences.rule_value', $user->location));
|
||||
}
|
||||
}
|
||||
}
|
||||
36
app/Support/Permissions/PermissionResource.php
Normal file
36
app/Support/Permissions/PermissionResource.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\Permissions;
|
||||
|
||||
/**
|
||||
* Übersetzt die Argumente eines Rechte-Checks in Objekttyp und ID:
|
||||
* $user->can('news.post.create', $category) – Modell mit permissionResource()
|
||||
* $user->can('news.post.create', ['news_category', 5]) – Laravel reicht das als zwei Argumente durch
|
||||
*/
|
||||
final class PermissionResource
|
||||
{
|
||||
/**
|
||||
* @param array<int, mixed> $arguments
|
||||
* @return array{0: string|null, 1: int|null}
|
||||
*/
|
||||
public static function from(array $arguments): array
|
||||
{
|
||||
$first = $arguments[0] ?? null;
|
||||
|
||||
if (is_object($first) && method_exists($first, 'permissionResource')) {
|
||||
return $first->permissionResource();
|
||||
}
|
||||
|
||||
if (is_array($first)) {
|
||||
return self::from($first);
|
||||
}
|
||||
|
||||
if (is_string($first)) {
|
||||
$id = $arguments[1] ?? null;
|
||||
|
||||
return [$first, is_numeric($id) ? (int) $id : null];
|
||||
}
|
||||
|
||||
return [null, null];
|
||||
}
|
||||
}
|
||||
75
app/Support/Permissions/PermissionSet.php
Normal file
75
app/Support/Permissions/PermissionSet.php
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\Permissions;
|
||||
|
||||
/**
|
||||
* Vorberechnete Rechte einer Person (gecacht, siehe Permissions).
|
||||
*/
|
||||
final class PermissionSet
|
||||
{
|
||||
/**
|
||||
* @param list<int> $roleIds
|
||||
* @param array<string, true> $global Ability => true („*“ = alles)
|
||||
* @param array<string, array<string, list<int>|true>> $objects Ability => Typ => IDs oder true (alle)
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly array $roleIds,
|
||||
public readonly array $global,
|
||||
public readonly array $objects,
|
||||
) {}
|
||||
|
||||
public function isSuperuser(): bool
|
||||
{
|
||||
return isset($this->global['*']);
|
||||
}
|
||||
|
||||
public function can(string $ability, ?string $resourceType = null, ?int $resourceId = null): bool
|
||||
{
|
||||
if ($this->isSuperuser() || isset($this->global[$ability])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($resourceType === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ids = $this->objects[$ability][$resourceType] ?? null;
|
||||
|
||||
return $ids === true || ($ids !== null && ($resourceId === null ? $ids !== [] : in_array($resourceId, $ids, true)));
|
||||
}
|
||||
|
||||
/**
|
||||
* IDs der Objekte, für die das Recht gilt – für Abfragen („nur meine Kategorien“).
|
||||
*
|
||||
* @return list<int>|true true = alle
|
||||
*/
|
||||
public function ids(string $ability, string $resourceType): array|true
|
||||
{
|
||||
if ($this->isSuperuser() || isset($this->global[$ability])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->objects[$ability][$resourceType] ?? [];
|
||||
}
|
||||
|
||||
public function hasAnyObjectGrant(): bool
|
||||
{
|
||||
return $this->objects !== [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return ['roleIds' => $this->roleIds, 'global' => $this->global, 'objects' => $this->objects];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self($data['roleIds'], $data['global'], $data['objects']);
|
||||
}
|
||||
}
|
||||
72
app/Support/Permissions/Permissions.php
Normal file
72
app/Support/Permissions/Permissions.php
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\Permissions;
|
||||
|
||||
use App\Models\Grant;
|
||||
use App\Models\User;
|
||||
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 = [];
|
||||
|
||||
foreach (Grant::query()->whereIn('role_id', $roleIds)->get(['ability', 'resource_type', 'resource_id']) 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;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue