Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions dds_web/api/schemas/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
####################################################################################################
Expand Down Expand Up @@ -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):
Expand Down
18 changes: 16 additions & 2 deletions dds_web/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
21 changes: 21 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading