From fea21cd337c536fb8467ccbe8846a1b88c472600 Mon Sep 17 00:00:00 2001 From: Saman Rassam Date: Fri, 27 Nov 2026 15:25:23 +0100 Subject: [PATCH] Implemented fix detected in HMS-2712 in utils.py (verifying access by ID check once, instead of iterating through all projects). Removed extranous check in project_schemas.py and added one test. --- dds_web/api/schemas/project_schemas.py | 15 +-------------- dds_web/utils.py | 18 ++++++++++++++++-- tests/test_utils.py | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/dds_web/api/schemas/project_schemas.py b/dds_web/api/schemas/project_schemas.py index 8c087c078..febb73db6 100644 --- a/dds_web/api/schemas/project_schemas.py +++ b/dds_web/api/schemas/project_schemas.py @@ -43,19 +43,6 @@ def verify_project_exists(spec_proj): return project -def verify_project_access(project): - """Check users access to project.""" - - if project not in auth.current_user().projects: - raise ddserr.AccessDeniedError( - message="Project access denied.", - username=auth.current_user().username, - project=project.public_id, - ) - - return project - - #################################################################################################### # SCHEMAS ################################################################################ SCHEMAS # #################################################################################################### @@ -210,7 +197,7 @@ def validate_project(self, value, **kwargs): """Validate existing project and user access to it.""" project = verify_project_exists(spec_proj=value) - verify_project_access(project=project) + dds_web.utils.verify_project_access(project=project) @marshmallow.validates_schema(skip_on_field_errors=True) def get_project_object(self, data, **kwargs): diff --git a/dds_web/utils.py b/dds_web/utils.py index 130b26ee3..ead5978b6 100644 --- a/dds_web/utils.py +++ b/dds_web/utils.py @@ -78,10 +78,24 @@ def get_required_item(req: str, obj: werkzeug.datastructures.ImmutableMultiDict # Cannot have type hint for return due to models.Project giving circular import def verify_project_access(project) -> None: """Verify that current authenticated user has access to project.""" - if project not in auth.current_user().projects: + current_user = auth.current_user() + if current_user.role in ["Unit Admin", "Unit Personnel"]: + # Unit users have access to all projects in their unit + has_access = project.unit_id == current_user.unit_id + elif current_user.role == "Researcher": + # Researchers have access to the projects they have been added to + project_user = models.ProjectUsers.query.filter_by( + project_id=project.id, user_id=current_user.username + ).one_or_none() + has_access = project_user is not None + else: + # Super Admins have access to all projects + has_access = current_user.role == "Super Admin" + + if not has_access: raise AccessDeniedError( message="Project access denied.", - username=auth.current_user().username, + username=current_user.username, project=project.public_id, ) diff --git a/tests/test_utils.py b/tests/test_utils.py index 83b5dbe5e..270ca9255 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -137,6 +137,27 @@ def test_verify_project_access_ok(client: flask.testing.FlaskClient) -> None: utils.verify_project_access(project=project) +def test_verify_project_access_no_unit(client: flask.testing.FlaskClient) -> None: + """A project without a unit is not accessible to unit users.""" + # Unit user + user = models.UnitUser.query.filter_by(unit_id=1).first() + assert user + + # Get project and remove its unit + project = models.Project.query.filter_by(unit_id=1).first() + assert project + project.unit_id = None + db.session.commit() + + # Set auth.current_user + flask.g.flask_httpauth_user = user + + # Verify project access -- not ok + with pytest.raises(AccessDeniedError) as err: + utils.verify_project_access(project=project) + assert "Project access denied" in str(err.value) + + def test_verify_project_user_key_denied(client: flask.testing.FlaskClient) -> None: """A user must have an entry in projectUserKeys to access a project.""" # User