Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Oct 11, 2024
1 parent ff35f8d commit 90d019c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 32 deletions.
11 changes: 7 additions & 4 deletions invenio_vocabularies/contrib/names/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@

from invenio_records_permissions.generators import AuthenticatedUser, SystemProcess

from invenio_vocabularies.services.generators import Tags
from invenio_vocabularies.services.generators import IfTags
from invenio_vocabularies.services.permissions import PermissionPolicy


class NamesPermissionPolicy(PermissionPolicy):
"""Names permission policy."""
"""Names permission policy.
Names endpoints are protected, only authenticated users can access them.
"""

can_search = [
SystemProcess(),
Tags(exclude=["non-searchable"], only_authenticated=True),
IfTags(exclude=["unlisted"], only_authenticated=True),
]
can_read = [SystemProcess(), AuthenticatedUser()]
# this permission is needed for the /api/vocabularies/ endpoint
can_list_vocabularies = [
SystemProcess(),
Tags(exclude=["non-searchable"], only_authenticated=True),
IfTags(exclude=["unlisted"], only_authenticated=True),
]
23 changes: 6 additions & 17 deletions invenio_vocabularies/services/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,12 @@
from invenio_search.engine import dsl


class AnyUser(Generator):
"""Allows any user."""

def needs(self, **kwargs):
"""Enabling Needs."""
return [any_user]

def query_filter(self, **kwargs):
"""Match only searchable values in search."""
return dsl.Q(
"bool",
must_not=[dsl.Q("term", tags="non-searchable")],
)


class Tags(Generator):
"""Allows any user."""
class IfTags(Generator):
"""Generator to filter based on tags.
This generator will filter records based on the tags field.
Optionally, it can be configured to only allow authenticated users.
"""

def __init__(self, include=None, exclude=None, only_authenticated=False):
"""Constructor."""
Expand Down
8 changes: 4 additions & 4 deletions invenio_vocabularies/services/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
from invenio_records_permissions import RecordPermissionPolicy
from invenio_records_permissions.generators import SystemProcess

from invenio_vocabularies.services.generators import Tags
from invenio_vocabularies.services.generators import IfTags


class PermissionPolicy(RecordPermissionPolicy):
"""Permission policy."""

can_search = [SystemProcess(), Tags(exclude=["non-searchable"])]
can_read = [SystemProcess(), Tags(exclude=["non-searchable"])]
can_search = [SystemProcess(), IfTags(exclude=["unlisted"])]
can_read = [SystemProcess(), IfTags(exclude=["unlisted"])]
can_create = [SystemProcess()]
can_update = [SystemProcess()]
can_delete = [SystemProcess()]
can_manage = [SystemProcess()]
# this permission is needed for the /api/vocabularies/ endpoint
can_list_vocabularies = [SystemProcess(), Tags(exclude=["non-searchable"])]
can_list_vocabularies = [SystemProcess(), IfTags(exclude=["unlisted"])]
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ def lang_data2(lang_data):

@pytest.fixture()
def non_searchable_lang_data(lang_data):
"""Example data for testing non-searchable cases."""
"""Example data for testing unlisted cases."""
data = dict(lang_data)
data["tags"] = ["non-searchable", "recommended"]
data["tags"] = ["unlisted", "recommended"]
return data


Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/names/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def non_searchable_name_data():
{"identifier": "gnd:4079154-3", "scheme": "gnd"},
],
"affiliations": [{"id": "cern"}, {"name": "CustomORG"}],
"tags": ["non-searchable"],
"tags": ["unlisted"],
}


Expand Down
4 changes: 2 additions & 2 deletions tests/contrib/names/test_name_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_non_searchable_tag(
superuser_identity,
indexer,
):
"""Test that non-searchable tags are not returned in search results."""
"""Test that unlisted tags are not returned in search results."""
# Service
assert service.id == "names"
assert service.config.indexer_queue_name == "names"
Expand All @@ -50,7 +50,7 @@ def test_non_searchable_tag(
res = service.search(anyuser_idty, type="names", q=f"id:{id_}", size=25, page=1)

Check failure on line 50 in tests/contrib/names/test_name_permissions.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.9, postgresql14, opensearch2)

test_non_searchable_tag invenio_records_resources.services.errors.PermissionDeniedError: search

Check failure on line 50 in tests/contrib/names/test_name_permissions.py

View workflow job for this annotation

GitHub Actions / Python / Tests (3.12, postgresql14, opensearch2)

test_non_searchable_tag invenio_records_resources.services.errors.PermissionDeniedError: search
assert res.total == 0

# Admins should be able to see the non-searchable tags
# Admins should be able to see the unlisted tags
res = service.search(
superuser_identity, type="names", q=f"id:{id_}", size=25, page=1
)
Expand Down
4 changes: 2 additions & 2 deletions tests/services/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_permissions_readonly(anyuser_idty, lang_type, lang_data, service):
def test_non_searchable_tag(
anyuser_idty, lang_type, non_searchable_lang_data, service, superuser_identity
):
"""Test that non-searchable tags are not returned in search results."""
"""Test that unlisted tags are not returned in search results."""
item = service.create(system_identity, non_searchable_lang_data)
id_ = item.id
# Refresh index to make changes live.
Expand All @@ -83,7 +83,7 @@ def test_non_searchable_tag(
res = service.search(anyuser_idty, type="languages", q=f"id:{id_}", size=25, page=1)
assert res.total == 0

# Admins should be able to see the non-searchable tags
# Admins should be able to see the unlisted tags
res = service.search(
superuser_identity, type="languages", q=f"id:{id_}", size=25, page=1
)
Expand Down

0 comments on commit 90d019c

Please sign in to comment.