12-05-2026 Frontend dev
This commit is contained in:
parent
405df0a122
commit
5b8bdf4182
779 changed files with 480564 additions and 6241 deletions
93
app/Actions/Admin/UserImpersonation.php
Normal file
93
app/Actions/Admin/UserImpersonation.php
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace App\Actions\Admin;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class UserImpersonation
|
||||
{
|
||||
public const SessionKey = 'impersonate_from';
|
||||
|
||||
public function canInitiate(User $user): bool
|
||||
{
|
||||
return $user->is_super_admin || $user->can('users:manage');
|
||||
}
|
||||
|
||||
public function canStart(User $admin, User $target): bool
|
||||
{
|
||||
return $this->canInitiate($admin)
|
||||
&& ! $admin->is($target)
|
||||
&& ! $this->isActive()
|
||||
&& $this->canBeImpersonated($target);
|
||||
}
|
||||
|
||||
public function canBeImpersonated(User $target): bool
|
||||
{
|
||||
return $target->canAccessCustomer();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AuthorizationException
|
||||
*/
|
||||
public function start(User $admin, User $target): void
|
||||
{
|
||||
if (! $this->canInitiate($admin)) {
|
||||
throw new AuthorizationException(__('Du darfst keine Benutzer-Impersonation starten.'));
|
||||
}
|
||||
|
||||
if ($admin->is($target)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->isActive()) {
|
||||
throw new AuthorizationException(__('Verschachtelte Benutzer-Impersonation ist nicht erlaubt.'));
|
||||
}
|
||||
|
||||
if (! $this->canBeImpersonated($target)) {
|
||||
throw new AuthorizationException(__('Dieser Benutzer kann nicht im Panel angemeldet werden.'));
|
||||
}
|
||||
|
||||
session([self::SessionKey => $admin->getKey()]);
|
||||
|
||||
Auth::login($target);
|
||||
}
|
||||
|
||||
public function stop(): ?User
|
||||
{
|
||||
$adminUserId = session(self::SessionKey);
|
||||
|
||||
session()->forget(self::SessionKey);
|
||||
|
||||
if (! is_numeric($adminUserId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$admin = User::query()->find((int) $adminUserId);
|
||||
|
||||
if (! $admin?->canAccessAdmin()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Auth::login($admin);
|
||||
|
||||
return $admin;
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return session()->has(self::SessionKey);
|
||||
}
|
||||
|
||||
public function impersonator(): ?User
|
||||
{
|
||||
$adminUserId = session(self::SessionKey);
|
||||
|
||||
if (! is_numeric($adminUserId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return User::query()->find((int) $adminUserId);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue