Skip to content

Commit

Permalink
Allow Impersonation without UI Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
SupianIDz committed Nov 25, 2024
1 parent 6242823 commit df5ea2c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<p align="center">
<img src="demo.gif" alt="Demo">
</p>
Expand Down Expand Up @@ -64,9 +63,25 @@ Add the trait `Octopy\Impersonate\Concerns\HasImpersonation` to your **User** mo
namespace App\Models;

use Octopy\Impersonate\Concerns\HasImpersonation;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use HasImpersonation;
}

```

If you plan to use the provided UI, add `Octopy\Impersonate\Contracts\HasImpersonationUI` interface to add mandatory configuration for the UI.

```php
namespace App\Models;

use Octopy\Impersonate\Concerns\HasImpersonation;
use Octopy\Impersonate\Contracts\HasImpersonationUI;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements HasImpersonationUI
{
use HasImpersonation;

Expand Down
10 changes: 0 additions & 10 deletions src/Concerns/HasImpersonation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ public function impersonate(mixed $impersonated = null) : Impersonate
return $manager;
}

/**
* @return string[]
*/
abstract public function getImpersonateSearchField() : array;

/**
* @return string
*/
abstract public function getImpersonateDisplayText() : string;

/**
* @param Authorization $authorization
* @return void
Expand Down
16 changes: 16 additions & 0 deletions src/Contracts/HasImpersonationUI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Octopy\Impersonate\Contracts;

interface HasImpersonationUI
{
/**
* @return string[]
*/
public function getImpersonateSearchField() : array;

/**
* @return string
*/
public function getImpersonateDisplayText() : string;
}
3 changes: 2 additions & 1 deletion src/Providers/ImpersonateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Octopy\Impersonate\Authorization;
use Octopy\Impersonate\Contracts\HasImpersonationUI;
use Octopy\Impersonate\Http\Middleware\ImpersonateMiddleware;
use Octopy\Impersonate\Impersonate;

Expand All @@ -16,7 +17,7 @@ class ImpersonateServiceProvider extends ServiceProvider
*/
public function boot(Router $router) : void
{
if (config('impersonate.enabled')) {
if (config('impersonate.enabled') && class_implements(config('impersonate.model'), HasImpersonationUI::class)) {
$router->pushMiddlewareToGroup('web', ImpersonateMiddleware::class);

$this->loadRoutesFrom(
Expand Down
3 changes: 2 additions & 1 deletion tests/Models/User1.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use Illuminate\Foundation\Auth\User;
use Octopy\Impersonate\Authorization;
use Octopy\Impersonate\Concerns\HasImpersonation;
use Octopy\Impersonate\Contracts\HasImpersonationUI;

/**
* @method static create(string[] $array)
* @property string $name
*/
class User1 extends User
class User1 extends User implements HasImpersonationUI
{
use HasImpersonation, SoftDeletes;

Expand Down

0 comments on commit df5ea2c

Please sign in to comment.