Repository navigation
Add network write-permission and resource-to-network lookups - #318
Merged
Merged
Conversation
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.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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).Network.check_write_permission: the project fallback calledproject.check_write_permission(user_id)with the defaultdo_raise=True, so a view-only user got a PermissionError (naming the project) even when called withdo_raise=False. It now passesdo_raise=Falseandis_adminthrough; the network-level error is raised only whendo_raiseis 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 thedo_raisefix. 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).