Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
||
|
||
namespace App\Support\Permissions;
|
||
|
||
use App\Models\Role;
|
||
use App\Models\User;
|
||
|
||
/**
|
||
* Schutz vor Rechte-Ausweitung: Wer keinen Vollzugriff hat, darf nur Rollen vergeben
|
||
* bzw. Rechte eintragen, die er selbst besitzt – nie „Administration“.
|
||
*/
|
||
final class RoleGuard
|
||
{
|
||
public static function canAssign(?User $actor, Role $role): bool
|
||
{
|
||
if (! $actor) {
|
||
return false;
|
||
}
|
||
|
||
if ($actor->permissions()->isSuperuser()) {
|
||
return true;
|
||
}
|
||
|
||
if ($role->isAdministration() || $role->grants()->where('ability', '*')->exists()) {
|
||
return false;
|
||
}
|
||
|
||
return $role->grants()->whereNull('resource_type')->pluck('ability')
|
||
->every(fn (string $ability) => $actor->can($ability));
|
||
}
|
||
|
||
public static function canGrant(?User $actor, string $ability): bool
|
||
{
|
||
return $actor !== null && ($actor->permissions()->isSuperuser() || ($ability !== '*' && $actor->can($ability)));
|
||
}
|
||
|
||
/**
|
||
* Konten mit Vollzugriff bearbeiten (Passwort, E-Mail, Rollen, Löschen) nur mit eigenem Vollzugriff.
|
||
*/
|
||
public static function canManageUser(?User $actor, User $target): bool
|
||
{
|
||
return $actor !== null && ($actor->permissions()->isSuperuser() || ! $target->permissions()->isSuperuser());
|
||
}
|
||
}
|