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

Inject sandbox instance in service provider for Laravel Octane support #169

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/Controllers/ImpersonateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ImpersonateController extends Controller
public function __construct()
{
$this->manager = app()->make(ImpersonateManager::class);

$guard = $this->manager->getDefaultSessionGuard();
$this->middleware('auth:' . $guard)->only('take');
}
Expand All @@ -31,27 +31,28 @@ public function __construct()
*/
public function take(Request $request, $id, $guardName = null)
{
$guardName = $guardName ?? $this->manager->getDefaultSessionGuard();
$manager = app()->make(ImpersonateManager::class);
$guardName = $guardName ?? $manager->getDefaultSessionGuard();

// Cannot impersonate yourself
if ($id == $request->user()->getAuthIdentifier() && ($this->manager->getCurrentAuthGuardName() == $guardName)) {
if ($id == $request->user()->getAuthIdentifier() && ($manager->getCurrentAuthGuardName() == $guardName)) {
abort(403);
}

// Cannot impersonate again if you're already impersonate a user
if ($this->manager->isImpersonating()) {
if ($manager->isImpersonating()) {
abort(403);
}

if (!$request->user()->canImpersonate()) {
abort(403);
}

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

if ($userToImpersonate->canBeImpersonated()) {
if ($this->manager->take($request->user(), $userToImpersonate, $guardName)) {
$takeRedirect = $this->manager->getTakeRedirectTo();
if ($manager->take($request->user(), $userToImpersonate, $guardName)) {
$takeRedirect = $manager->getTakeRedirectTo();
if ($takeRedirect !== 'back') {
return redirect()->to($takeRedirect);
}
Expand All @@ -66,13 +67,14 @@ public function take(Request $request, $id, $guardName = null)
*/
public function leave()
{
if (!$this->manager->isImpersonating()) {
$manager = app()->make(ImpersonateManager::class);
if (!$manager->isImpersonating()) {
abort(403);
}

$this->manager->leave();
$manager->leave();

$leaveRedirect = $this->manager->getLeaveRedirectTo();
$leaveRedirect = $manager->getLeaveRedirectTo();
if ($leaveRedirect !== 'back') {
return redirect()->to($leaveRedirect);
}
Expand Down
5 changes: 4 additions & 1 deletion src/ImpersonateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Auth\AuthManager;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Container\Container;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Event;
use Illuminate\View\Compilers\BladeCompiler;
Expand Down Expand Up @@ -34,7 +35,9 @@ public function register()
$this->app->bind(ImpersonateManager::class, ImpersonateManager::class);

$this->app->singleton(ImpersonateManager::class, function ($app) {
return new ImpersonateManager($app);
return new ImpersonateManager(function () {
return Container::getInstance();
});
});

$this->app->alias(ImpersonateManager::class, 'impersonate');
Expand Down
5 changes: 3 additions & 2 deletions src/Services/ImpersonateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lab404\Impersonate\Services;

use Closure;
use Exception;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;
Expand All @@ -19,9 +20,9 @@ class ImpersonateManager
/** @var Application $app */
private $app;

public function __construct(Application $app)
public function __construct(Closure $app)
{
$this->app = $app;
$this->app = $app();
}

/**
Expand Down