Skip to content

oarepo_config.workflows.simplified.community

oarepo_config.workflows.simplified.community

Community workflow configuration for OARepo.

CommunityWorkflow dataclass

Bases: BaseWorkflowSettings

Workflow configuration for deposits inside communities.

Source code in oarepo_config/workflows/simplified/community.py
@dataclasses.dataclass
class CommunityWorkflow(BaseWorkflowSettings):
    """Workflow configuration for deposits inside communities."""

    draft_creation_community_roles: list[str] = dataclasses.field(
        default_factory=lambda: ["submitter", "curator", "owner"]
    )
    """Restrict draft creation to community members with at least one of these roles.

    If not specified (empty list), any member of the community can create a
    draft.  Used in addition to the site-wide
    `draft_creation_roles` /
    `draft_creation_needs` restrictions.
    """

    read_restricted_community_roles: list[str] = dataclasses.field(default_factory=lambda: ["curator", "owner"])
    """Community roles that may read restricted (published) records without requesting access.

    If empty, only the record owner can read the content after publishing. By default allowing curators
    and community owners to access restricted records.

    Note:
        Community roles are used instead of
        `RecordCommunitiesAction`
        so that each workflow can have independent read/draft/manage permissions.
    """

    read_draft_community_roles: list[str] = dataclasses.field(default_factory=list)
    """Community roles that may read draft records without requesting access.

    If empty, only the record owner can read the content of a draft.  Curators
    can always read draft records when the record is submitted for review.

    Note:
        Community roles are used instead of
        `RecordCommunitiesAction`
        so that each workflow can have independent read/draft/manage permissions.
    """

    record_manage_community_roles: list[str] = dataclasses.field(default_factory=lambda: ["curator", "owner"])
    """Community roles that may manage (edit, delete, publish) records.

    If empty, only the record owner can manage the record. By default allowing curators
    and community owners to manage all records.

    Note:
        Community roles are used instead of
        `RecordCommunitiesAction`
        so that each workflow can have independent read/draft/manage permissions.
    """

    community_curator_roles: list[str] = dataclasses.field(default_factory=lambda: ["curator"])
    """Community roles that allow users to review and curate records."""

    # --- permission policy ----------------------------------------------------

    def _build_permission_policy(self) -> type[BasePermissionPolicy]:
        class PermissionPolicy(BooleanPermissionPolicyMixin, self.base_permission_policy):  # type: ignore[name-defined]
            """A permission policy for the workflow."""

            can_create = self._build_record_create_generators()
            can_read = self.base_permission_policy.can_read + self._build_record_view_permissions()
            can_rdm_manage = self._build_record_rdm_manage_generators(self.base_permission_policy.can_rdm_manage)  # type: ignore[attr-defined]
            can_rdm_view = self._build_record_rdm_view_permissions(self.base_permission_policy.can_rdm_view)  # type: ignore[attr-defined]
            can_rdm_preview = self._build_record_rdm_preview_generators(self.base_permission_policy.can_rdm_preview)  # type: ignore[attr-defined]
            can_publish = (*self.base_permission_policy.can_publish, RequestActive())  # type: ignore[attr-defined]

        return PermissionPolicy

    def _build_record_create_generators(self) -> tuple[Generator, ...]:
        """Extend base create generators with community membership constraints.

        Calls `_build_record_create_generators` to
        resolve site-wide role- and need-based restrictions, then appends an
        `InAnyCommunity`
        generator for each role listed in `draft_creation_community_roles`.

        Returns:
            A tuple of permission generators for the `can_create` policy action.

        """
        create_generators = list(super()._build_record_create_generators())
        for role in self.draft_creation_community_roles:
            create_generators += [InAnyCommunity(PrimaryCommunityRole(role))]
        return tuple(create_generators)

    # --- RDM permission helpers -----------------------------------------------

    def _replace_communities_action_with_roles(
        self,
        original_permissions: list[Generator],
        community_roles: list[str],
    ) -> list[Generator]:
        """Replace `RecordCommunitiesAction` with explicit community-role generators.

        Filters *original_permissions* to remove any
        `RecordCommunitiesAction`
        instance (which grants access to *all* community members), then appends a
        `CommunityRole`
        generator for each role in *community_roles*.

        This lets each workflow define fine-grained, role-scoped access instead of
        the blanket community-wide access granted by
        `RecordCommunitiesAction`.

        Args:
            original_permissions: The existing generator list from the base
                permission policy (e.g. `can_rdm_view`).
            community_roles: Community role names to grant access to.

        Returns:
            An updated generator list.

        """
        result = [gen for gen in original_permissions if not isinstance(gen, RecordCommunitiesAction)]
        result.extend(CommunityRole(role) for role in community_roles)
        return result

    def _build_record_rdm_view_permissions(self, original_rdm_permissions: list[Generator]) -> list[Generator]:
        """Build `can_rdm_view` generators for this workflow.

        Replaces `RecordCommunitiesAction` with
        `read_restricted_community_roles`-scoped generators so that only
        the listed community roles (rather than all community members) can read
        restricted records.

        Args:
            original_rdm_permissions: The existing `can_rdm_view` generators
                from the base permission policy.

        Returns:
            Updated generator list.

        """
        return self._replace_communities_action_with_roles(
            original_rdm_permissions, self.read_restricted_community_roles
        )

    def _build_record_rdm_preview_generators(self, original_rdm_permissions: list[Generator]) -> list[Generator]:
        """Build `can_rdm_preview` generators for this workflow.

        Replaces `RecordCommunitiesAction` with
        `read_draft_community_roles`-scoped generators so that only the
        listed community roles (rather than all community members) can preview
        draft records.

        Args:
            original_rdm_permissions: The existing `can_rdm_preview` generators
                from the base permission policy.

        Returns:
            Updated generator list.

        """
        return self._replace_communities_action_with_roles(original_rdm_permissions, self.read_draft_community_roles)

    def _build_record_rdm_manage_generators(self, original_rdm_permissions: list[Generator]) -> list[Generator]:
        """Build `can_rdm_manage` generators for this workflow.

        Replaces `RecordCommunitiesAction` with
        `record_manage_community_roles`-scoped generators so that only
        the listed community roles (rather than all community members) can
        manage records.

        Args:
            original_rdm_permissions: The existing `can_rdm_manage` generators
                from the base permission policy.

        Returns:
            Updated generator list.

        """
        return self._replace_communities_action_with_roles(original_rdm_permissions, self.record_manage_community_roles)

    # --- request policy -------------------------------------------------------

    def _build_request_policy(self) -> type[WorkflowRequestPolicy]:
        curator_generators: list[PrimaryCommunityRole] = [
            PrimaryCommunityRole(role) for role in self.community_curator_roles
        ]

        requests = {
            # File modification requests let record owners unlock a published
            # record's files (grace-period feature). Created and accepted by
            # invenio's file_modification service (owner submits, system accepts),
            # so no reviewer and no record state transition.
            FileModification.type_id: WorkflowRequest(
                requesters=[RecordOwners()],
                recipients=[RecordOwnersForRecipients()],
                transitions=WorkflowTransitions(),
            ),
            # Quota increase requests let record owners raise a draft's storage
            # quota from their additional allowance. Created and accepted by
            # invenio's quota_increase service (owner submits, system accepts),
            # so no reviewer and no record state transition.
            QuotaIncrease.type_id: WorkflowRequest(
                requesters=[RecordOwners()],
                recipients=[RecordOwnersForRecipients()],
                transitions=WorkflowTransitions(),
            ),
            CommunitySubmission.type_id: WorkflowRequest(
                requesters=[
                    IfNewRecord(
                        then_=self._build_record_create_generators(),
                        else_=[
                            # Note: this permission check can be called from /review url, that is
                            # inside review service's create method. The problem is that review
                            # service does not pass the original record - it fills the "record"
                            # parameter with a community. That's why we can not use IfInState
                            # and similar permission generators that depend on the record.
                            *[
                                IfRDMRecordPassed(
                                    # the user has called the requests' create method directly
                                    then_=[RequireAll(RecordOwners(), PrimaryCommunityRole(role))],
                                    # the user went through the review flow which did not pass the record
                                    else_=[PrimaryCommunityRole(role)],
                                )
                                for role in self.draft_creation_community_roles
                            ],
                            *curator_generators,
                        ],
                    )
                ],
                recipients=curator_generators,
                transitions=WorkflowTransitions(
                    submitted="submitted",
                    accepted="published",
                    declined="draft",
                    cancelled="draft",
                ),
            ),
            PublishChangedMetadataRequestType.type_id: WorkflowRequest(
                requesters=[
                    IfInState(
                        ["draft", "revision_requested"],
                        [
                            RecordOwners(),
                            *curator_generators,
                        ],
                    )
                ],
                recipients=curator_generators,
                transitions=WorkflowTransitions(
                    submitted="submitted",
                    accepted="published",
                    declined="revision_requested",
                    cancelled="draft",
                ),
            ),
            PublishNewVersionRequestType.type_id: WorkflowRequest(
                requesters=[
                    IfInState(
                        ["draft", "revision_requested"],
                        [
                            RecordOwners(),
                            *curator_generators,
                        ],
                    )
                ],
                recipients=curator_generators,
                transitions=WorkflowTransitions(
                    submitted="submitted",
                    accepted="published",
                    declined="revision_requested",
                    cancelled="draft",
                ),
            ),
            # note: we need to patch https://github.com/inveniosoftware/invenio-rdm-records/blob/master/invenio_rdm_records/services/access/service.py#L784,
            # this will not work without the patch
            UserAccessRequest.type_id: WorkflowRequest(
                requesters=[AuthenticatedUser()],
                recipients=curator_generators,
            ),
        }

        return self._create_request_policy("CommunityReviewRequestPolicy", requests)

