|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Middleware; |
| 4 | +use Illuminate\Support\Facades\Auth; |
| 5 | +use Illuminate\Support\Str; |
| 6 | +use App\Models\User; |
| 7 | +use Closure; |
| 8 | + |
| 9 | +class Impersonate |
| 10 | +{ |
| 11 | + public function handle($request, Closure $next) |
| 12 | + { |
| 13 | + $adminUser = User::whereNotNull('auth_as')->where('role', 'admin')->first(); |
| 14 | + |
| 15 | + if ($adminUser) { |
| 16 | + |
| 17 | + $originalUser = $adminUser->id; |
| 18 | + |
| 19 | + $id = is_numeric($adminUser->auth_as) ? $adminUser->auth_as : $adminUser->id; |
| 20 | + $user = User::find($id); |
| 21 | + |
| 22 | + $name = $user->name; |
| 23 | + |
| 24 | + if(Auth::user()->id === $originalUser) { |
| 25 | + |
| 26 | + // Generate unique token |
| 27 | + $token = Str::random(60); |
| 28 | + if(\Route::currentRouteName() !== 'authAs'){ |
| 29 | + $adminUser->remember_token = $token; |
| 30 | + $adminUser->save(); |
| 31 | + echo "<script>window.location.href = '".url('studio/links')."';</script>"; |
| 32 | + } |
| 33 | + |
| 34 | + Auth::loginUsingId($id); |
| 35 | + setcookie("display_auth_nav", "true", time() + (10 * 365 * 24 * 60 * 60), "/"); |
| 36 | + } |
| 37 | + |
| 38 | +if(isset($_COOKIE['display_auth_nav'])) { |
| 39 | +if (file_exists(base_path(findAvatar($id)))) { |
| 40 | + $img = '<img alt="avatar" class="iimg irounded" src="' . url(findAvatar($id)) . '">'; |
| 41 | +} elseif (file_exists(base_path("assets/linkstack/images/").findFile('avatar'))) { |
| 42 | + $img = '<img alt="avatar" class="iimg irounded" src="' . url("assets/linkstack/images/") . "/" . findFile('avatar') . '">'; |
| 43 | +} else { |
| 44 | + $img = '<img alt="avatar" class="iimg" src="' . asset('assets/linkstack/images/logo.svg') . '">'; |
| 45 | +} |
| 46 | +$dashboard = url('dashboard'); |
| 47 | +$URL = url('/auth-as'); |
| 48 | +$csrf = csrf_token(); |
| 49 | +$remember_token = User::find($originalUser); |
| 50 | +$token = $remember_token->remember_token; |
| 51 | +$customHtml = |
| 52 | +<<<EOD |
| 53 | +
|
| 54 | +<style> |
| 55 | + .ibar { |
| 56 | + position: fixed; |
| 57 | + top: 0; |
| 58 | + left: 0; |
| 59 | + width: 100%; |
| 60 | + height: 67px; |
| 61 | + background-color: #4d4c51; |
| 62 | + z-index: 911; |
| 63 | + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); |
| 64 | + } |
| 65 | +
|
| 66 | + .itext1 { |
| 67 | + color: white; |
| 68 | + font-family: "Inter", sans-serif; |
| 69 | + font-size: 18px; |
| 70 | + display: flex; |
| 71 | + align-items: center; |
| 72 | + justify-content: space-between; |
| 73 | + padding: 17px 16px; |
| 74 | + } |
| 75 | +
|
| 76 | + .itext1 span a { |
| 77 | + display: flex; |
| 78 | + align-items: center; |
| 79 | + justify-content: space-between; |
| 80 | + } |
| 81 | +
|
| 82 | + .itext1 a { |
| 83 | + color: white; |
| 84 | + text-decoration: none; |
| 85 | + } |
| 86 | +
|
| 87 | + .itext1 svg { |
| 88 | + width: 32px; |
| 89 | + height: 32px; |
| 90 | + fill: currentColor; |
| 91 | + margin-left: 8px; |
| 92 | + margin-bottom: 4px; |
| 93 | + } |
| 94 | +
|
| 95 | + .iimg { |
| 96 | + width: 32px; |
| 97 | + height: 32px; |
| 98 | + margin-right: 8px; |
| 99 | + margin-bottom: 3px; |
| 100 | + } |
| 101 | +
|
| 102 | + .irounded { |
| 103 | + border-radius: 50%; |
| 104 | + } |
| 105 | +
|
| 106 | + body { |
| 107 | + padding-top: 60px; /* Add padding equal to the height of .ibar */ |
| 108 | + } |
| 109 | +</style> |
| 110 | +
|
| 111 | +<div class="ibar"> |
| 112 | + <p class="itext1"> |
| 113 | + <span> |
| 114 | + <a href="$dashboard">$img $name</a> |
| 115 | + </span> |
| 116 | + <a style="cursor:pointer" onclick="document.getElementById('submitForm').submit(); return false;"> |
| 117 | + <svg xmlns="http://www.w3.org/2000/svg" class="bi bi-x" viewBox="0 0 16 16"> |
| 118 | + <path |
| 119 | + d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z" |
| 120 | + /> |
| 121 | + </svg> |
| 122 | + </a> |
| 123 | + </p> |
| 124 | +</div> |
| 125 | +
|
| 126 | +<form id="submitForm" action="$URL" method="POST" style="display: none;"> |
| 127 | +<input type="hidden" name="_token" value="$csrf"> |
| 128 | +<input type="hidden" name="token" value="$token"> |
| 129 | +<input type="hidden" name="id" value="$originalUser"> |
| 130 | +</form> |
| 131 | +
|
| 132 | +<script> |
| 133 | + function submitForm() { |
| 134 | + document.getElementById('submitForm').submit(); |
| 135 | + } |
| 136 | +</script> |
| 137 | +
|
| 138 | +EOD;; |
| 139 | +} else {$customHtml = "";} |
| 140 | + |
| 141 | + $response = $next($request); |
| 142 | + $content = $response->getContent(); |
| 143 | + $modifiedContent = preg_replace('/<body([^>]*)>/', "<body$1>{$customHtml}", $content); |
| 144 | + $response->setContent($modifiedContent); |
| 145 | + |
| 146 | + return $response; |
| 147 | + } else { |
| 148 | + if(isset($_COOKIE['display_auth_nav'])) { |
| 149 | + setcookie("display_auth_nav", "", time() - 3600, "/"); |
| 150 | + Auth::logout(); |
| 151 | + } |
| 152 | + return $next($request); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments