forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiTokens.php
More file actions
138 lines (125 loc) · 4.89 KB
/
ApiTokens.php
File metadata and controls
138 lines (125 loc) · 4.89 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
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace App\Filament\Pages;
use Carbon\Carbon;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Concerns\InteractsWithInfolists;
use Filament\Infolists\Contracts\HasInfolists;
use Filament\Infolists\Infolist;
use Filament\Pages\Page;
use Filament\Support\Enums\FontFamily;
use Filament\Support\Enums\MaxWidth;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\HtmlString;
class ApiTokens extends Page implements HasForms, HasInfolists, HasTable
{
use InteractsWithForms, InteractsWithInfolists, InteractsWithTable;
protected static ?string $navigationIcon = 'tabler-api';
protected static string $view = 'filament.pages.api-tokens';
protected static ?string $title = 'API Tokens';
protected static ?string $navigationGroup = 'Settings';
protected static ?int $navigationSort = 5;
public ?string $token = '';
/**
* NOTE: This page is disabled until the api feature is ready.
*/
public static function canAccess(): bool
{
return false;
}
public function tokenInfolist(Infolist $infolist): Infolist
{
return $infolist
->state([
'token' => $this->token,
])
->schema([
Section::make()
->columns(1)
->schema([
TextEntry::make('token')
->label('API Token')
->formatStateUsing(fn (string $state) => explode('|', $state)[1])
->helperText('Copy and save the token above, this token will not be shown again!')
->color('gray')
->copyable()
->copyableState(fn (string $state) => explode('|', $state)[1])
->fontFamily(FontFamily::Mono),
]),
]);
}
public function table(Table $table): Table
{
return $table
->relationship(fn (): MorphMany => Auth::user()->tokens())
->headerActions([
Action::make('createToken')
->form([
TextInput::make('token_name')
->label('Name')
->maxLength('100')
->required(),
CheckboxList::make('token_abilities')
->label('Abilities')
->options([
'result-read' => 'Read results',
])
->descriptions([
'result-read' => new HtmlString('Allow the token to retrieve result data.'),
])
->required()
->bulkToggleable(),
DateTimePicker::make('token_expires_at')
->label('Expires at')
->nullable(),
])
->action(function (array $data) {
$token = Auth::user()->createToken(
name: $data['token_name'],
abilities: $data['token_abilities'],
expiresAt: $data['token_expires_at'] ? Carbon::parse($data['token_expires_at']) : null,
);
$this->token = $token->plainTextToken;
})
->label('Create API Token')
->modal('createToken')
->modalWidth(MaxWidth::ExtraLarge),
])
->columns([
TextColumn::make('id')
->label('ID')
->sortable(),
TextColumn::make('name')
->searchable(),
TextColumn::make('last_used_at')
->alignEnd()
->dateTime()
->sortable(),
TextColumn::make('expires_at')
->alignEnd()
->dateTime()
->sortable(),
])
->actions([
ActionGroup::make([
DeleteAction::make(),
]),
])
->bulkActions([
// ...
]);
}
}