-
-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4.x] Add ability to impersonate a user (#8622)
* Add ability to impersonate a user * Remove testing * Use correct permission name * Only impersonate one person at a time * 🍺 * For @jacksleight, because he asked so nicely * Tidy up and add config to enable/disable * We don't actually need this * Add a toast success message * Tweak the Stop Impersonating overlay button * Add Stop Impersonating to user avatar dropdown, give it an animation * fix code style * Why don't you fix it yourself mr lint? --------- Co-authored-by: Jack McDade <[email protected]>
- Loading branch information
1 parent
f23ef9e
commit 438ad5a
Showing
13 changed files
with
243 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<style> | ||
#__impersonator-link__ { | ||
position: fixed; | ||
display: flex; | ||
z-index: 100; | ||
align-items: center; | ||
bottom: 0; | ||
right: 0; | ||
padding: 6px 10px; | ||
color: #726e23; | ||
font-size: 14px; | ||
border-radius: 4px 0 0 0; | ||
background: #fbfab0; | ||
border-left: 1px solid #e8dc1e; | ||
border-top: 1px solid #e8dc1e; | ||
text-decoration: none; | ||
transform: translateX(calc(100% - 35px)); | ||
transition: transform 0.21s cubic-bezier(0.11, 0, 0.5, 0); | ||
box-shadow: -1px -2px 4px 0 rgba(20,20,20,.02); | ||
} | ||
#__impersonator-link__:hover { | ||
transform: translateX(0); | ||
} | ||
#__impersonator-link__ svg { | ||
margin-right: 10px; | ||
} | ||
</style> | ||
|
||
<a id="__impersonator-link__" href="{{ $url }}"> | ||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" height="20" width="20" viewBox="-0.5 -0.5 14 14"><g><path fill="#fbfab0" d="M0.4642857142857143 4.494285714285715C0.4643683571428572 5.727465714285715 0.8797935714285715 6.924672857142857 1.6435714285714287 7.892857142857143l2.35651 1.8977214285714286c0.3400057142857143 0.2738357142857143 0.8248314285714285 0.2738357142857143 1.164837142857143 0L7.521428571428571 7.892857142857143c0.7637778571428572 -0.9681842857142857 1.179202142857143 -2.1653914285714286 1.1792857142857143 -3.398571428571429V1.4671428571428573C7.427884285714286 0.8084895000000001 6.0156478571428575 0.4647388571428572 4.5825 0.4647388571428572 3.149352142857143 0.4647388571428572 1.7371157142857143 0.8084895000000001 0.4642857142857143 1.4671428571428573v3.027142857142857Z" stroke-width="1"></path><path id="Vector_2" stroke="#726e23" stroke-linecap="round" stroke-linejoin="round" d="M1.6435714285714287 7.892857142857143C0.8797935714285715 6.924672857142857 0.4643683571428572 5.727465714285715 0.4642857142857143 4.494285714285715V1.4671428571428573C1.7371157142857143 0.8084895000000001 3.149352142857143 0.4647388571428572 4.5825 0.4647388571428572c1.4331478571428573 0 2.8453842857142857 0.34375064285714285 4.118214285714285 1.002404" stroke-width="1"></path><path id="Vector_3" fill="#e8dc1e" d="M7.23359 12.48c-0.3249535714285714 0.07345 -0.6626842857142857 0.06880714285714286 -0.9855021428571429 -0.013464285714285715 -0.32282714285714287 -0.08236428571428572 -0.62153 -0.2400357142857143 -0.8716407142857143 -0.4601071428571429 -0.8689107142857143 -0.7490785714285715 -1.5123364285714287 -1.7250071428571427 -1.8583500000000002 -2.8188085714285713 -0.3460042857142857 -1.0937828571428572 -0.3809557142857143 -2.2622507142857144 -0.1009357142857143 -3.374762857142857l0.6778571428571428 -2.7114285714285713c1.4722407142857143 -0.37337857142857145 3.0141428571428572 -0.37540285714285715 4.487349285714286 -0.005887142857142857C10.055592857142857 3.465057142857143 11.414 4.194561428571428 12.535714285714286 5.218571428571429l-0.6778571428571428 2.7021428571428574c-0.2732785714285714 1.113515 -0.8502 2.129307142857143 -1.6666928571428572 2.9342857142857146 -0.8164 0.8049785714285714 -1.8402985714285713 1.3675071428571428 -2.957574285714286 1.625Z" stroke-width="1"></path><path id="Vector_4" stroke="#726e23" stroke-linecap="round" stroke-linejoin="round" d="M7.233534285714286 12.48c-0.3249535714285714 0.07345 -0.6626842857142857 0.06880714285714286 -0.9855021428571429 -0.013464285714285715 -0.32282714285714287 -0.08236428571428572 -0.62153 -0.2400357142857143 -0.8716407142857143 -0.4601071428571429v0c-0.8689107142857143 -0.7490785714285715 -1.5123364285714287 -1.7250071428571427 -1.8583500000000002 -2.8188085714285713 -0.3460042857142857 -1.0937828571428572 -0.3809557142857143 -2.2622507142857144 -0.1009357142857143 -3.374762857142857l0.6778571428571428 -2.7114285714285713c1.4722407142857143 -0.37337857142857145 3.0141428571428572 -0.37540285714285715 4.487349285714286 -0.005887142857142857C10.0555 3.465057142857143 11.413907142857143 4.194561428571428 12.535714285714286 5.218571428571429l-0.6778571428571428 2.7021428571428574c-0.2732785714285714 1.113515 -0.8502928571428572 2.129307142857143 -1.6666928571428572 2.9342857142857146 -0.8164928571428571 0.8049785714285714 -1.8403635714285715 1.3675071428571428 -2.95763 1.625v0Z" stroke-width="1"></path><path id="Vector_5" stroke="#726e23" stroke-linecap="round" stroke-linejoin="round" d="M5.209276428571428 6.045c0.11713000000000001 -0.12258071428571428 0.2655992857142857 -0.21075785714285716 0.42928785714285717 -0.25494857142857147 0.16369785714285714 -0.0442 0.3363657142857143 -0.04273285714285714 0.49928357142857144 0.004234285714285714 0.15945428571428574 0.037514285714285715 0.30615928571428574 0.11654500000000001 0.4252114285714286 0.22906928571428573 0.11905214285714286 0.11253357142857144 0.20620785714285714 0.2545492857142857 0.2526457142857143 0.411645" stroke-width="1"></path><path id="Vector_6" stroke="#726e23" stroke-linecap="round" stroke-linejoin="round" d="M8.886437857142857 6.964276428571429c0.11338785714285714 -0.12185642857142859 0.2574464285714286 -0.21099 0.417105 -0.25807785714285714 0.15962142857142858 -0.04708785714285714 0.32899285714285714 -0.050393571428571426 0.49037857142857144 -0.009573571428571428 0.1613857142857143 0.04081071428571429 0.30875 0.12425214285714287 0.42686428571428575 0.2415864285714286 0.11802142857142857 0.117325 0.20242857142857143 0.2642342857142857 0.24421428571428574 0.4253507142857143" stroke-width="1"></path></g></svg> | ||
{{ __('Stop impersonating') }} | ||
</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Statamic\Actions; | ||
|
||
use Illuminate\Events\NullDispatcher; | ||
use Illuminate\Support\Facades\Auth; | ||
use Statamic\Contracts\Auth\User as UserContract; | ||
use Statamic\Facades\CP\Toast; | ||
use Statamic\Facades\User; | ||
|
||
class Impersonate extends Action | ||
{ | ||
protected $confirm = false; | ||
|
||
public static function title() | ||
{ | ||
return __('Start Impersonating'); | ||
} | ||
|
||
public function visibleTo($item) | ||
{ | ||
if (! config('statamic.users.impersonate.enabled', true) || session()->get('statamic_impersonated_by')) { | ||
return false; | ||
} | ||
|
||
return $item instanceof UserContract && $item != User::current(); | ||
} | ||
|
||
public function visibleToBulk($items) | ||
{ | ||
return false; | ||
} | ||
|
||
public function authorize($authed, $user) | ||
{ | ||
return $authed->can('impersonate users'); | ||
} | ||
|
||
public function run($users, $values) | ||
{ | ||
$guard = Auth::guard(); | ||
|
||
$dispatcher = $guard->getDispatcher(); | ||
|
||
if ($dispatcher) { | ||
$guard->setDispatcher(new NullDispatcher($dispatcher)); | ||
} | ||
|
||
try { | ||
$currentUser = $guard->user(); | ||
|
||
$guard->login($users->first()); | ||
session()->put('statamic_impersonated_by', $currentUser->getKey()); | ||
Toast::success(__('You are now impersonating').' '.$users->first()->name()); | ||
} finally { | ||
if ($dispatcher) { | ||
$guard->setDispatcher($dispatcher); | ||
} | ||
} | ||
} | ||
|
||
public function redirect($users, $values) | ||
{ | ||
if ($url = config('statamic.users.impersonate.redirect')) { | ||
return $url; | ||
} | ||
|
||
return $users->first()->can('access cp') ? cp_route('dashboard') : '/'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Controllers\CP\Auth; | ||
|
||
use Illuminate\Events\NullDispatcher; | ||
use Illuminate\Support\Facades\Auth; | ||
use Statamic\Facades\User; | ||
|
||
class ImpersonationController | ||
{ | ||
public function stop() | ||
{ | ||
if ($originalUserId = session()->pull('statamic_impersonated_by')) { | ||
$guard = Auth::guard(); | ||
|
||
$dispatcher = $guard->getDispatcher(); | ||
|
||
if ($dispatcher) { | ||
$guard->setDispatcher(new NullDispatcher($dispatcher)); | ||
} | ||
|
||
$originalUser = User::find($originalUserId); | ||
|
||
if ($originalUser) { | ||
$guard->login($originalUser); | ||
} | ||
|
||
if ($dispatcher) { | ||
$guard->setDispatcher($dispatcher); | ||
} | ||
} | ||
|
||
return redirect()->route('statamic.cp.users.index'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Response; | ||
|
||
class StopImpersonating | ||
{ | ||
public function handle($request, Closure $next) | ||
{ | ||
$response = $next($request); | ||
|
||
if (! $this->shouldInjectLink($response)) { | ||
return $response; | ||
} | ||
|
||
return $this->injectLink($response); | ||
} | ||
|
||
private function shouldInjectLink($response) | ||
{ | ||
if (! session()->get('statamic_impersonated_by')) { | ||
return false; | ||
} | ||
|
||
if (! $response instanceof Response) { | ||
return false; | ||
} | ||
|
||
if (! $content = $response->content()) { | ||
return false; | ||
} | ||
|
||
if (stripos($content, '<html') === false) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function injectLink($response) | ||
{ | ||
$content = $response->content(); | ||
|
||
$link = view('statamic::impersonator.terminate', [ | ||
'url' => route('statamic.cp.impersonation.stop'), | ||
])->render(); | ||
|
||
return $response->setContent(str_replace('</body>', $link.'</body>', $content)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters