-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManageApiTokens.php
More file actions
126 lines (106 loc) · 3.37 KB
/
ManageApiTokens.php
File metadata and controls
126 lines (106 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Backstage\Filament\Users\Pages;
use BackedEnum;
use Backstage\Filament\Users\Models\User;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Pages\Concerns\InteractsWithFormActions;
use Filament\Pages\Page;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
class ManageApiTokens extends Page implements HasTable
{
use InteractsWithFormActions;
use InteractsWithSchemas;
use InteractsWithTable;
public static function getNavigationIcon(): string | BackedEnum | Htmlable | null
{
return Heroicon::OutlinedDocumentText;
}
public string $view = 'backstage/users::pages.manage-api-tokens';
public array $data = [];
public static function shouldRegisterNavigation(): bool
{
return false;
}
public static function canAccess(): bool
{
return config('backstage.users.record.manage-api-tokens', false);
}
protected function getTableQuery(): Builder | Relation | null
{
return Filament::auth()->user()->tokens()->getQuery();
}
public function getFormActions(): array
{
return [
$this->getSubmitFormActions(),
];
}
public function getSubmitFormActions(): Action
{
return Action::make('submit')
->label(__('Create'))
->action('create')
->color('primary')
->icon('heroicon-o-plus')
->requiresConfirmation();
}
public function content(Schema $schema): Schema
{
return $schema
->schema([
TextInput::make('name')
->label(__('Name'))
->required()
->maxLength(255)
->placeholder(__('Enter token name')),
$this->getSubmitFormActions(),
])
->statePath('data');
}
public function create()
{
$state = $this->content->getState();
/**
* @var User $user
*/
$user = Filament::auth()->user();
$token = $user->createToken($state['name']);
Notification::make()
->title(__('Token created'))
->body(__('Please save this token: :token', ['token' => $token->plainTextToken]))
->success()
->persistent()
->send();
$this->content->fill();
}
public function table(Table $table): Table
{
return $table
->query($this->getTableQuery())
->columns([
TextColumn::make('name')
->label(__('Name'))
->sortable()
->searchable()
->placeholder(__('Enter token name')),
TextColumn::make('created_at')
->label(__('Created'))
->sortable()
->dateTime()
->since()
->placeholder(__('Enter token name')),
]);
}
}