draft_creation_community_roles: list[str] = dataclasses.field(default_factory=lambda: ['submitter', 'curator', 'owner']) class-attribute instance-attribute

Restrict draft creation to community members with at least one of these roles.

If not specified (empty list), any member of the community can create a draft. Used in addition to the site-wide draft_creation_roles / draft_creation_needs restrictions.

read_restricted_community_roles: list[str] = dataclasses.field(default_factory=lambda: ['curator', 'owner']) class-attribute instance-attribute

Community roles that may read restricted (published) records without requesting access.

If empty, only the record owner can read the content after publishing. By default allowing curators and community owners to access restricted records.

Note

Community roles are used instead of RecordCommunitiesAction so that each workflow can have independent read/draft/manage permissions.

read_draft_community_roles: list[str] = dataclasses.field(default_factory=list) class-attribute instance-attribute

Community roles that may read draft records without requesting access.

If empty, only the record owner can read the content of a draft. Curators can always read draft records when the record is submitted for review.

Note

Community roles are used instead of RecordCommunitiesAction so that each workflow can have independent read/draft/manage permissions.

record_manage_community_roles: list[str] = dataclasses.field(default_factory=lambda: ['curator', 'owner']) class-attribute instance-attribute

Community roles that may manage (edit, delete, publish) records.

If empty, only the record owner can manage the record. By default allowing curators and community owners to manage all records.

Note

Community roles are used instead of RecordCommunitiesAction so that each workflow can have independent read/draft/manage permissions.

community_curator_roles: list[str] = dataclasses.field(default_factory=lambda: ['curator']) class-attribute instance-attribute

Community roles that allow users to review and curate records.