Skip to content

Add network write-permission and resource-to-network lookups - #318

Merged
knoxsp merged 1 commit into
masterfrom
feature/network-write-permission-check
Oct 22, 2026
Merged

knoxsp merged 1 commit into
masterfrom
feature/network-write-permission-check

Conversation

@knoxsp

@knoxsp knoxsp commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Why

HWI's read-only access enforcement (hwi MR !69) needs to ask whether a user can edit a network, and to find the network a node/link/group/scenario/resource attribute belongs to. It was doing both by querying hydra-base's tables directly from HWI. That belongs here, behind the lib API (as for projects: check_project_write_permission).

Changes

  • lib/network.check_network_write_permission(network_id, is_admin=None, user_id=...): True if the user can edit the network (creator, admin, owner with edit, or write access on the parent project); False for a view-only user or an unknown network. Never raises for lack of permission.
  • lib/network.get_resource_network_id(resource_type, resource_id, user_id=...): network ID for NETWORK, NODE, LINK, GROUP, SCENARIO or RESOURCEATTR; None if it doesn't exist, the input is bad, or the user cannot read that network (so it does not reveal which networks exist).
  • Bug fix in Network.check_write_permission: the project fallback called project.check_write_permission(user_id) with the default do_raise=True, so a view-only user got a PermissionError (naming the project) even when called with do_raise=False. It now passes do_raise=False and is_admin through; the network-level error is raised only when do_raise is True, as intended. The message for a refused user changes from the project's to the network's.

Testing

12 new tests in tests/test_network_permission_checks.py (creator, write share, view-only, no access, unknown network, is_admin, each resource type, bad input, unreadable network not revealed). The two view-only/no-access tests fail without the do_raise fix. Ran with the default sqlite backend only, together with test_groups, test_scenario, test_network, tests/project and test_users: 150 passed.

Independent of #317 (write checks); pairs with hwi MR !69 (hydra/hwi on gitlab.waterstrategy.org).

check_network_write_permission answers whether a user can edit a network
(False for view-only users or an unknown network) and get_resource_network_id
finds the network a node, link, group, scenario or resource attribute belongs
to (None if missing or unreadable), so callers such as HWI can enforce
read-only access through hydra-base instead of querying its tables.

Also pass do_raise=False and is_admin through to the project fallback in
Network.check_write_permission, which otherwise raised for a view-only user
even when asked not to.
@knoxsp
knoxsp merged commit 5356a1b into master Oct 22, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants