2024-04-08 00:59:55 -04:00
|
|
|
<?php
|
|
|
|
|
|
2025-09-08 13:12:33 -04:00
|
|
|
namespace App\Filament\Admin\Resources\ApiKeys\Pages;
|
2024-04-08 00:59:55 -04:00
|
|
|
|
2026-01-27 11:36:07 +01:00
|
|
|
use App\Enums\TablerIcon;
|
2025-09-08 13:12:33 -04:00
|
|
|
use App\Filament\Admin\Resources\ApiKeys\ApiKeyResource;
|
2024-04-23 19:45:11 -04:00
|
|
|
use App\Models\ApiKey;
|
2025-06-19 18:24:25 +02:00
|
|
|
use App\Traits\Filament\CanCustomizeHeaderActions;
|
|
|
|
|
use App\Traits\Filament\CanCustomizeHeaderWidgets;
|
|
|
|
|
use Filament\Actions\Action;
|
|
|
|
|
use Filament\Actions\ActionGroup;
|
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;
|
2025-09-06 16:47:54 -04:00
|
|
|
use Illuminate\Support\Str;
|
2024-04-08 00:59:55 -04:00
|
|
|
|
|
|
|
|
class CreateApiKey extends CreateRecord
|
|
|
|
|
{
|
2025-06-19 18:24:25 +02:00
|
|
|
use CanCustomizeHeaderActions;
|
|
|
|
|
use CanCustomizeHeaderWidgets;
|
|
|
|
|
|
2024-04-08 00:59:55 -04:00
|
|
|
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;
|
|
|
|
|
|
2025-06-19 18:24:25 +02:00
|
|
|
/** @return array<Action|ActionGroup> */
|
|
|
|
|
protected function getDefaultHeaderActions(): array
|
2024-12-12 18:26:37 +01:00
|
|
|
{
|
|
|
|
|
return [
|
2026-01-27 20:07:18 -05:00
|
|
|
Action::make('create')
|
|
|
|
|
->hiddenLabel()
|
|
|
|
|
->action('create')
|
|
|
|
|
->keyBindings(['mod+s'])
|
|
|
|
|
->tooltip(trans('filament-panels::resources/pages/create-record.form.actions.create.label'))
|
2026-01-27 11:36:07 +01:00
|
|
|
->icon(TablerIcon::FilePlus),
|
2024-12-12 18:26:37 +01:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getFormActions(): array
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2024-04-23 19:45:11 -04:00
|
|
|
|
2024-11-06 09:09:10 +01:00
|
|
|
protected function handleRecordCreation(array $data): Model
|
|
|
|
|
{
|
2025-02-24 15:44:47 +01:00
|
|
|
$data['identifier'] = ApiKey::generateTokenIdentifier(ApiKey::TYPE_APPLICATION);
|
2025-09-06 16:47:54 -04:00
|
|
|
$data['token'] = Str::random(ApiKey::KEY_LENGTH);
|
2025-10-07 23:12:31 +02:00
|
|
|
$data['user_id'] = user()?->id;
|
2025-02-24 15:44:47 +01:00
|
|
|
$data['key_type'] = ApiKey::TYPE_APPLICATION;
|
|
|
|
|
|
2024-11-06 09:09:10 +01:00
|
|
|
$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
|
|
|
}
|