mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-03 06:56:15 +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)),
|
||||
]);
|
||||
}
|
||||
}
|
@ -15,9 +15,7 @@
|
||||
|
||||
class NotificationChannelsList extends Widget
|
||||
{
|
||||
protected $listeners = [
|
||||
'$refresh' => 'refreshTable',
|
||||
];
|
||||
protected $listeners = ['$refresh'];
|
||||
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
use App\Models\Project;
|
||||
use Filament\Widgets\Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class SelectProject extends Widget
|
||||
@ -20,15 +19,7 @@ class SelectProject extends Widget
|
||||
public function mount(): void
|
||||
{
|
||||
$this->currentProject = auth()->user()->currentProject;
|
||||
$this->projects = Project::query()
|
||||
->where(function (Builder $query) {
|
||||
if (auth()->user()->isAdmin()) {
|
||||
return;
|
||||
}
|
||||
$query->where('user_id', auth()->id())
|
||||
->orWhereHas('users', fn ($query) => $query->where('user_id', auth()->id()));
|
||||
})
|
||||
->get();
|
||||
$this->projects = auth()->user()->allProjects()->get();
|
||||
}
|
||||
|
||||
public function updateProject(Project $project): void
|
||||
|
@ -8,6 +8,7 @@
|
||||
use Filament\Forms\Components\Checkbox;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Notifications\Notification;
|
||||
|
||||
class Create
|
||||
@ -23,29 +24,22 @@ public static function form(): array
|
||||
)
|
||||
->live()
|
||||
->reactive()
|
||||
->rules(CreateServerProvider::rules()['provider']),
|
||||
->rules(fn (Get $get) => CreateServerProvider::rules($get())['provider']),
|
||||
TextInput::make('name')
|
||||
->rules(CreateServerProvider::rules()['name']),
|
||||
->rules(fn (Get $get) => CreateServerProvider::rules($get())['name']),
|
||||
TextInput::make('token')
|
||||
->label('API Key')
|
||||
->validationAttribute('API Key')
|
||||
->visible(fn ($get) => in_array($get('provider'), [
|
||||
ServerProvider::DIGITALOCEAN,
|
||||
ServerProvider::LINODE,
|
||||
ServerProvider::VULTR,
|
||||
ServerProvider::HETZNER,
|
||||
]))
|
||||
->rules(fn ($get) => CreateServerProvider::providerRules($get())['token']),
|
||||
->visible(fn ($get) => isset(CreateServerProvider::rules($get())['token']))
|
||||
->rules(fn (Get $get) => CreateServerProvider::rules($get())['token']),
|
||||
TextInput::make('key')
|
||||
->label('Access Key')
|
||||
->visible(function ($get) {
|
||||
return $get('provider') == ServerProvider::AWS;
|
||||
})
|
||||
->rules(fn ($get) => CreateServerProvider::providerRules($get())['key']),
|
||||
->visible(fn ($get) => isset(CreateServerProvider::rules($get())['key']))
|
||||
->rules(fn (Get $get) => CreateServerProvider::rules($get())['key']),
|
||||
TextInput::make('secret')
|
||||
->label('Secret')
|
||||
->visible(fn ($get) => $get('provider') == ServerProvider::AWS)
|
||||
->rules(fn ($get) => CreateServerProvider::providerRules($get())['secret']),
|
||||
->visible(fn ($get) => isset(CreateServerProvider::rules($get())['secret']))
|
||||
->rules(fn (Get $get) => CreateServerProvider::rules($get())['secret']),
|
||||
Checkbox::make('global')
|
||||
->label('Is Global (Accessible in all projects)'),
|
||||
];
|
||||
@ -57,7 +51,7 @@ public static function form(): array
|
||||
public static function action(array $data): void
|
||||
{
|
||||
try {
|
||||
app(CreateServerProvider::class)->create(auth()->user(), $data);
|
||||
app(CreateServerProvider::class)->create(auth()->user(), auth()->user()->currentProject, $data);
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->title($e->getMessage())
|
||||
|
@ -22,6 +22,6 @@ public static function form(): array
|
||||
|
||||
public static function action(ServerProvider $provider, array $data): void
|
||||
{
|
||||
app(EditServerProvider::class)->edit($provider, auth()->user(), $data);
|
||||
app(EditServerProvider::class)->edit($provider, auth()->user()->currentProject, $data);
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ public static function form(): array
|
||||
public static function action(array $data): void
|
||||
{
|
||||
try {
|
||||
app(ConnectSourceControl::class)->connect(auth()->user(), $data);
|
||||
app(ConnectSourceControl::class)->connect(auth()->user(), auth()->user()->currentProject, $data);
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->title($e->getMessage())
|
||||
|
@ -2,9 +2,8 @@
|
||||
|
||||
namespace App\Web\Pages\Settings\SourceControls\Actions;
|
||||
|
||||
use App\Actions\SourceControl\ConnectSourceControl;
|
||||
use App\Actions\SourceControl\EditSourceControl;
|
||||
use App\Enums\SourceControl;
|
||||
use App\Models\SourceControl;
|
||||
use Exception;
|
||||
use Filament\Forms\Components\Checkbox;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
@ -13,30 +12,27 @@
|
||||
|
||||
class Edit
|
||||
{
|
||||
public static function form(): array
|
||||
public static function form(SourceControl $sourceControl): array
|
||||
{
|
||||
return [
|
||||
TextInput::make('name')
|
||||
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['name']),
|
||||
->rules(fn (Get $get) => EditSourceControl::rules($sourceControl, $get())['name']),
|
||||
TextInput::make('token')
|
||||
->label('API Key')
|
||||
->validationAttribute('API Key')
|
||||
->visible(fn ($get) => in_array($get('provider'), [
|
||||
SourceControl::GITHUB,
|
||||
SourceControl::GITLAB,
|
||||
]))
|
||||
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['token']),
|
||||
->visible(fn (Get $get) => EditSourceControl::rules($sourceControl, $get())['token'] ?? false)
|
||||
->rules(fn (Get $get) => EditSourceControl::rules($sourceControl, $get())['token']),
|
||||
TextInput::make('url')
|
||||
->label('URL (optional)')
|
||||
->visible(fn ($get) => $get('provider') == SourceControl::GITLAB)
|
||||
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['url'])
|
||||
->visible(fn (Get $get) => EditSourceControl::rules($sourceControl, $get())['url'] ?? false)
|
||||
->rules(fn (Get $get) => EditSourceControl::rules($sourceControl, $get())['url'])
|
||||
->helperText('If you run a self-managed gitlab enter the url here, leave empty to use gitlab.com'),
|
||||
TextInput::make('username')
|
||||
->visible(fn ($get) => $get('provider') == SourceControl::BITBUCKET)
|
||||
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['username']),
|
||||
->visible(fn (Get $get) => EditSourceControl::rules($sourceControl, $get())['username'] ?? false)
|
||||
->rules(fn (Get $get) => EditSourceControl::rules($sourceControl, $get())['username']),
|
||||
TextInput::make('password')
|
||||
->visible(fn ($get) => $get('provider') == SourceControl::BITBUCKET)
|
||||
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['password']),
|
||||
->visible(fn (Get $get) => EditSourceControl::rules($sourceControl, $get())['password'] ?? false)
|
||||
->rules(fn (Get $get) => EditSourceControl::rules($sourceControl, $get())['password']),
|
||||
Checkbox::make('global')
|
||||
->label('Is Global (Accessible in all projects)'),
|
||||
];
|
||||
@ -45,10 +41,10 @@ public static function form(): array
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function action(\App\Models\SourceControl $sourceControl, array $data): void
|
||||
public static function action(SourceControl $sourceControl, array $data): void
|
||||
{
|
||||
try {
|
||||
app(EditSourceControl::class)->edit($sourceControl, auth()->user(), $data);
|
||||
app(EditSourceControl::class)->edit($sourceControl, auth()->user()->currentProject, $data);
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->title($e->getMessage())
|
||||
|
@ -69,7 +69,7 @@ public function getTable(): Table
|
||||
'global' => $record->project_id === null,
|
||||
];
|
||||
})
|
||||
->form(Edit::form())
|
||||
->form(fn (SourceControl $record) => Edit::form($record))
|
||||
->authorize(fn (SourceControl $record) => auth()->user()->can('update', $record))
|
||||
->using(fn (array $data, SourceControl $record) => Edit::action($record, $data))
|
||||
->modalWidth(MaxWidth::Medium),
|
||||
|
@ -126,7 +126,7 @@ public static function form(): array
|
||||
public static function action(array $data): void
|
||||
{
|
||||
try {
|
||||
app(CreateStorageProvider::class)->create(auth()->user(), $data);
|
||||
app(CreateStorageProvider::class)->create(auth()->user(), auth()->user()->currentProject, $data);
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->title($e->getMessage())
|
||||
|
@ -22,6 +22,6 @@ public static function form(): array
|
||||
|
||||
public static function action(StorageProvider $provider, array $data): void
|
||||
{
|
||||
app(EditStorageProvider::class)->edit($provider, auth()->user(), $data);
|
||||
app(EditStorageProvider::class)->edit($provider, auth()->user()->currentProject, $data);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user