Skip to content

Commit

Permalink
feat!: Updated auth plugin to use filament plugin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
phpsa committed Feb 18, 2024
1 parent dad492b commit 1e3e710
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 89 deletions.
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

Route::get('/impersonate/stop', fn () => ImpersonateLink::leave())
->name('filament-authentication.stop.impersonation')
->middleware(config('filament-authentication.impersonate.guard'));
->middleware('web');
22 changes: 13 additions & 9 deletions src/Actions/ImpersonateLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Http\RedirectResponse;
use Lab404\Impersonate\Services\ImpersonateManager;
use Illuminate\Contracts\Auth\Authenticatable as User;

use Phpsa\FilamentAuthentication\FilamentAuthentication;

class ImpersonateLink
{
Expand All @@ -30,7 +30,8 @@ public static function make(): Action
*/
public static function allowed(User $current, User $target): bool
{
return config('filament-authentication.impersonate.enabled', false)
$enabled = FilamentAuthentication::getPlugin()->impersonateEnabled();
return $enabled
&& $current->isNot($target)
&& ! app(ImpersonateManager::class)->isImpersonating()
&& (! method_exists($current, 'canImpersonate') || $current->canImpersonate())
Expand All @@ -46,32 +47,35 @@ public static function impersonate(User $record): false|Redirector|RedirectRespo
app(ImpersonateManager::class)->take(
Filament::auth()->user(),
$record,
config('filament-authentication.impersonate.guard', 'web')
FilamentAuthentication::getPlugin()->getImpersonateGuard()
);

session()->put('impersonate.back_to', url()->previous());

session()->forget(array_unique([
'password_hash_'.config('filament-authentication.impersonate.guard', 'web'),
'password_hash_'.config('filament.auth.guard'),
'password_hash_' . FilamentAuthentication::getPlugin()->getImpersonateGuard(),
'password_hash_' . config('filament.auth.guard'),
]));

return redirect(config('filament-authentication.impersonate.redirect', '/'));
return redirect(FilamentAuthentication::getPlugin()->getImpersonateRedirect());
}

public static function leave(): Redirector|RedirectResponse
{

if (! app(ImpersonateManager::class)->isImpersonating()) {
return redirect('/');
}

app(ImpersonateManager::class)->leave();

session()->forget(array_unique([
'password_hash_'.config('filament-authentication.impersonate.guard'),
'password_hash_'.config('filament.auth.guard'),
'password_hash_' . FilamentAuthentication::getPlugin()->getImpersonateGuard(),
'password_hash_' . config('filament.auth.guard'),
]));

return redirect(
session()->pull('impersonate.back_to') ?? config('filament.path')
session()->pull('impersonate.back_to') ?? '/'
);
}
}
144 changes: 137 additions & 7 deletions src/FilamentAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,146 @@

namespace Phpsa\FilamentAuthentication;

use Filament\Navigation\MenuItem;
use Closure;
use Filament\Panel;
use App\Models\User;
use Filament\Contracts\Plugin;
use Spatie\Permission\Models\Role;
use Filament\Tables\Columns\TextColumn;
use Spatie\Permission\Models\Permission;
use Phpsa\FilamentAuthentication\Resources\RoleResource;
use Phpsa\FilamentAuthentication\Resources\UserResource;
use Phpsa\FilamentAuthentication\Resources\PermissionResource;
use Phpsa\FilamentAuthentication\Http\Middleware\ImpersonatingMiddleware;

