refactor(Servergroup): Refactor server group access and shared-group handling#10077
refactor(Servergroup): Refactor server group access and shared-group handling#10077lkmatsumura wants to merge 3 commits into
Conversation
…s a shared group. - Added is_shared_group property to the result to simplify the identificantion of shared group- Added is_shared_group property to the result to simplify the identification of shared group - rules centralized in get_server_groups_for_user_query() function
…and shared group logic - change to call get_server_groups_for_user() to retrive the servergroups when possible - same for get_server_group() - Simplifying the method of detecting whether the group is shared or not using the information now returned by get_server_groups_for_user() and get_server_group().
- A shared group is not allowed to be deleted. - Only the owner can do it.
WalkthroughRefactored server-group access and icon selection: introduced new query helpers to compute ChangesServer Group Access and Icon Refactoring
Sequence DiagramsequenceDiagram
participant ServerGroupView
participant get_server_group
participant get_server_groups_for_user
participant get_server_groups_for_user_query
participant Database
ServerGroupView->>get_server_group: get_server_group(gid)
get_server_group->>get_server_groups_for_user: servergroup_id=gid
get_server_groups_for_user->>get_server_groups_for_user_query: build visibility query
get_server_groups_for_user_query->>Database: SELECT with is_shared_group logic
Database-->>get_server_groups_for_user_query: groups with is_shared_group
get_server_groups_for_user-->>get_server_group: annotated results
get_server_group-->>ServerGroupView: single group or None
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
web/pgadmin/utils/server_access.py (1)
133-133: ⚡ Quick winUse
Server.sharedinstead ofServer.shared == Truein SQLAlchemy filter.In SQLAlchemy, boolean columns can be used directly in filter expressions. Using
== Trueis redundant and flagged by linters.♻️ Proposed fix
.filter( Server.servergroup_id == ServerGroup.id, - Server.shared == True + Server.shared )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/pgadmin/utils/server_access.py` at line 133, The SQLAlchemy filter currently uses the redundant comparison Server.shared == True; replace that expression with the boolean column itself (Server.shared) wherever used in the query/filter (e.g., in the function or method that constructs the query referencing Server.shared) so the filter reads simply Server.shared to satisfy linters and follow SQLAlchemy conventions.Source: Linters/SAST tools
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@web/pgadmin/utils/server_access.py`:
- Line 133: The SQLAlchemy filter currently uses the redundant comparison
Server.shared == True; replace that expression with the boolean column itself
(Server.shared) wherever used in the query/filter (e.g., in the function or
method that constructs the query referencing Server.shared) so the filter reads
simply Server.shared to satisfy linters and follow SQLAlchemy conventions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 66b9468b-6998-4f8c-b411-8278e08db886
📒 Files selected for processing (2)
web/pgadmin/browser/server_groups/__init__.pyweb/pgadmin/utils/server_access.py
dpage
left a comment
There was a problem hiding this comment.
Thanks for taking this on — centralizing the visibility logic in server_access.py and replacing the per-server has_shared_server() loop with a single SQL EXISTS is a nice direction. A few things need addressing before this is mergeable:
1. Desktop mode is broken — (0).label('is_shared_group') raises AttributeError.
In get_server_groups_for_user_query(), the non-SERVER_MODE branch does:
ServerGroup.query.add_columns((0).label('is_shared_group'))int has no .label() method, so this throws at query-build time (verified locally). Since get_nodes() was also changed to always route through get_all_server_groups(), the server-group tree fails to render in desktop mode — the default deployment. Use from sqlalchemy import literal and literal(0).label('is_shared_group').
2. Access regression in update().
update() previously fetched ServerGroup.query.filter_by(user_id=current_user.id, id=gid). It now uses get_server_group(gid), which in SERVER_MODE returns groups the user does not own as long as they contain a shared server. The method then does servergroup.name = data['name']; db.session.commit() with no ownership guard, so a user can rename another user's group. Deletion is correctly gated against shared groups (can_delete), but rename needs the same owner check.
3. Style checks will fail CI.
Server.shared == True→ E712 (useServer.shared.is_(True)or justServer.shared).- Only one blank line between the two new top-level functions → E302.
- A couple of lines exceed 79 chars (the
.all()call and a docstring line) → E501. for group, is_shared in sg:has a double space.
4. No tests, no changelog entry. A refactor touching access control should have a test for the owned / shared / desktop paths, and docs/en_US/release_notes_*.rst needs a line.
Since this isn't tied to a reported issue, it'd also help to note what user-facing problem it's solving — the diff reads as pure cleanup, so the bar is "demonstrably no behaviour change," and right now #1/#2 are behaviour changes.
Summary
Refactor server group access and shared-group handling in pgAdmin4 to centralize visibility logic and simplify browser rendering.
What changed
Updated
web/pgadmin/utils/server_access.pyget_server_groups_for_user()andget_server_groups_for_user_query()get_server_group()to reuse shared access logicis_shared_groupmetadata on returned groupsUpdated
web/pgadmin/browser/server_groups/__init__.pyServerGroupModule.has_shared_server()logicNotes
Summary by CodeRabbit
Bug Fixes
Improvements