Files
mareike/app/Providers/AuthCheckProvider.php
T
2026-06-20 18:02:00 +02:00

68 lines
1.8 KiB
PHP

<?php
namespace App\Providers;
use App\Enumerations\UserRole;
use App\Models\User;
class AuthCheckProvider {
public function checkLoggedIn() : bool {
if (!auth()->check()) {
return false;
}
$user = auth()->user();
$tenant = app('tenant');
if ($tenant->slug === 'lv') {
return $user->active;
}
if ($this->isMainAdministrator($user)) {
return true;
}
return $user->active && $tenant->slug === $user->local_group;
}
public function getUserRole() : ?string {
if (!$this->checkLoggedIn()) {
return null;
}
$user = auth()->user();
if (app('tenant')->slug === 'lv') {
return $user->user_role_main;
}
// "Bundesrecht steht über Landesrecht":
// Ein ROLE_ADMINISTRATOR auf LV-Ebene ist auf jedem Sub-Tenant automatisch Administrator,
// unabhängig von user_role_local_group.
if ($this->isMainAdministrator($user)) {
return UserRole::USER_ROLE_ADMIN;
}
return $user->user_role_local_group;
}
/**
* Gibt true zurück, wenn der Nutzer auf LV-Ebene Administrator ist.
* Diese Rolle hebt das lokale Rechtesystem für alle Sub-Tenants auf.
*/
public function isMainAdministrator(?User $user = null) : bool {
$user ??= auth()->user();
return $user !== null
&& $user->user_role_main === UserRole::USER_ROLE_ADMIN;
}
/**
* Bequemer Helper für die Berechtigungs-Checks im gesamten System.
* Gibt true zurück, wenn der aktuell eingeloggte Nutzer im Kontext des
* aktuellen Tenants effektiv Administrator ist.
*/
public function isAdministrator() : bool {
return $this->getUserRole() === UserRole::USER_ROLE_ADMIN;
}
}