class FilamentAuthentication
class FilamentAuthentication implements Plugin
{
public static function resources(array $with = []): array
protected bool $preloadRoles = true;
protected bool $preloadPermissions = true;
protected bool $impersonate = false;
protected string $impersonateGuard = 'web';
protected string|Closure $impersonateRedirect = '/';

/**
* @var array<string, class-string>
*/
protected array $models = [
'User' => User::class,
'Role' => Role::class,
'Permission' => Permission::class,
];

/**
* @var array<string, class-string>
*/
protected array $resources = [
'UserResource' => UserResource::class,
'RoleResource' => RoleResource::class,
'PermissionResource' => PermissionResource::class,
];

public static function make(): self
{
return new static();
}

public static function getPlugin(): self
{
return filament('filament-authentication');
}

public function getId(): string
{
return 'filament-authentication';
}

public function register(Panel $panel): void
{
$panel->resources($this->resources);
$panel->middleware([ImpersonatingMiddleware::class]);
}

public function boot(Panel $panel): void
{
TextColumn::macro('humanDate', function () {
/** @var \Filament\Tables\Columns\TextColumn&\Filament\Tables\Columns\Concerns\CanFormatState $this */
$this->formatStateUsing(fn ($state): ?string => $state ? $state->diffForHumans() : null);

return $this;
});
}

public function setPreload(bool $roles = true, bool $permissions = true): self
{
$this->preloadRoles = $roles;
$this->preloadPermissions = $permissions;

return $this;
}

public function getPreloadRoles(): bool
{
return $this->preloadRoles;
}

public function getPreloadPermissions(): bool
{
return $this->preloadPermissions;
}

public function setImpersonation(bool $enabled = true, string $guard = 'web', string|Closure $redirect = '/'): self
{
$this->impersonate = $enabled;
$this->impersonateGuard = $guard;
$this->impersonateRedirect = $redirect;

return $this;
}

public function impersonateEnabled(): bool
{
return $this->impersonate;
}

public function getImpersonateGuard(): string
{
return $this->impersonateGuard;
}

public function getImpersonateRedirect(): string|Closure
{
$value = $this->impersonateRedirect;
if (! $value instanceof Closure) {
return $value;
}
return $value();
}

/**
* @param array<string,class-string> $overrides
*/
public function overrideModels(array $overrides): self
{
$this->models = array_merge($this->models, $overrides);
return $this;
}

public function getModel(string $model): string
{
return $this->models[$model];
}

/**
* @param array<string,class-string> $overrides
*/
public function overrideResources(array $overrides): self
{
$this->resources = array_merge($this->resources, $overrides);
return $this;
}

public function getResource(string $resource): string
{
return array_merge(
config('filament-authentication.resources'),
$with
);
return $this->resources[$resource];
}
}
32 changes: 0 additions & 32 deletions src/FilamentAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,12 @@ class FilamentAuthenticationProvider extends PackageServiceProvider
{
public static string $name = 'filament-authentication';

protected array $widgets = [
LatestUsersWidget::class,
];

protected function getResources(): array
{
return config('filament-authentication.resources');
}

protected function getWidgets(): array
{
return config('filament-authentication.resources.widgets');
}

public function configurePackage(Package $package): void
{
Config::push('filament.middleware.base', ImpersonatingMiddleware::class);

$package->name('filament-authentication')
->hasConfigFile()
->hasViews()
->hasRoute('web')
->hasTranslations();
}

public function getPages(): array
{
return config('filament-authentication.pages');
}

public function packageRegistered(): void
{

TextColumn::macro('humanDate', function () {
/** @var \Filament\Tables\Columns\TextColumn&\Filament\Tables\Columns\Concerns\CanFormatState $this */
$this->formatStateUsing(fn ($state): ?string => $state ? $state->diffForHumans() : null);

return $this;
});
}
}
2 changes: 1 addition & 1 deletion src/Http/Middleware/ImpersonatingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function handle(Request $request, \Closure $next)
$response->setContent(
str_replace(
'</body>',
$this->getHtmlContent($request).'</body>',
$this->getHtmlContent($request) . '</body>',
// @phpstan-ignore-next-line
$response->getContent()
)
Expand Down
8 changes: 3 additions & 5 deletions src/Resources/PermissionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Forms\Components\Section as Card;
use Filament\Forms\Components\BelongsToManyMultiSelect;
use Phpsa\FilamentAuthentication\FilamentAuthentication;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages\EditPermission;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages\ViewPermission;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages\ListPermissions;
Expand All @@ -29,7 +30,7 @@ class PermissionResource extends Resource

public function __construct()
{
static::$model = config('filament-authentication.models.Permission');
static::$model = FilamentAuthentication::getPlugin()->getModel('Permission');
}

public static function getLabel(): string
Expand Down Expand Up @@ -61,10 +62,7 @@ public static function form(Form $form): Form
->required()
->label(strval(__('filament-authentication::filament-authentication.field.guard_name')))
->default(config('auth.defaults.guard')),
// BelongsToManyMultiSelect::make('roles')
// ->label(strval(__('filament-authentication::filament-authentication.field.roles')))
// ->relationship('roles', 'name')
// ->preload(config('filament-spatie-roles-permissions.preload_roles'))

]),
]),
]);
Expand Down
7 changes: 4 additions & 3 deletions src/Resources/PermissionResource/Pages/CreatePermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages;

