forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiTokenResource.php
More file actions
158 lines (144 loc) · 5.83 KB
/
ApiTokenResource.php
File metadata and controls
158 lines (144 loc) · 5.83 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\ApiTokenResource\Pages;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Laravel\Sanctum\PersonalAccessToken;
class ApiTokenResource extends Resource
{
protected static ?string $model = PersonalAccessToken::class;
protected static ?string $navigationIcon = 'tabler-api';
protected static ?string $navigationGroup = 'Settings';
protected static ?string $label = 'API Token';
protected static ?string $pluralLabel = 'API Tokens';
public static function getTokenFormSchema(): array
{
return [
Grid::make()
->schema([
TextInput::make('name')
->label('Name')
->unique(ignoreRecord: true)
->maxLength(100)
->required(),
CheckboxList::make('abilities')
->label('Abilities')
->options([
'results:read' => 'Read results',
'speedtests:run' => 'Run speedtest',
'ookla:list-servers' => 'List servers',
])
->required()
->bulkToggleable()
->descriptions([
'results:read' => 'Allow this token to read results.',
'speedtests:run' => 'Allow this token to run speedtests.',
'ookla:list-servers' => 'Allow this token to list servers.',
]),
DateTimePicker::make('expires_at')
->label('Expires at')
->nullable()
->native(false)
->helperText('Leave empty for no expiration'),
])
->columns([
'lg' => 1,
]),
];
}
public static function form(Form $form): Form
{
return $form->schema(static::getTokenFormSchema());
}
public static function table(Table $table): Table
{
return $table
->query(PersonalAccessToken::query()->where('tokenable_id', Auth::id()))
->columns([
TextColumn::make('name')->searchable(),
TextColumn::make('abilities')->badge(),
TextColumn::make('created_at')
->dateTime(config('app.datetime_format'))
->timezone(config('app.display_timezone'))
->toggleable()
->sortable()
->alignEnd(),
TextColumn::make('last_used_at')
->dateTime(config('app.datetime_format'))
->timezone(config('app.display_timezone'))
->toggleable()
->toggledHiddenByDefault()
->sortable()
->alignEnd(),
TextColumn::make('expires_at')
->dateTime(config('app.datetime_format'))
->timezone(config('app.display_timezone'))
->toggleable()
->sortable()
->alignEnd(),
])
->filters([
TernaryFilter::make('expired')
->label('Token Status')
->placeholder('All tokens')
->falseLabel('Active tokens')
->trueLabel('Expired tokens')
->native(false)
->queries(
true: fn (Builder $query) => $query
->where('expires_at', '<=', now()),
false: fn (Builder $query) => $query
->where(function (Builder $q) {
$q->whereNull('expires_at')
->orWhere('expires_at', '>', now());
}),
blank: fn (Builder $query) => $query,
),
SelectFilter::make('abilities')
->label('Abilities')
->multiple()
->options([
'results:read' => 'Read results',
'speedtests:run' => 'Run speedtest',
'ookla:list-servers' => 'List servers',
])
->query(function (Builder $query, array $data): Builder {
foreach ($data['values'] ?? [] as $value) {
$query->whereJsonContains('abilities', $value);
}
return $query;
}),
])
->actions([
ActionGroup::make([
EditAction::make()
->disabled(fn ($record) => $record->expires_at !== null && $record->expires_at->isPast())
->modalWidth('xl'),
DeleteAction::make(),
]),
])
->bulkActions([
DeleteBulkAction::make(),
]);
}
public static function getPages(): array
{
return [
'index' => Pages\ListApiTokens::route('/'),
];
}
}