Skip to content

Commit

Permalink
Merge Jetstream
Browse files Browse the repository at this point in the history
Compability with Laravel Jetstream
  • Loading branch information
SupianIDz authored Nov 9, 2023
2 parents a3340ec + 7b33215 commit 4c9705d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
47 changes: 27 additions & 20 deletions src/Impersonate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Octopy\Impersonate;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Laravel\Jetstream\Jetstream;
use Octopy\Impersonate\Concerns\HasImpersonation;
use Octopy\Impersonate\Events\BeginImpersonation;
use Octopy\Impersonate\Events\LeaveImpersonation;
Expand All @@ -15,11 +15,6 @@

class Impersonate
{
/**
* @var StatefulGuard
*/
protected StatefulGuard $guard;

/**
* @var Repository
*/
Expand All @@ -28,15 +23,15 @@ class Impersonate
/**
* @var SessionStorage
*/
protected SessionStorage $storage;
protected SessionStorage $session;

/**
* Impersonate constructor.
*/
public function __construct()
public function __construct(protected Auth $auth)
{
$this->repository = new Repository;
$this->storage = new SessionStorage;
$this->session = new SessionStorage;

$this->guard(config(
'impersonate.guard'
Expand All @@ -51,7 +46,7 @@ public function __construct()
*/
public function guard(string $guard) : self
{
$this->guard = Auth::guard($guard);
$this->auth->guard($guard);

return $this;
}
Expand All @@ -61,7 +56,7 @@ public function guard(string $guard) : self
*/
public function check() : bool
{
return $this->storage->isInImpersonatingMode();
return $this->session->isInImpersonatingMode();
}

/**
Expand All @@ -73,7 +68,7 @@ public function authorized() : bool
return false;
}

return $this->guard->check() && app('impersonate.authorization')->isImpersonator($this->impersonator());
return $this->auth->check() && app('impersonate.authorization')->isImpersonator($this->impersonator());
}

/**
Expand All @@ -82,18 +77,18 @@ public function authorized() : bool
public function impersonator() : Model|Authenticatable
{
if ($this->check()) {
return $this->repository->find($this->storage->getImpersonator());
return $this->repository->find($this->session->getImpersonator());
}

return $this->guard->user();
return $this->auth->user();
}

/**
* @return Model|Authenticatable
*/
public function impersonated() : Model|Authenticatable
{
return $this->repository->find($this->storage->getImpersonated());
return $this->repository->find($this->session->getImpersonated());
}

/**
Expand All @@ -108,11 +103,17 @@ public function begin(mixed $impersonator, mixed $impersonated) : Impersonate
$impersonated = $this->fetchModel($impersonated);

if ($this->validate($impersonator, $impersonated)) {
$this->storage
$this->session
->setImpersonator($impersonator)
->setImpersonated($impersonated);

$this->guard->login($impersonated);
$this->auth->login($impersonated);

if (class_exists(Jetstream::class)) {
$this->session->setPasswordHash(
$this->auth->user()->getAuthPassword()
);
}

event(new BeginImpersonation(
$impersonator, $impersonated
Expand All @@ -131,8 +132,14 @@ public function leave() : Impersonate
$impersonator = $this->impersonator();
$impersonated = $this->impersonated();

if ($this->storage->flush()) {
$this->guard->login($impersonator);
if ($this->session->flush()) {
$this->auth->login($impersonator);

if (class_exists(Jetstream::class)) {
$this->session->setPasswordHash(
$this->auth->user()->getAuthPassword()
);
}

event(new LeaveImpersonation(
$impersonator, $impersonated
Expand Down
12 changes: 11 additions & 1 deletion src/Storage/SessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Octopy\Impersonate\Storage;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class SessionStorage
Expand Down Expand Up @@ -70,4 +69,15 @@ public function flush() : bool

return true;
}

/**
* @param string $password
* @return void
*/
public function setPasswordHash(string $password) : void
{
session()->put([
'password_hash_sanctum' => $password,
]);
}
}

0 comments on commit 4c9705d

Please sign in to comment.