use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\Config;
use Spatie\Permission\Contracts\Permission;
use Filament\Resources\Pages\CreateRecord;
use Spatie\Permission\PermissionRegistrar;
use Spatie\Permission\Contracts\Permission;
use Phpsa\FilamentAuthentication\FilamentAuthentication;

class CreatePermission extends CreateRecord
{
public static function getResource(): string
{
return Config::get('filament-authentication.resources.PermissionResource');
return FilamentAuthentication::getPlugin()->getResource('PermissionResource');
}

public function afterSave(): void
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/PermissionResource/Pages/EditPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
use Filament\Resources\Pages\EditRecord;
use Spatie\Permission\PermissionRegistrar;
use Spatie\Permission\Contracts\Permission;
use Phpsa\FilamentAuthentication\FilamentAuthentication;

class EditPermission extends EditRecord
{
public static function getResource(): string
{
return Config::get('filament-authentication.resources.PermissionResource');
return FilamentAuthentication::getPlugin()->getResource('PermissionResource');
}

public function afterSave(): void
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/PermissionResource/Pages/ListPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
use Filament\Tables\Actions\BulkAction;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Collection;
use Phpsa\FilamentAuthentication\FilamentAuthentication;

class ListPermissions extends ListRecords
{
public static function getResource(): string
{
return Config::get('filament-authentication.resources.PermissionResource');
return FilamentAuthentication::getPlugin()->getResource('PermissionResource');
}

protected function getHeaderActions(): array
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/PermissionResource/Pages/ViewPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use Filament\Actions\EditAction;
use Illuminate\Support\Facades\Config;
use Filament\Resources\Pages\ViewRecord;
use Phpsa\FilamentAuthentication\FilamentAuthentication;

class ViewPermission extends ViewRecord
{
public static function getResource(): string
{
return Config::get('filament-authentication.resources.PermissionResource');
return FilamentAuthentication::getPlugin()->getResource('PermissionResource');
}

protected function getHeaderActions(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Spatie\Permission\PermissionRegistrar;
use Filament\Tables\Actions\DissociateBulkAction;
use Filament\Resources\RelationManagers\RelationManager;
use Phpsa\FilamentAuthentication\FilamentAuthentication;

class RoleRelationManager extends RelationManager
{
Expand Down Expand Up @@ -44,7 +45,7 @@ public function table(Table $table): Table
])
->headerActions([
CreateAction::make(),
AttachAction::make()->preloadRecordSelect(config('filament-authentication.preload_roles', true))
AttachAction::make()->preloadRecordSelect(FilamentAuthentication::getPlugin()->getPreloadRoles())
->recordSelect(fn($select) => $select->multiple())
->closeModalByClickingAway(false),
])
Expand Down
Loading

0 comments on commit 1e3e710

Please sign in to comment.