Skip to content

Commit

Permalink
permissions: Add rdm curation permission policy example
Browse files Browse the repository at this point in the history
  • Loading branch information
rekt-hard committed Oct 1, 2024
1 parent 595f733 commit ef01256
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
4 changes: 2 additions & 2 deletions invenio_curations/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from invenio_curations.services import facets

from .permissions import CurationPermissionPolicy
from .permissions import CurationRDMRequestPermissionPolicy


class CurationsSearchOptions(RequestSearchOptions):
Expand Down Expand Up @@ -43,7 +43,7 @@ class CurationsServiceConfig(RecordServiceConfig, ConfiguratorMixin):

# common configuration
permission_policy_cls = FromConfig(
"REQUESTS_PERMISSION_POLICY", default=CurationPermissionPolicy
"REQUESTS_PERMISSION_POLICY", default=CurationRDMRequestPermissionPolicy
)
# TODO: update search options?
search = CurationsSearchOptions
61 changes: 59 additions & 2 deletions invenio_curations/services/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,71 @@

"""Curations permissions."""

from invenio_rdm_records.services.generators import IfFileIsLocal
from invenio_rdm_records.services.permissions import RDMRecordPermissionPolicy
from invenio_records_permissions.generators import SystemProcess
from invenio_requests.services.generators import Creator, Receiver, Status
from invenio_requests.services.permissions import (
PermissionPolicy as RequestPermissionPolicy,
)

from invenio_curations.services.generators import (
CurationModerators,
IfCurationRequestExists,
)


class CurationRDMRecordPermissionPolicy(RDMRecordPermissionPolicy):
"""RDM record policy for curations."""

can_preview = RDMRecordPermissionPolicy.can_preview + [
IfCurationRequestExists(then_=[CurationModerators()], else_=[])
]
can_view = RDMRecordPermissionPolicy.can_view + [
IfCurationRequestExists(then_=[CurationModerators()], else_=[])
]
can_read = RDMRecordPermissionPolicy.can_read + [
IfCurationRequestExists(then_=[CurationModerators()], else_=[])
]
can_read_files = RDMRecordPermissionPolicy.can_read_files + [
IfCurationRequestExists(then_=[CurationModerators()], else_=[])
]

# in order to get all base permissions in, we just add ours instead of adapting the then_ clause of the base permission
can_get_content_files = RDMRecordPermissionPolicy.can_get_content_files + [
IfFileIsLocal(then_=can_read_files, else_=[SystemProcess()])
]

can_read_draft = RDMRecordPermissionPolicy.can_read_draft + [
IfCurationRequestExists(then_=[CurationModerators()], else_=[])
]
can_draft_read_files = RDMRecordPermissionPolicy.can_draft_read_files + [
IfCurationRequestExists(then_=[CurationModerators()], else_=[])
]

# in order to get all base permissions in, we just add ours instead of adapting the then_ clause of the base permission
can_draft_get_content_files = (
RDMRecordPermissionPolicy.can_draft_get_content_files
+ [IfFileIsLocal(then_=can_draft_read_files, else_=[SystemProcess()])]
)

# in order to get all base permissions in, we just add ours instead of adapting the then_ clause of the base permission
can_draft_media_get_content_files = (
RDMRecordPermissionPolicy.can_draft_media_get_content_files
+ [IfFileIsLocal(then_=can_preview, else_=[SystemProcess()])]
)

can_media_read_files = RDMRecordPermissionPolicy.can_media_read_files + [
IfCurationRequestExists(then_=[CurationModerators()], else_=[])
]
can_media_get_content_files = (
RDMRecordPermissionPolicy.can_media_get_content_files
+ [IfFileIsLocal(then_=can_read, else_=[SystemProcess()])]
)


class CurationPermissionPolicy(RequestPermissionPolicy):
"""Permission policy for curations."""
class CurationRDMRequestPermissionPolicy(RequestPermissionPolicy):
"""Request permission policy for curations."""

can_read = RequestPermissionPolicy.can_read + [
Status(
Expand Down

0 comments on commit ef01256

Please sign in to comment.