2024-04-08 00:59:55 -04:00
|
|
|
<?php
|
|
|
|
|
|
2024-12-07 15:51:27 +01:00
|
|
|
namespace App\Filament\Admin\Resources\ApiKeyResource\Pages;
|
2024-04-08 00:59:55 -04:00
|
|
|
|
2024-12-07 15:51:27 +01:00
|
|
|
use App\Filament\Admin\Resources\ApiKeyResource;
|
2024-04-23 19:45:11 -04:00
|
|
|
use App\Models\ApiKey;
|
2024-06-29 17:38:18 -04:00
|
|
|
use Filament\Forms\Components\Fieldset;
|
|
|
|
|
use Filament\Forms\Components\Hidden;
|
|
|
|
|
use Filament\Forms\Components\TagsInput;
|
|
|
|
|
use Filament\Forms\Components\Textarea;
|
|
|
|
|
use Filament\Forms\Components\ToggleButtons;
|
2024-04-23 19:45:11 -04:00
|
|
|
use Filament\Forms\Form;
|
2024-04-08 00:59:55 -04:00
|
|
|
use Filament\Resources\Pages\CreateRecord;
|
2024-11-06 09:09:10 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-04-08 00:59:55 -04:00
|
|
|
|
|
|
|
|
class CreateApiKey extends CreateRecord
|
|
|
|
|
{
|
|
|
|
|
protected static string $resource = ApiKeyResource::class;
|
2024-04-12 13:05:04 -04:00
|
|
|
|
2024-12-12 18:26:37 +01:00
|
|
|
protected static bool $canCreateAnother = false;
|
|
|
|
|
|
|
|
|
|
protected function getHeaderActions(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
$this->getCreateFormAction()->formId('form'),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getFormActions(): array
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2024-04-23 19:45:11 -04:00
|
|
|
|
|
|
|
|
public function form(Form $form): Form
|
|
|
|
|
{
|
|
|
|
|
return $form
|
|
|
|
|
->schema([
|
2024-06-29 17:38:18 -04:00
|
|
|
Hidden::make('identifier')->default(ApiKey::generateTokenIdentifier(ApiKey::TYPE_APPLICATION)),
|
|
|
|
|
Hidden::make('token')->default(str_random(ApiKey::KEY_LENGTH)),
|
2024-04-23 19:45:11 -04:00
|
|
|
|
2024-06-29 17:38:18 -04:00
|
|
|
Hidden::make('user_id')
|
2024-04-23 19:45:11 -04:00
|
|
|
->default(auth()->user()->id)
|
|
|
|
|
->required(),
|
|
|
|
|
|
2024-06-29 17:38:18 -04:00
|
|
|
Hidden::make('key_type')
|
2024-05-10 16:42:05 -04:00
|
|
|
->inlineLabel()
|
2024-05-31 01:59:33 -04:00
|
|
|
->default(ApiKey::TYPE_APPLICATION)
|
|
|
|
|
->required(),
|
2024-04-23 19:45:11 -04:00
|
|
|
|
2024-06-29 17:38:18 -04:00
|
|
|
Fieldset::make('Permissions')
|
2024-04-23 19:45:11 -04:00
|
|
|
->columns([
|
|
|
|
|
'default' => 1,
|
|
|
|
|
'sm' => 1,
|
|
|
|
|
'md' => 2,
|
|
|
|
|
])
|
|
|
|
|
->schema(
|
2024-11-06 09:09:10 +01:00
|
|
|
collect(ApiKey::getPermissionList())->map(fn ($resource) => ToggleButtons::make('permissions_' . $resource)
|
2024-05-10 16:42:05 -04:00
|
|
|
->label(str($resource)->replace('_', ' ')->title())->inline()
|
2024-04-23 19:45:11 -04:00
|
|
|
->options([
|
2025-02-08 23:16:54 -05:00
|
|
|
0 => trans('admin/apikey.permissions.none'),
|
|
|
|
|
1 => trans('admin/apikey.permissions.read'),
|
|
|
|
|
// 2 => 'Write', // Makes no sense to have write-only permissions when you can't read it?
|
|
|
|
|
3 => trans('admin/apikey.permissions.read_write'),
|
2024-04-23 19:45:11 -04:00
|
|
|
])
|
|
|
|
|
->icons([
|
|
|
|
|
0 => 'tabler-book-off',
|
|
|
|
|
1 => 'tabler-book',
|
|
|
|
|
2 => 'tabler-writing',
|
|
|
|
|
3 => 'tabler-writing',
|
|
|
|
|
])
|
|
|
|
|
->colors([
|
|
|
|
|
0 => 'success',
|
|
|
|
|
1 => 'warning',
|
|
|
|
|
2 => 'danger',
|
|
|
|
|
3 => 'danger',
|
|
|
|
|
])
|
|
|
|
|
->required()
|
|
|
|
|
->columnSpan([
|
|
|
|
|
'default' => 1,
|
|
|
|
|
'sm' => 1,
|
|
|
|
|
'md' => 1,
|
|
|
|
|
])
|
|
|
|
|
->default(0),
|
|
|
|
|
)->all(),
|
|
|
|
|
),
|
|
|
|
|
|
2024-06-29 17:38:18 -04:00
|
|
|
TagsInput::make('allowed_ips')
|
2025-02-08 23:16:54 -05:00
|
|
|
->placeholder('127.0.0.1 or 192.168.1.1')
|
|
|
|
|
->label(trans('admin/apikey.whitelist'))
|
|
|
|
|
->helperText(trans('admin/apikey.whitelist_help'))
|
2024-06-13 08:21:56 +02:00
|
|
|
->columnSpanFull(),
|
2024-04-23 19:45:11 -04:00
|
|
|
|
2024-06-29 17:38:18 -04:00
|
|
|
Textarea::make('memo')
|
2024-04-23 19:45:11 -04:00
|
|
|
->required()
|
2025-02-08 23:16:54 -05:00
|
|
|
->label(trans('admin/apikey.description'))
|
|
|
|
|
->helperText(trans('admin/apikey.description_help'))
|
2024-04-23 19:45:11 -04:00
|
|
|
->columnSpanFull(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
2024-11-06 09:09:10 +01:00
|
|
|
|
|
|
|
|
protected function handleRecordCreation(array $data): Model
|
|
|
|
|
{
|
|
|
|
|
$permissions = [];
|
|
|
|
|
|
|
|
|
|
foreach (ApiKey::getPermissionList() as $permission) {
|
|
|
|
|
if (isset($data['permissions_' . $permission])) {
|
|
|
|
|
$permissions[$permission] = intval($data['permissions_' . $permission]);
|
|
|
|
|
unset($data['permissions_' . $permission]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data['permissions'] = $permissions;
|
|
|
|
|
|
|
|
|
|
return parent::handleRecordCreation($data);
|
|
|
|
|
}
|
2024-04-08 00:59:55 -04:00
|
|
|
}
|