-
Notifications
You must be signed in to change notification settings - Fork 63
feat: resource-scoped server access permissions for Viewer/Demo users #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kayvanaarssen
wants to merge
5
commits into
David-Crty:main
Choose a base branch
from
kayvanaarssen:feature/resource-scoped-permissions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6f5560
feat: resource-scoped server access permissions for Viewer users
e03f573
fix: address review findings on resource-scoped permissions
2fdbf85
fix: address remaining review findings
ed1f060
fix: centralise restore eligibility in policy
8876b9b
fix: authorize view and set restoreId before Redis branch in confirmR…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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(), | ||
| ]); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.