Profil zeigt geschriebene Beiträge, Als Person anmelden (0.27.1)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m40s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m0s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m51s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m1s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m45s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m36s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m21s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m40s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m0s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m51s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m1s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m45s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m36s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m21s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
1469f50401
commit
546c94db1c
127 changed files with 643 additions and 337 deletions
74
app/Support/Impersonation.php
Normal file
74
app/Support/Impersonation.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* „Als Benutzer anmelden“: Die Administration sieht das Portal mit den Rechten einer
|
||||
* anderen Person, um Ansicht und Rechte zu prüfen. Jeder Wechsel wird protokolliert;
|
||||
* zurück geht es über das Band oben („Zurück zu meinem Konto“).
|
||||
*/
|
||||
class Impersonation
|
||||
{
|
||||
public const SESSION = 'impersonator_id';
|
||||
|
||||
public function canImpersonate(?User $actor, User $target): bool
|
||||
{
|
||||
return $actor !== null
|
||||
&& ! $this->active()
|
||||
&& $actor->can('users.manage')
|
||||
&& $target->is_active
|
||||
&& ! $target->is($actor)
|
||||
// Vollzugriff nur, wer selbst Vollzugriff hat
|
||||
&& (! $target->permissions()->isSuperuser() || $actor->permissions()->isSuperuser());
|
||||
}
|
||||
|
||||
public function start(User $actor, User $target): void
|
||||
{
|
||||
if (! $this->canImpersonate($actor, $target)) {
|
||||
throw new RuntimeException('Anmelden als diese Person ist nicht erlaubt.');
|
||||
}
|
||||
|
||||
Audit::log('user.impersonation_started', ['target' => $target->id, 'name' => $target->name]);
|
||||
$this->switchTo($target);
|
||||
session()->put(self::SESSION, $actor->id);
|
||||
}
|
||||
|
||||
public function stop(): ?User
|
||||
{
|
||||
$original = $this->impersonator();
|
||||
|
||||
if (! $original) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$target = Auth::user();
|
||||
session()->forget(self::SESSION);
|
||||
$this->switchTo($original);
|
||||
Audit::log('user.impersonation_stopped', ['target' => $target?->id, 'name' => $target?->name]);
|
||||
|
||||
return $original;
|
||||
}
|
||||
|
||||
public function active(): bool
|
||||
{
|
||||
return session()->has(self::SESSION);
|
||||
}
|
||||
|
||||
public function impersonator(): ?User
|
||||
{
|
||||
$id = session()->get(self::SESSION);
|
||||
|
||||
return $id ? User::query()->find($id) : null;
|
||||
}
|
||||
|
||||
private function switchTo(User $user): void
|
||||
{
|
||||
Auth::guard('web')->login($user);
|
||||
// Sitzungsprüfung (Passwort-Fingerabdruck) auf die neue Person umstellen
|
||||
session()->forget('password_hash_web');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue