This commit is contained in:
Julian Prieber 2023-07-14 18:18:41 +02:00
parent b3e0b36dba
commit 4652bc3e5e
1 changed files with 55 additions and 55 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace App\Http\Middleware; namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use App\Models\User; use App\Models\User;
@ -16,47 +17,42 @@ class Impersonate
})->first(); })->first();
if ($adminUser && is_numeric($adminUser->auth_as)) { if ($adminUser && is_numeric($adminUser->auth_as)) {
$originalUserId = $adminUser->id;
$impersonateUserId = is_numeric($adminUser->auth_as) ? $adminUser->auth_as : $adminUser->id;
$impersonateUser = User::find($impersonateUserId);
$impersonateUserName = $impersonateUser->name;
$originalUser = $adminUser->id; if (Auth::user()->id === $originalUserId) {
$id = is_numeric($adminUser->auth_as) ? $adminUser->auth_as : $adminUser->id;
$user = User::find($id);
$name = $user->name;
if(Auth::user()->id === $originalUser) {
// Generate unique token
$token = Str::random(60); $token = Str::random(60);
if(\Route::currentRouteName() !== 'authAs'){ if (\Route::currentRouteName() !== 'authAs') {
$adminUser->remember_token = $token; $adminUser->remember_token = $token;
$adminUser->save(); $adminUser->save();
echo "<script>window.location.href = '".url('studio/links')."';</script>"; echo "<script>window.location.href = '" . url('studio/links') . "';</script>";
} }
Auth::loginUsingId($id); Auth::loginUsingId($impersonateUserId);
$request->session()->put('display_auth_nav', $token); $request->session()->put('display_auth_nav', $token);
$request->session()->save(); $request->session()->save();
} }
if($request->session()->has('display_auth_nav')) { if ($request->session()->has('display_auth_nav')) {
$dashboard = url('dashboard'); $dashboardUrl = url('dashboard');
$URL = url('/auth-as'); $authAsUrl = url('/auth-as');
$csrf = csrf_token(); $csrfToken = csrf_token();
$remember_token = User::find($originalUser); $rememberTokenUser = User::find($originalUserId);
$token = $remember_token->remember_token; $rememberToken = $rememberTokenUser->remember_token;
$storageToken = $request->session()->get('display_auth_nav'); $storageToken = $request->session()->get('display_auth_nav');
if($storageToken === $token) {
if (file_exists(base_path(findAvatar($id)))) {
$img = '<img alt="avatar" class="iimg irounded" src="' . url(findAvatar($id)) . '">';
} elseif (file_exists(base_path("assets/linkstack/images/").findFile('avatar'))) {
$img = '<img alt="avatar" class="iimg irounded" src="' . url("assets/linkstack/images/") . "/" . findFile('avatar') . '">';
} else {
$img = '<img alt="avatar" class="iimg" src="' . asset('assets/linkstack/images/logo.svg') . '">';
}
$customHtml =
<<<EOD
if ($storageToken === $rememberToken) {
if (file_exists(base_path(findAvatar($impersonateUserId)))) {
$avatarUrl = url(findAvatar($impersonateUserId));
} elseif (file_exists(base_path("assets/linkstack/images/") . findFile('avatar'))) {
$avatarUrl = url("assets/linkstack/images/") . "/" . findFile('avatar');
} else {
$avatarUrl = asset('assets/linkstack/images/logo.svg');
}
$customHtml = <<<EOD
<style> <style>
.ibar { .ibar {
position: fixed; position: fixed;
@ -117,7 +113,7 @@ $customHtml =
<div class="ibar"> <div class="ibar">
<p class="itext1"> <p class="itext1">
<span> <span>
<a href="$dashboard">$img $name</a> <a href="$dashboardUrl"><img alt="avatar" class="iimg irounded" src="$avatarUrl">$impersonateUserName</a>
</span> </span>
<a style="cursor:pointer" onclick="document.getElementById('submitForm').submit(); return false;"> <a style="cursor:pointer" onclick="document.getElementById('submitForm').submit(); return false;">
<svg xmlns="http://www.w3.org/2000/svg" class="bi bi-x" viewBox="0 0 16 16"> <svg xmlns="http://www.w3.org/2000/svg" class="bi bi-x" viewBox="0 0 16 16">
@ -129,10 +125,10 @@ $customHtml =
</p> </p>
</div> </div>
<form id="submitForm" action="$URL" method="POST" style="display: none;"> <form id="submitForm" action="$authAsUrl" method="POST" style="display: none;">
<input type="hidden" name="_token" value="$csrf"> <input type="hidden" name="_token" value="$csrfToken">
<input type="hidden" name="token" value="$token"> <input type="hidden" name="token" value="$rememberToken">
<input type="hidden" name="id" value="$originalUser"> <input type="hidden" name="id" value="$originalUserId">
</form> </form>
<script> <script>
@ -140,9 +136,10 @@ $customHtml =
document.getElementById('submitForm').submit(); document.getElementById('submitForm').submit();
} }
</script> </script>
EOD;
EOD;; } else {
} else {$customHtml = "";} $customHtml = "";
}
$response = $next($request); $response = $next($request);
$content = $response->getContent(); $content = $response->getContent();
@ -151,11 +148,14 @@ EOD;;
return $response; return $response;
} else { } else {
if($request->session()->has('display_auth_nav')) { if ($request->session()->has('display_auth_nav')) {
$request->session()->forget('display_auth_nav'); $request->session()->forget('display_auth_nav');
Auth::logout(); Auth::logout();
} }
return $next($request); return $next($request);
}}else{return $next($request);} }
} else {
return $next($request);
}
} }
} }