-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathServerAccess.php
More file actions
194 lines (157 loc) · 5.58 KB
/
ServerAccess.php
File metadata and controls
194 lines (157 loc) · 5.58 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace App\Livewire\User;
use App\Models\DatabaseServer;
use App\Models\Snapshot;
use App\Models\User;
use App\Models\UserServerAccess;
use App\Traits\Toast;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
class ServerAccess extends Component
{
use AuthorizesRequests, Toast;
#[Locked]
public User $user;
public bool $showGrantModal = false;
public string $selectedServerId = '';
/** @var array<int, string> */
public array $allowedDatabases = [];
public string $databaseSearch = '';
public bool $canDownload = true;
public bool $canBackup = false;
public bool $canRestore = false;
public function mount(User $user): void
{
$this->user = $user;
}
public function openGrantModal(?string $accessId = null): void
{
$this->authorize('update', $this->user);
$this->reset(['selectedServerId', 'allowedDatabases', 'databaseSearch', 'canDownload', 'canBackup', 'canRestore']);
$this->canDownload = true;
if ($accessId !== null) {
$access = UserServerAccess::where('id', $accessId)
->where('user_id', $this->user->id)
->firstOrFail();
$this->selectedServerId = $access->database_server_id;
$this->allowedDatabases = $access->allowed_databases ?? [];
$this->canDownload = $access->can_download;
$this->canBackup = $access->can_backup;
$this->canRestore = $access->can_restore;
}
$this->showGrantModal = true;
}
public function updatedSelectedServerId(): void
{
$this->databaseSearch = '';
}
/**
* Known database names from past snapshots for the selected server.
*
* @return array<int, string>
*/
#[Computed]
public function knownDatabases(): array
{
if ($this->selectedServerId === '') {
return [];
}
return Snapshot::where('database_server_id', $this->selectedServerId)
->whereNotNull('database_name')
->when($this->databaseSearch !== '', function ($q) {
$q->where('database_name', 'like', '%'.$this->databaseSearch.'%');
})
->distinct()
->orderBy('database_name')
->pluck('database_name')
->filter(fn ($db) => ! in_array($db, $this->allowedDatabases, strict: true))
->values()
->all();
}
public function addDatabase(string $database): void
{
$db = trim($database);
if ($db !== '' && ! in_array($db, $this->allowedDatabases, strict: true)) {
$this->allowedDatabases[] = $db;
}
$this->databaseSearch = '';
}
public function addSearchedDatabase(): void
{
$this->addDatabase($this->databaseSearch);
}
public function removeDatabase(string $database): void
{
$this->allowedDatabases = array_values(
array_filter($this->allowedDatabases, fn ($d) => $d !== $database)
);
}
public function grantAccess(): void
{
$this->authorize('update', $this->user);
$this->validate([
'selectedServerId' => 'required|exists:database_servers,id',
'canDownload' => 'boolean',
'canBackup' => 'boolean',
'canRestore' => 'boolean',
]);
UserServerAccess::updateOrCreate(
[
'user_id' => $this->user->id,
'database_server_id' => $this->selectedServerId,
],
[
'allowed_databases' => ! empty($this->allowedDatabases) ? array_values($this->allowedDatabases) : null,
'can_download' => $this->canDownload,
'can_backup' => $this->canBackup,
'can_restore' => $this->canRestore,
]
);
$this->showGrantModal = false;
$this->success(__('Access granted successfully.'));
}
public function revokeAccess(int $accessId): void
{
$this->authorize('update', $this->user);
UserServerAccess::where('id', $accessId)
->where('user_id', $this->user->id)
->delete();
$this->success(__('Access revoked successfully.'));
}
/**
* Servers available for selection: excludes already-granted servers
* except the one currently selected (so editing a grant keeps it visible).
*
* @return array<int, array<string, string>>
*/
public function availableServerOptions(): array
{
$grantedIds = $this->user->serverAccesses()->pluck('database_server_id')->all();
// When editing an existing grant, keep its server selectable
if ($this->selectedServerId !== '') {
$grantedIds = array_filter($grantedIds, fn ($id) => $id !== $this->selectedServerId);
}
return DatabaseServer::query()
->whereNotIn('id', array_values($grantedIds))
->orderBy('name')
->get()
->map(fn (DatabaseServer $server) => [
'id' => $server->id,
'name' => $server->name.' ('.$server->database_type->value.')',
])
->toArray();
}
public function render(): View
{
$accesses = $this->user->serverAccesses()
->with('databaseServer')
->get();
return view('livewire.user.server-access', [
'accesses' => $accesses,
'availableServers' => $this->availableServerOptions(),
]);
}
}