Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for setting the redirect leave URL when impersonating a user #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('admin@test.rocks');
$leaveRedirectTo = 'http://example.com';

// Act
$response = $this->get(route('impersonate', ['id' => 'user@test.rocks', '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('admin@test.rocks');
$leaveRedirectTo = 'http://localhost/custom-redirect-url';

// Act
$this->manager->take($this->app['auth']->user(), $this->manager->findUserById('user@test.rocks'), null, $leaveRedirectTo);

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

}