better impersonate

This commit is contained in:
Andras Bacsai
2024-12-16 12:48:09 +01:00
parent a3e53fc3aa
commit a0ad4bc90e
3 changed files with 37 additions and 17 deletions

View File

@@ -21,16 +21,28 @@ class Index extends Component
public function mount() public function mount()
{ {
if (! isCloud()) { if (! isCloud() && ! isDev()) {
return redirect()->route('dashboard'); return redirect()->route('dashboard');
} }
if (Auth::id() !== 0 && ! session('impersonating')) {
if (Auth::id() !== 0) {
return redirect()->route('dashboard'); return redirect()->route('dashboard');
} }
$this->getSubscribers(); $this->getSubscribers();
} }
public function back()
{
if (session('impersonating')) {
session()->forget('impersonating');
$user = User::find(0);
$team_to_switch_to = $user->teams->first();
Auth::login($user);
refreshSession($team_to_switch_to);
return redirect(request()->header('Referer'));
}
}
public function submitSearch() public function submitSearch()
{ {
if ($this->search !== '') { if ($this->search !== '') {
@@ -52,9 +64,10 @@ class Index extends Component
if (Auth::id() !== 0) { if (Auth::id() !== 0) {
return redirect()->route('dashboard'); return redirect()->route('dashboard');
} }
session(['impersonating' => true]);
$user = User::find($user_id); $user = User::find($user_id);
$team_to_switch_to = $user->teams->first(); $team_to_switch_to = $user->teams->first();
Cache::forget("team:{$user->id}"); // Cache::forget("team:{$user->id}");
Auth::login($user); Auth::login($user);
refreshSession($team_to_switch_to); refreshSession($team_to_switch_to);

View File

@@ -340,17 +340,19 @@
</li> </li>
@endif @endif
@if (isCloud() && isInstanceAdmin()) @if (isCloud() || isDev())
<li> @if (isInstanceAdmin() || session('impersonating'))
<a title="Admin" class="menu-item" href="/admin"> <li>
<svg class="text-pink-600 icon" viewBox="0 0 256 256" <a title="Admin" class="menu-item" href="/admin">
xmlns="http://www.w3.org/2000/svg"> <svg class="text-pink-600 icon" viewBox="0 0 256 256"
<path fill="currentColor" xmlns="http://www.w3.org/2000/svg">
d="M177.62 159.6a52 52 0 0 1-34 34a12.2 12.2 0 0 1-3.6.55a12 12 0 0 1-3.6-23.45a28 28 0 0 0 18.32-18.32a12 12 0 0 1 22.9 7.2ZM220 144a92 92 0 0 1-184 0c0-28.81 11.27-58.18 33.48-87.28a12 12 0 0 1 17.9-1.33l19.69 19.11L127 19.89a12 12 0 0 1 18.94-5.12C168.2 33.25 220 82.85 220 144m-24 0c0-41.71-30.61-78.39-52.52-99.29l-20.21 55.4a12 12 0 0 1-19.63 4.5L80.71 82.36C67 103.38 60 124.06 60 144a68 68 0 0 0 136 0" /> <path fill="currentColor"
</svg> d="M177.62 159.6a52 52 0 0 1-34 34a12.2 12.2 0 0 1-3.6.55a12 12 0 0 1-3.6-23.45a28 28 0 0 0 18.32-18.32a12 12 0 0 1 22.9 7.2ZM220 144a92 92 0 0 1-184 0c0-28.81 11.27-58.18 33.48-87.28a12 12 0 0 1 17.9-1.33l19.69 19.11L127 19.89a12 12 0 0 1 18.94-5.12C168.2 33.25 220 82.85 220 144m-24 0c0-41.71-30.61-78.39-52.52-99.29l-20.21 55.4a12 12 0 0 1-19.63 4.5L80.71 82.36C67 103.38 60 124.06 60 144a68 68 0 0 0 136 0" />
Admin </svg>
</a> Admin
</li> </a>
</li>
@endif
@endif @endif
<div class="flex-1"></div> <div class="flex-1"></div>
@if (isInstanceAdmin() && !isCloud()) @if (isInstanceAdmin() && !isCloud())

View File

@@ -1,7 +1,12 @@
<div> <div>
<h1>Admin Dashboard</h1> <h1>Admin Dashboard</h1>
<h3 class="pt-4">Who am I now?</h3> <div class="flex gap-2 pt-4">
<div class="pb-4">{{ auth()->user()->name }}</div> <h3>Who am I now?</h3>
@if (session('impersonating'))
<x-forms.button wire:click="back">Go back to root</x-forms.button>
@endif
</div>
<div class="pb-4">{{ auth()->user()->name }} ({{ auth()->user()->email }})</div>
<form wire:submit="submitSearch" class="flex flex-col gap-2 lg:flex-row"> <form wire:submit="submitSearch" class="flex flex-col gap-2 lg:flex-row">
<x-forms.input wire:model="search" placeholder="Search for a user" /> <x-forms.input wire:model="search" placeholder="Search for a user" />
<x-forms.button type="submit">Search</x-forms.button> <x-forms.button type="submit">Search</x-forms.button>