Skip to content
Merged
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
48 changes: 11 additions & 37 deletions isic/ingest/models/lesion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@

from django.conf import settings
from django.contrib.auth.models import AnonymousUser, User
from django.contrib.postgres.aggregates import BoolAnd
from django.core.validators import RegexValidator
from django.db import models
from django.db.models import Case, OuterRef, Value, When
from django.db.models.aggregates import Count
from django.db.models.constraints import CheckConstraint, UniqueConstraint
from django.db.models.expressions import Exists, F, Window
from django.db.models.functions import Concat
from django.db.models.functions.comparison import Coalesce
from django.db.models.query import QuerySet
from django.db.models.query_utils import Q
from django.urls import reverse
Expand Down Expand Up @@ -271,42 +269,18 @@ def view_lesion_list(user_obj: User, qs: QuerySet[Lesion] | None = None) -> Quer

if user_obj.is_staff:
return qs
elif user_obj.is_authenticated:
lesion_visibility_requirements = Q(accessions__image__public=True) | Q(
# this needs list coercion because otherwise it will be a subquery that contains
# the user_id, which doesn't allow users with identical privileges to share
# the query cache.
cohort__contributor_id__in=list(
user_obj.owned_contributors.order_by().values_list("id", flat=True)
)
)

# only add the user share requirement if the user has shares, since it will put
# the user_id into the query (making query caching less effective).
if user_obj.image_shares_received.exists():
lesion_visibility_requirements |= Q(user_share_id=user_obj.id)

return qs.filter(
id__in=Lesion.objects.values("id")
# if an image doesn't have shares it will return null which is skipped by BoolAnd.
# this can lead to a scenario where a lesion has a private image without shares but
# is still visible because bool_and(null, true) = true.
# coalesce it so that bool_and always has non-null values to work with.
.alias(user_share_id=Coalesce(F("accessions__image__shares"), -1))
.annotate(
# note that these requirements are copied from ImagePermissions.view_image_list
visible=BoolAnd(lesion_visibility_requirements)
)
.filter(visible=True)
.values("id")
)
else:
return qs.filter(
id__in=Lesion.objects.values("id")
.annotate(visible=BoolAnd(Q(accessions__image__public=True)))
.filter(visible=True)
.values("id")
)
from isic.core.models.image import Image, ImagePermissions

lesion_images = Image.objects.filter(accession__lesion_id=OuterRef("pk"))
visible = ImagePermissions.visible_image_q(user_obj)
has_visible_image = Exists(lesion_images.filter(visible))
has_invisible_image = Exists(lesion_images.exclude(visible))

# a lesion is visible when it has published images and the user can see all of them.
# the first subquery could be a bare "has any image", but carrying the predicate in both
# lets postgres filter each side before joining, which is 3x faster on the first page.
return qs.filter(has_visible_image, ~has_invisible_image)

@staticmethod
def view_lesion(user_obj: User, obj: Lesion) -> bool:
Expand Down
35 changes: 35 additions & 0 deletions isic/ingest/tests/test_api_lesion.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,41 @@ def test_api_lesion_permissions_contributor(
assert resp.status_code == 200, resp.json()


@pytest.mark.django_db
@pytest.mark.usefixtures("_search_index")
def test_api_lesion_permissions_shares(
client,
user,
user_factory,
lesion_factory,
image_factory,
image_share_factory,
accession_factory,
):
lesion = lesion_factory()
image_factory(accession__lesion=lesion, public=True)
private_image = image_factory(accession__lesion=lesion, public=False)
# shared with the user and with someone else
image_share_factory(image=private_image, grantee=user)
image_share_factory(image=private_image)

def visible_lesion_ids() -> list[str]:
resp = client.get(reverse("api:lesion_list"))
assert resp.status_code == 200, resp.json()
return [result["id"] for result in resp.json()["results"]]

assert visible_lesion_ids() == []
client.force_login(user_factory())
assert visible_lesion_ids() == []
client.force_login(user)
assert visible_lesion_ids() == [lesion.id]

# an unpublished accession doesn't change who can see the lesion
accession_factory(lesion=lesion)
assert user.has_perm("ingest.view_lesion", lesion)
assert not user_factory().has_perm("ingest.view_lesion", lesion)


@pytest.mark.django_db
@pytest.mark.usefixtures("_search_index")
def test_api_lesion_completeness(client, lesion_factory, image_factory):
Expand Down