Skip to content

Commit

Permalink
Get/set leave redirect to url from/to session
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeccanici committed Dec 22, 2023
1 parent d8ab69f commit ff0340c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
5 changes: 5 additions & 0 deletions config/laravel-impersonate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
'session_guard_using' => 'impersonator_guard_using',

/**
* The session key used to store the URI to go to after leaving an impersonation.
*/
'session_leave_redirect_to' => 'impersonator_leave_redirect_to',

/**
* The default impersonator guard used.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/Controllers/ImpersonateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public function take(Request $request, $id, $guardName = null)

$userToImpersonate = $this->manager->findUserById($id, $guardName);

$leaveRedirectUrl = $request->get('leaveRedirectTo');

if ($userToImpersonate->canBeImpersonated()) {
if ($this->manager->take($request->user(), $userToImpersonate, $guardName)) {
if ($this->manager->take($request->user(), $userToImpersonate, $guardName, $leaveRedirectUrl)) {
$takeRedirect = $this->manager->getTakeRedirectTo();
if ($takeRedirect !== 'back') {
return redirect()->to($takeRedirect);
Expand All @@ -70,9 +72,10 @@ public function leave()
abort(403);
}

$leaveRedirect = $this->manager->getLeaveRedirectTo();

$this->manager->leave();

$leaveRedirect = $this->manager->getLeaveRedirectTo();
if ($leaveRedirect !== 'back') {
return redirect()->to($leaveRedirect);
}
Expand Down
13 changes: 12 additions & 1 deletion src/Services/ImpersonateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function getImpersonatorGuardUsingName()
* @param string|null $guardName
* @return bool
*/
public function take($from, $to, $guardName = null)
public function take($from, $to, $guardName = null, $leaveRedirectUrl = null)
{
$this->saveAuthCookieInSession();

Expand All @@ -116,6 +116,7 @@ public function take($from, $to, $guardName = null)
session()->put($this->getSessionKey(), $from->getAuthIdentifier());
session()->put($this->getSessionGuard(), $currentGuard);
session()->put($this->getSessionGuardUsing(), $guardName);
session()->put($this->getSessionLeaveRedirectTo(), $leaveRedirectUrl);

$this->app['auth']->guard($currentGuard)->quietLogout();
$this->app['auth']->guard($guardName)->quietLogin($to);
Expand Down Expand Up @@ -158,6 +159,7 @@ public function clear()
session()->forget($this->getSessionKey());
session()->forget($this->getSessionGuard());
session()->forget($this->getSessionGuardUsing());
session()->forget($this->getSessionLeaveRedirectTo());
}

public function getSessionKey(): string
Expand All @@ -180,6 +182,11 @@ public function getDefaultSessionGuard(): string
return config('laravel-impersonate.default_impersonator_guard');
}

public function getSessionLeaveRedirectTo(): string
{
return config('laravel-impersonate.session_leave_redirect_to');
}

public function getTakeRedirectTo(): string
{
try {
Expand All @@ -194,6 +201,10 @@ public function getTakeRedirectTo(): string
public function getLeaveRedirectTo(): string
{
try {
if ($uri = session($this->getSessionLeaveRedirectTo())) {
return $uri;
}

$uri = route(config('laravel-impersonate.leave_redirect_to'));
} catch (\InvalidArgumentException $e) {
$uri = config('laravel-impersonate.leave_redirect_to');
Expand Down
52 changes: 52 additions & 0 deletions tests/ImpersonateControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Lab404\Tests;

use Lab404\Impersonate\Services\ImpersonateManager;

class ImpersonateControllerTest extends TestCase
{
/** @var ImpersonateManager $manager */
protected $manager;

/** @var string $guard */
protected $guard;

public function setUp(): void
{
parent::setUp();

$this->manager = $this->app->make(ImpersonateManager::class);
$this->guard = 'web';
}

/** @test */
public function it_gets_leave_redirect_to_from_session_if_exists()
{
// Arrange
$leaveRedirectTo = 'http://example.com';
$this->app['session']->put($this->manager->getSessionLeaveRedirectTo(), $leaveRedirectTo);
$this->app['session']->put($this->manager->getSessionKey(), 'test_session_key');

// Act
$response = $this->get(route('impersonate.leave'));

// Assert
$response->assertRedirect($leaveRedirectTo);
}

/** @test */
public function it_gets_leave_redirect_to_from_query_parameter_and_stores_it_in_session()
{
// Arrange
$this->withoutExceptionHandling();
$this->app['auth']->loginUsingId('[email protected]');
$leaveRedirectTo = 'http://example.com';

// Act
$response = $this->get(route('impersonate', ['id' => '[email protected]', 'guardName' => 'web', 'leaveRedirectTo' => $leaveRedirectTo]));

// Assert
$this->assertEquals($leaveRedirectTo, $this->app['session']->get($this->manager->getSessionLeaveRedirectTo()));
}
}
15 changes: 15 additions & 0 deletions tests/ImpersonateManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,19 @@ public function it_renames_the_remember_web_cookie_when_taking_and_reverts_the_c
$this->assertEquals($cookie->getName(), $response->headers->getCookies()[0]->getName());
$this->assertEquals($cookie->getValue(), $response->headers->getCookies()[0]->getValue());
}

/** @test */
public function it_puts_leave_redirect_to_in_session_when_take_is_called()
{
// Arrange
$this->app['auth']->loginUsingId('[email protected]');
$leaveRedirectTo = 'http://localhost/custom-redirect-url';

// Act
$this->manager->take($this->app['auth']->user(), $this->manager->findUserById('[email protected]'), null, $leaveRedirectTo);

// Assert
$this->assertEquals($leaveRedirectTo, $this->app['session']->get($this->manager->getSessionLeaveRedirectTo()));
}

}

0 comments on commit ff0340c

Please sign in to comment.