mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
API Feature (#334)
This commit is contained in:
115
app/Web/Pages/Settings/APIKeys/Index.php
Normal file
115
app/Web/Pages/Settings/APIKeys/Index.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\APIKeys;
|
||||
|
||||
use App\Models\PersonalAccessToken;
|
||||
use App\Web\Components\Page;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\Radio;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
|
||||
class Index extends Page
|
||||
{
|
||||
protected static ?string $navigationGroup = 'Settings';
|
||||
|
||||
protected static ?string $slug = 'settings/api-keys';
|
||||
|
||||
protected static ?string $title = 'API Keys';
|
||||
|
||||
protected static ?string $navigationIcon = 'icon-plug';
|
||||
|
||||
protected static ?int $navigationSort = 11;
|
||||
|
||||
public string $token = '';
|
||||
|
||||
protected $listeners = ['$refresh'];
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
return auth()->user()?->can('viewAny', PersonalAccessToken::class) ?? false;
|
||||
}
|
||||
|
||||
public function getWidgets(): array
|
||||
{
|
||||
return [
|
||||
[Widgets\ApiKeysList::class],
|
||||
];
|
||||
}
|
||||
|
||||
public function unmountAction(bool $shouldCancelParentActions = true, bool $shouldCloseModal = true): void
|
||||
{
|
||||
parent::unmountAction($shouldCancelParentActions, $shouldCloseModal);
|
||||
|
||||
$this->token = '';
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Action::make('read-the-docs')
|
||||
->label('Read the Docs')
|
||||
->icon('heroicon-o-document-text')
|
||||
->color('gray')
|
||||
->url(config('scribe.static.url'))
|
||||
->openUrlInNewTab(),
|
||||
Action::make('create')
|
||||
->label('Create new Key')
|
||||
->icon('heroicon-o-plus')
|
||||
->modalHeading('Create a new Key')
|
||||
->modalSubmitActionLabel('Create')
|
||||
->form(function () {
|
||||
if ($this->token) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
TextInput::make('name')
|
||||
->label('Token Name')
|
||||
->required(),
|
||||
Radio::make('permission')
|
||||
->options([
|
||||
'read' => 'Read',
|
||||
'write' => 'Read & Write',
|
||||
])
|
||||
->required(),
|
||||
];
|
||||
})
|
||||
->infolist(function () {
|
||||
if ($this->token) {
|
||||
return [
|
||||
TextEntry::make('token')
|
||||
->state($this->token)
|
||||
->tooltip('Copy')
|
||||
->copyable()
|
||||
->helperText('You can see the token only one!'),
|
||||
];
|
||||
}
|
||||
|
||||
return [];
|
||||
})
|
||||
->authorize('create', PersonalAccessToken::class)
|
||||
->modalWidth(MaxWidth::Large)
|
||||
->action(function (array $data) {
|
||||
$permissions = ['read'];
|
||||
if ($data['permission'] === 'write') {
|
||||
$permissions[] = 'write';
|
||||
}
|
||||
$token = auth()->user()->createToken($data['name'], $permissions);
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
|
||||
$this->token = $token->plainTextToken;
|
||||
|
||||
$this->halt();
|
||||
})
|
||||
->modalSubmitAction(function () {
|
||||
if ($this->token) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
->closeModalByClickingAway(fn () => ! $this->token),
|
||||
];
|
||||
}
|
||||
}
|
62
app/Web/Pages/Settings/APIKeys/Widgets/ApiKeysList.php
Normal file
62
app/Web/Pages/Settings/APIKeys/Widgets/ApiKeysList.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\APIKeys\Widgets;
|
||||
|
||||
use App\Models\PersonalAccessToken;
|
||||
use Filament\Tables\Actions\DeleteAction;
|
||||
use Filament\Tables\Actions\DeleteBulkAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ApiKeysList extends Widget
|
||||
{
|
||||
protected $listeners = ['$refresh'];
|
||||
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
return auth()->user()->tokens()->getQuery();
|
||||
}
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
TextColumn::make('name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('abilities')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('created_at')
|
||||
->label('Created At')
|
||||
->formatStateUsing(fn (PersonalAccessToken $record) => $record->created_at_by_timezone)
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('last_used_at')
|
||||
->label('Last Used At')
|
||||
->formatStateUsing(fn (PersonalAccessToken $record) => $record->getDateTimeByTimezone($record->last_used_at))
|
||||
->searchable()
|
||||
->sortable(),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table
|
||||
->heading('')
|
||||
->actions([
|
||||
DeleteAction::make('delete')
|
||||
->modalHeading('Delete Token')
|
||||
->authorize(fn (PersonalAccessToken $record) => auth()->user()->can('delete', $record))
|
||||
->using(function (array $data, PersonalAccessToken $record) {
|
||||
$record->delete();
|
||||
}),
|
||||
])
|
||||
->bulkActions([
|
||||
DeleteBulkAction::make()
|
||||
->requiresConfirmation()
|
||||
->authorize(auth()->user()->can('deleteMany', PersonalAccessToken::class)),
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user