Fetch a server by ID, verifying the current user has access. Args: sid: Server ID. only_owned: If True, only return servers owned by the current user. Use this for write operations (change_password, clear_saved_password, etc.) that must not mutate another
(sid, only_owned=False)
| 22 | |
| 23 | |
| 24 | def get_server(sid, only_owned=False): |
| 25 | """Fetch a server by ID, verifying the current user has access. |
| 26 | |
| 27 | Args: |
| 28 | sid: Server ID. |
| 29 | only_owned: If True, only return servers owned by the current |
| 30 | user. Use this for write operations (change_password, |
| 31 | clear_saved_password, etc.) that must not mutate another |
| 32 | user's server record via shared access. |
| 33 | |
| 34 | Returns the server if: |
| 35 | - Desktop mode (single user, no isolation needed), OR |
| 36 | - The user owns it, OR |
| 37 | - The server is shared AND only_owned is False. |
| 38 | |
| 39 | Returns None otherwise (caller should return HTTP 410 Gone, in |
| 40 | line with the rest of the server views — see e.g. |
| 41 | web/pgadmin/browser/server_groups/servers/__init__.py). |
| 42 | |
| 43 | The Administrator role does not grant access to other users' |
| 44 | private servers — admins are subject to the same data isolation |
| 45 | as regular users. To make a server admin-visible, share it |
| 46 | (Server.shared=True). |
| 47 | |
| 48 | Note: In pgAdmin, Server.shared=True means the server is visible |
| 49 | to all authenticated users. SharedServer records are created |
| 50 | lazily for per-user customization, not for access control. |
| 51 | """ |
| 52 | if not config.SERVER_MODE: |
| 53 | return Server.query.filter_by(id=sid).first() |
| 54 | |
| 55 | if only_owned: |
| 56 | return Server.query.filter_by( |
| 57 | id=sid, user_id=current_user.id).first() |
| 58 | |
| 59 | return Server.query.filter( |
| 60 | Server.id == sid, |
| 61 | or_( |
| 62 | Server.user_id == current_user.id, |
| 63 | Server.shared |
| 64 | ) |
| 65 | ).first() |
| 66 | |
| 67 | |
| 68 | def get_server_group(gid): |
no test coverage detected