-
-
Notifications
You must be signed in to change notification settings - Fork 254
Fix role-based server access for users with node-restricted permissions #2002
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -266,7 +266,7 @@ public function activity(): MorphToMany | |
| */ | ||
| public function accessibleServers(): Builder | ||
| { | ||
| if ($this->canned('viewAny', Server::class)) { | ||
| if ($this->canViewServers()) { | ||
| return Server::select('servers.*') | ||
| ->leftJoin('subusers', 'subusers.server_id', '=', 'servers.id') | ||
| ->where(function (Builder $builder) { | ||
|
|
@@ -278,6 +278,22 @@ public function accessibleServers(): Builder | |
| return $this->directAccessibleServers(); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the user has permission to view servers via role permissions. | ||
| */ | ||
| public function canViewServers(): bool | ||
| { | ||
| if ($this->isRootAdmin()) { | ||
| return true; | ||
| } | ||
|
|
||
| try { | ||
| return $this->hasPermissionTo('viewList server'); | ||
| } catch (\Spatie\Permission\Exceptions\PermissionDoesNotExist) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import please |
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns all the servers that a user can access "directly". | ||
| * This means either because they are the owner or a subuser of the server. | ||
|
|
@@ -438,13 +454,21 @@ public function getTenants(Panel $panel): array|Collection | |
| public function canAccessTenant(Model $tenant): bool | ||
| { | ||
| if ($tenant instanceof Server) { | ||
| if ($this->canned('view', $tenant) || $tenant->owner_id === $this->id) { | ||
| if ($tenant->owner_id === $this->id) { | ||
| return true; | ||
| } | ||
|
|
||
| $subuser = $tenant->subusers->where('user_id', $this->id)->first(); | ||
| if ($subuser !== null) { | ||
| return true; | ||
| } | ||
|
|
||
| return $subuser !== null; | ||
| // Check if user has role-based access to this server's node | ||
| if ($this->canViewServers() && $this->canTarget($tenant->node)) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return false; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,12 +22,12 @@ public function before(User $user, string $ability, string|Server $server): ?boo | |||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| if (Subuser::doesPermissionExist($ability)) { | ||||||
| // Owner has full server permissions | ||||||
| if ($server->owner_id === $user->id) { | ||||||
| return true; | ||||||
| } | ||||||
| // Owner has full server permissions | ||||||
| if ($server->owner_id === $user->id) { | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| if (Subuser::doesPermissionExist($ability)) { | ||||||
| $subuser = $server->subusers->where('user_id', $user->id)->first(); | ||||||
| // If the user is a subuser check their permissions | ||||||
| if ($subuser && in_array($ability, $subuser->permissions)) { | ||||||
|
|
@@ -40,6 +40,16 @@ public function before(User $user, string $ability, string|Server $server): ?boo | |||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| // Check if user has role-based permission for this specific ability | ||||||
| $permissionName = $ability . ' ' . $this->modelName; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Isn't there a better way to do this @Boy132 ? |
||||||
| try { | ||||||
| if ($user->hasPermissionTo($permissionName)) { | ||||||
| return true; | ||||||
| } | ||||||
| } catch (\Spatie\Permission\Exceptions\PermissionDoesNotExist) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import class please |
||||||
| // Permission doesn't exist, continue to default policies | ||||||
| } | ||||||
|
|
||||||
| // Return null to let default policies take over | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have enums?