Skip to content

Commit

Permalink
fix: creator only allowed to interact with owned
Browse files Browse the repository at this point in the history
* not see restricted files

* not see records not owned by them
  • Loading branch information
utnapischtim committed Oct 26, 2024
1 parent 0583461 commit 0f45117
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
42 changes: 39 additions & 3 deletions invenio_records_marc21/services/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,53 @@

"""Permissions generators for Invenio Marc21 Records."""

from flask import current_app
from flask import current_app, g
from invenio_records_permissions.generators import Generator
from invenio_search.engine import dsl


class Marc21RecordCreators(Generator):
"""Allows record owners."""

def needs(self, **kwargs):
"""Enabling Needs."""
def needs(self, identity=None, record=None, **kwargs):
"""Enabling Needs.
The creator is only allowed to interact with the record which is created
by the creator.
"""
if record is None or identity is None:
return current_app.config.get("MARC21_RECORD_CREATOR_NEEDS", [])

if identity.id == record.parent.access.owner.owner_id:
return current_app.config.get("MARC21_RECORD_CREATOR_NEEDS", [])

return []

def excludes(self, identity=None, record=None, **kwargs):
"""Preventing Needs.
The creator is only allowed to interact with the record created by the
creator. By returning the role if the record is not created by the
creator is prevents the user of interacting with the record.
"""
if record is None:
return []

# TODO: because of strange tests behavior
if "identity" not in g:
return []

if g.identity.id == record.parent.access.owner.owner_id:
return []

return current_app.config.get("MARC21_RECORD_CREATOR_NEEDS", [])

def query_filter(self, identity=None, **kwargs):
"""Allow only to see records which the creator has created."""
users = [n.value for n in identity.provides if n.method == "id"]
if users:
return dsl.Q("terms", **{"parent.access.owned_by.user": users})


class Marc21RecordManagers(Generator):
"""Allows record owners."""
Expand Down
4 changes: 2 additions & 2 deletions invenio_records_marc21/services/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Marc21RecordPermissionPolicy(RecordPermissionPolicy):

# Allow reading metadata of a record
can_read = [
IfRestricted("record", then_=can_view, else_=can_all),
IfRestricted("record", then_=can_curate, else_=can_all),
]
# Used for search filtering of deleted records
# cannot be implemented inside can_read - otherwise permission will
Expand All @@ -77,7 +77,7 @@ class Marc21RecordPermissionPolicy(RecordPermissionPolicy):
can_manage_files = can_curate

can_read_files = [
IfRestricted("files", then_=can_view, else_=can_all),
IfRestricted("files", then_=can_curate, else_=can_all),
]
can_get_content_files = [
IfFileIsLocal(then_=can_read_files, else_=[SystemProcess()])
Expand Down

0 comments on commit 0f45117

Please sign in to comment.