Skip to content

Commit

Permalink
feat: Improvements to endpoint resource & token generation
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-code-labx committed Jun 6, 2024
1 parent 731988d commit d28e74c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 23 deletions.
3 changes: 0 additions & 3 deletions src/StarterKits/Filament/FilamentPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

namespace XtendPackages\RESTPresenter\StarterKits\Filament;

use App\Filament\Pages\Auth\Login;
use App\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
Expand Down
14 changes: 3 additions & 11 deletions src/StarterKits/Filament/Resources/EndpointResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Filament\Tables;
use Filament\Tables\Grouping\Group;
use Filament\Tables\Table;
use Illuminate\Support\Str;
use XtendPackages\RESTPresenter\Models\Endpoint;
use XtendPackages\RESTPresenter\StarterKits\Filament\Resources\EndpointResource\Pages;

Expand Down Expand Up @@ -51,11 +50,8 @@ public static function form(Form $form): Form
->required()
->maxLength(255),
Forms\Components\Toggle::make('is_authenticated')
->label('Authenticated')
->label('Auth')
->default(false),
Forms\Components\Toggle::make('is_active')
->label('Active')
->default(true),
]);
}

Expand All @@ -66,7 +62,6 @@ public static function table(Table $table): Table
->groups([
Group::make('group')
->collapsible()
->getDescriptionFromRecordUsing(fn (Endpoint $record): string => Str::of($record->route)->beforeLast('.')->value())
->titlePrefixedWithLabel(false),
])
->columns([
Expand All @@ -82,6 +77,7 @@ public static function table(Table $table): Table
->weight('medium')
->alignLeft(),
Tables\Columns\TextColumn::make('type')
->label('Method')
->badge()
->color(fn (string $state): string => match ($state) {
'GET' => 'success',
Expand All @@ -97,11 +93,7 @@ public static function table(Table $table): Table
->alignLeft(),
Tables\Columns\IconColumn::make('is_authenticated')
->boolean()
->label('Authenticated')
->alignCenter(),
Tables\Columns\IconColumn::make('is_active')
->boolean()
->label('Active')
->label('Auth')
->alignCenter(),
])
->filters([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@

use Filament\Notifications\Actions\Action;
use Filament\Notifications\Notification;
use Illuminate\Support\Carbon;

class GenerateApiTokenAction
{
public function __invoke(): void
public function __invoke(array $data): void
{
if ($data['abilities'] ?? false) {
$data['abilities'] = collect($data['abilities'])->map(
fn ($ability) => $ability['name'].($ability['only'] ? ':'.$ability['only'] : ''),
)->toArray();
}

$config = [
'abilities' => type(config('rest-presenter.auth.abilities'))->asArray(),
'tokenName' => type(config('rest-presenter.auth.token_name'))->asString(),
'abilities' => type($data['abilities'] ?? config('rest-presenter.auth.abilities'))->asArray(),
'tokenName' => type($data['tokenName'] ?? config('rest-presenter.auth.token_name'))->asString(),
];

$token = auth()->user()->createToken(
name: $config['tokenName'],
abilities: $config['abilities'],
expiresAt: Carbon::parse($data['expires_at']) ?? null,
)->plainTextToken;

Notification::make()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@
namespace XtendPackages\RESTPresenter\StarterKits\Filament\Resources\EndpointResource\Forms;

use Filament\Forms;
use Filament\Forms\Form;

class GenerateApiTokenForm
{
public function __invoke()
{
return [
Forms\Components\TextInput::make('token_name')
Forms\Components\TextInput::make('tokenName')
->default('RESTPresenter API Token')
->required(),
Forms\Components\CheckboxList::make('abilities')
Forms\Components\Repeater::make('abilities')
->schema([
Forms\Components\TextInput::make('name')
->default('*')
->required(),
Forms\Components\Select::make('only')
->options([
'create' => 'Create',
'read' => 'Read',
'update' => 'Update',
'delete' => 'Delete',
]),
])
->grid(3)
->required(),
Forms\Components\DateTimePicker::make('expires_at')
->default(now()->addMonth()->setTime(12, 0)->format('Y-m-d H:i:s'))
->required(),
Forms\Components\DatePicker::make('expires_at'),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ protected function getActions(): array
->button(),
Actions\ActionGroup::make([
Actions\Action::make('generate-api-token')
->action(fn () => resolve(EndpointResource\Actions\GenerateApiTokenAction::class)())
->form(fn () => resolve(EndpointResource\Forms\GenerateApiTokenForm::class)())
->action(fn (array $data) => resolve(EndpointResource\Actions\GenerateApiTokenAction::class)($data))
->label('Generate Token')
->icon('heroicon-o-key'),
Actions\Action::make('manage-api-tokens')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace XtendPackages\RESTPresenter\StarterKits\Filament\Resources\UserResource\Pages;

use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use XtendPackages\RESTPresenter\StarterKits\Filament\Resources\UserResource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Laravel\Sanctum\PersonalAccessToken;

class TokensRelationManager extends RelationManager
{
Expand All @@ -25,11 +26,21 @@ public function table(Table $table): Table
Tables\Columns\TextColumn::make('id'),
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('abilities')
->badge()
->color(fn (string $state): string => match ($state) {
'*' => 'success',
default => 'info',
})
->listWithLineBreaks(),
Tables\Columns\TextColumn::make('created_at')
->dateTime(),
Tables\Columns\TextColumn::make('expires_at')
->dateTime(),
Tables\Columns\IconColumn::make('expired')
->boolean()
->color(fn (bool $state): string => $state ? 'danger' : 'gray')
->getStateUsing(fn (PersonalAccessToken $record) => $record->expires_at->isPast())
->alignCenter(),
])
->filters([
//
Expand Down

0 comments on commit d28e74c

Please sign in to comment.