Skip to content
Snippets Groups Projects
Verified Commit 694c04ca authored by Erwan Rouchet's avatar Erwan Rouchet
Browse files

Only give guest access to public objects without authentication

parent 287eed8f
No related branches found
No related tags found
1 merge request!2297Only give guest access to public objects without authentication
...@@ -74,7 +74,6 @@ class TestCorpus(FixtureAPITestCase): ...@@ -74,7 +74,6 @@ class TestCorpus(FixtureAPITestCase):
mock_now.return_value = FAKE_NOW mock_now.return_value = FAKE_NOW
cls.corpus_hidden = Corpus.objects.create(name="C Hidden") cls.corpus_hidden = Corpus.objects.create(name="C Hidden")
@expectedFailure
def test_anon(self): def test_anon(self):
# An anonymous user has only access to public # An anonymous user has only access to public
with self.assertNumQueries(4): with self.assertNumQueries(4):
...@@ -225,7 +224,6 @@ class TestCorpus(FixtureAPITestCase): ...@@ -225,7 +224,6 @@ class TestCorpus(FixtureAPITestCase):
self.assertEqual(len(data), 13) self.assertEqual(len(data), 13)
self.assertSetEqual({corpus["top_level_type"] for corpus in data}, {None, "top_level"}) self.assertSetEqual({corpus["top_level_type"] for corpus in data}, {None, "top_level"})
@expectedFailure
def test_mixin(self): def test_mixin(self):
vol1 = Element.objects.get(name="Volume 1") vol1 = Element.objects.get(name="Volume 1")
vol2 = Element.objects.get(name="Volume 2") vol2 = Element.objects.get(name="Volume 2")
...@@ -345,7 +343,7 @@ class TestCorpus(FixtureAPITestCase): ...@@ -345,7 +343,7 @@ class TestCorpus(FixtureAPITestCase):
"description": self.corpus_public.description, "description": self.corpus_public.description,
"public": True, "public": True,
"indexable": False, "indexable": False,
"rights": ["read", "write", "admin"], "rights": ["read"],
"created": DB_CREATED, "created": DB_CREATED,
"authorized_users": 1, "authorized_users": 1,
"top_level_type": None, "top_level_type": None,
......
...@@ -43,7 +43,7 @@ class TestRetrieveElements(FixtureAPITestCase): ...@@ -43,7 +43,7 @@ class TestRetrieveElements(FixtureAPITestCase):
"public": True, "public": True,
}, },
"thumbnail_url": self.vol.thumbnail.s3_url, "thumbnail_url": self.vol.thumbnail.s3_url,
"thumbnail_put_url": self.vol.thumbnail.s3_put_url, "thumbnail_put_url": None,
"worker_version": None, "worker_version": None,
"confidence": None, "confidence": None,
"zone": None, "zone": None,
...@@ -51,7 +51,7 @@ class TestRetrieveElements(FixtureAPITestCase): ...@@ -51,7 +51,7 @@ class TestRetrieveElements(FixtureAPITestCase):
"mirrored": False, "mirrored": False,
"created": "2020-02-02T01:23:45.678000Z", "created": "2020-02-02T01:23:45.678000Z",
"creator": None, "creator": None,
"rights": ["read", "write", "admin"], "rights": ["read"],
"metadata_count": 0, "metadata_count": 0,
"classifications": [ "classifications": [
{ {
...@@ -102,6 +102,8 @@ class TestRetrieveElements(FixtureAPITestCase): ...@@ -102,6 +102,8 @@ class TestRetrieveElements(FixtureAPITestCase):
""" """
Check getting an element only gives a thumbnail URL with folders Check getting an element only gives a thumbnail URL with folders
""" """
self.client.force_login(self.user)
self.assertTrue(self.vol.type.folder) self.assertTrue(self.vol.type.folder)
response = self.client.get(reverse("api:element-retrieve", kwargs={"pk": str(self.vol.id)})) response = self.client.get(reverse("api:element-retrieve", kwargs={"pk": str(self.vol.id)}))
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
...@@ -230,7 +232,7 @@ class TestRetrieveElements(FixtureAPITestCase): ...@@ -230,7 +232,7 @@ class TestRetrieveElements(FixtureAPITestCase):
"public": True, "public": True,
}, },
"thumbnail_url": self.vol.thumbnail.s3_url, "thumbnail_url": self.vol.thumbnail.s3_url,
"thumbnail_put_url": self.vol.thumbnail.s3_put_url, "thumbnail_put_url": None,
"worker_version": str(self.worker_version.id), "worker_version": str(self.worker_version.id),
"confidence": None, "confidence": None,
"zone": None, "zone": None,
...@@ -238,7 +240,7 @@ class TestRetrieveElements(FixtureAPITestCase): ...@@ -238,7 +240,7 @@ class TestRetrieveElements(FixtureAPITestCase):
"mirrored": False, "mirrored": False,
"created": "2020-02-02T01:23:45.678000Z", "created": "2020-02-02T01:23:45.678000Z",
"creator": None, "creator": None,
"rights": ["read", "write", "admin"], "rights": ["read"],
"metadata_count": 0, "metadata_count": 0,
"classifications": [], "classifications": [],
"worker_run": { "worker_run": {
...@@ -265,7 +267,7 @@ class TestRetrieveElements(FixtureAPITestCase): ...@@ -265,7 +267,7 @@ class TestRetrieveElements(FixtureAPITestCase):
"public": True, "public": True,
}, },
"thumbnail_url": self.vol.thumbnail.s3_url, "thumbnail_url": self.vol.thumbnail.s3_url,
"thumbnail_put_url": self.vol.thumbnail.s3_put_url, "thumbnail_put_url": None,
"worker_version": None, "worker_version": None,
"confidence": None, "confidence": None,
"zone": None, "zone": None,
...@@ -273,7 +275,7 @@ class TestRetrieveElements(FixtureAPITestCase): ...@@ -273,7 +275,7 @@ class TestRetrieveElements(FixtureAPITestCase):
"mirrored": False, "mirrored": False,
"created": "2020-02-02T01:23:45.678000Z", "created": "2020-02-02T01:23:45.678000Z",
"creator": None, "creator": None,
"rights": ["read", "write", "admin"], "rights": ["read"],
"metadata_count": 0, "metadata_count": 0,
"classifications": [ "classifications": [
{ {
......
...@@ -10,6 +10,8 @@ def has_access(user: User, instance, level: int, skip_public: bool = False) -> b ...@@ -10,6 +10,8 @@ def has_access(user: User, instance, level: int, skip_public: bool = False) -> b
Check if the user has access to a generic instance with a minimum level Check if the user has access to a generic instance with a minimum level
If skip_public parameter is set to true, exclude rights on public instances If skip_public parameter is set to true, exclude rights on public instances
""" """
if user.is_anonymous:
return level <= Role.Guest.value and not skip_public and getattr(instance, "public", False)
return True return True
...@@ -18,6 +20,11 @@ def filter_rights(user: User, model, level: int): ...@@ -18,6 +20,11 @@ def filter_rights(user: User, model, level: int):
Return a generic queryset of objects with access rights for this user. Return a generic queryset of objects with access rights for this user.
Level filtering parameter should be an integer between 1 and 100. Level filtering parameter should be an integer between 1 and 100.
""" """
if user.is_anonymous:
if hasattr(model, "public"):
return model.objects.filter(public=True).annotate(max_level=Value(Role.Guest.value, IntegerField()))
return model.objects.none()
return model.objects.annotate(max_level=Value(Role.Admin.value, IntegerField())) return model.objects.annotate(max_level=Value(Role.Admin.value, IntegerField()))
...@@ -25,4 +32,9 @@ def get_max_level(user: User, instance) -> Optional[int]: ...@@ -25,4 +32,9 @@ def get_max_level(user: User, instance) -> Optional[int]:
""" """
Returns the maximum access level on a given model instance Returns the maximum access level on a given model instance
""" """
if user.is_anonymous:
if getattr(instance, "public", False):
return Role.Guest.value
return None
return Role.Admin.value return Role.Admin.value
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment