generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from mailcarrierapp/feat/token-dashboard
[2.x] Add tokens dashboard
- Loading branch information
Showing
5 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace MailCarrier\Resources; | ||
|
||
use Filament\Resources\Resource; | ||
use Filament\Tables; | ||
use Laravel\Sanctum\PersonalAccessToken; | ||
use MailCarrier\Resources\ApiTokenResource\Pages; | ||
|
||
class ApiTokenResource extends Resource | ||
{ | ||
protected static ?string $model = PersonalAccessToken::class; | ||
|
||
protected static ?string $modelLabel = 'API Tokens'; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-key'; | ||
|
||
protected static ?string $navigationGroup = 'Management'; | ||
|
||
/** | ||
* List all the records. | ||
*/ | ||
public static function table(Tables\Table $table): Tables\Table | ||
{ | ||
return $table | ||
->columns([ | ||
Tables\Columns\TextColumn::make('name') | ||
->placeholder('No name provided'), | ||
|
||
Tables\Columns\TextColumn::make('expires_at') | ||
->label('Expiration date (UTC)') | ||
->dateTime() | ||
->placeholder('Never expires'), | ||
|
||
Tables\Columns\TextColumn::make('last_used_at') | ||
->dateTime() | ||
->placeholder('Never used'), | ||
]) | ||
->actions([ | ||
Tables\Actions\DeleteAction::make(), | ||
]); | ||
} | ||
|
||
/** | ||
* Get Filament CRUD pages. | ||
*/ | ||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListApiTokens::route('/'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace MailCarrier\Resources\ApiTokenResource\Actions; | ||
|
||
use Carbon\Carbon; | ||
use Filament\Actions\Action; | ||
use Filament\Actions\Concerns\CanCustomizeProcess; | ||
use Filament\Forms; | ||
use Filament\Support\Enums\Alignment; | ||
use Filament\Support\Enums\MaxWidth; | ||
use MailCarrier\Actions\Auth\GenerateToken; | ||
use MailCarrier\Resources\ApiTokenResource; | ||
|
||
class CreateAction extends Action | ||
{ | ||
use CanCustomizeProcess; | ||
|
||
const GENERATED_TOKEN_FIELD_NAME = 'generated_token'; | ||
|
||
public static function getDefaultName(): ?string | ||
{ | ||
return 'create'; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->label(fn (): string => __('filament-actions::create.single.label', [ | ||
'label' => 'API Token', | ||
])); | ||
|
||
$this->authorize(fn (): bool => ApiTokenResource::canCreate()); | ||
|
||
$this->modalHeading(fn (): string => __('filament-actions::create.single.modal.heading', [ | ||
'label' => 'API Token', | ||
])); | ||
|
||
$this->modalSubmitActionLabel(__('filament-actions::create.single.modal.actions.create.label')); | ||
|
||
$this->successNotificationTitle(__('filament-actions::create.single.notifications.created.title')); | ||
|
||
$this->modalFooterActionsAlignment(Alignment::End); | ||
|
||
$this->modalWidth(MaxWidth::Large); | ||
|
||
$this->record(null); | ||
|
||
$this->form([ | ||
Forms\Components\TextInput::make('name') | ||
->hidden(fn (Forms\Get $get) => !is_null($get(static::GENERATED_TOKEN_FIELD_NAME))) | ||
->required(), | ||
|
||
Forms\Components\DateTimePicker::make('expires_at') | ||
->minDate(Carbon::now()) | ||
->label('Expiration date (UTC)') | ||
->hidden(fn (Forms\Get $get) => !is_null($get(static::GENERATED_TOKEN_FIELD_NAME))) | ||
->helperText('Leave empty to never expire'), | ||
|
||
Forms\Components\TextInput::make('generated_token') | ||
->readOnly() | ||
->dehydrated(false) | ||
->helperText('Copy it to a safe place, it won\'t be shown anymore') | ||
->extraInputAttributes([ | ||
'onClick' => 'this.select()', | ||
]) | ||
->visible(fn (Forms\Get $get) => !is_null($get(static::GENERATED_TOKEN_FIELD_NAME))), | ||
]); | ||
|
||
$this->action(function (array $data, Forms\Form $form): void { | ||
$form->fill([ | ||
static::GENERATED_TOKEN_FIELD_NAME => GenerateToken::resolve()->run( | ||
$data['name'], | ||
expiresAt: $data['expires_at'], | ||
), | ||
]); | ||
|
||
$this->modalSubmitAction(false); | ||
$this->modalCancelActionLabel('Close'); | ||
$this->sendSuccessNotification(); | ||
$this->halt(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace MailCarrier\Resources\ApiTokenResource\Pages; | ||
|
||
use Filament\Resources\Pages\ListRecords; | ||
use MailCarrier\Resources\ApiTokenResource; | ||
use MailCarrier\Resources\ApiTokenResource\Actions\CreateAction; | ||
|
||
class ListApiTokens extends ListRecords | ||
{ | ||
protected static string $resource = ApiTokenResource::class; | ||
|
||
protected function getHeaderActions(): array | ||
{ | ||
return [ | ||
CreateAction::make(), | ||
]; | ||
} | ||
} |