Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions site/cds_rdm/requests/committee_approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def execute(self, identity: Identity, uow: UnitOfWork) -> None:
# the request is persisted as submitted.
enrollment = _resolve_enrollment(self.request)

# Only the enrollment's referee group may receive (and so accept) the
# request, whatever receiver the caller passed.
if self.request.receiver.reference_dict != {"group": enrollment.referee_group}:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if user provided receiver_group is popped in views.py is this even a possibility?

raise ValidationError(
"Committee approval requests must be sent to the community's "
"referee group."
)

# Reject if the parent already carries an approval report number.
if (
topic.parent.get("permission_flags", {})
Expand Down
56 changes: 41 additions & 15 deletions site/cds_rdm/requests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,34 @@ def submit_committee_approval(pid_value):
return jsonify({"message": str(e)}), 409

body = request.get_json(force=True) or {}
receiver_group = body.pop("receiver_group", None)
# The receiver always comes from the community's enrollment. A
# client-provided ``receiver_group`` is ignored so that callers cannot
# route the request (and its accept action) to a group of their choice.
body.pop("receiver_group", None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the request body validated against a schema where the existence of this field could raise a validation error?

payload = body.get("payload", {})

if not receiver_group:
return jsonify({"message": "receiver_group is required"}), 400
default_community_id = (record._record.parent.get("communities", {}) or {}).get(
"default"
)
enrollment = get_committee_approval_enrollment(default_community_id)
if not enrollment:
return (
jsonify(
{
"message": "The record's community is not enrolled in the "
"committee approval workflow."
}
),
400,
)

try:
# Resolve receiver group entity.
receiver = ResolverRegistry.resolve_entity(
{"group": receiver_group}, raise_=True
{"group": enrollment.referee_group}, raise_=True
)

title = record.data.get("metadata", {}).get("title", "")
default_community_id = (
record._record.parent.get("communities", {}) or {}
).get("default")
enrollment = get_committee_approval_enrollment(default_community_id)
approval_label = (enrollment and enrollment.label) or "Committee approval"
payload["approval_label"] = approval_label
payload["approval_label"] = enrollment.label or "Committee approval"
req = current_requests_service.create(
identity=g.identity,
data={
Expand Down Expand Up @@ -176,12 +185,15 @@ def publish_public_record(pid_value):
src = draft_record.data
src_id = src["id"]

src_identifiers = src.get("metadata", {}).get("identifiers", [])
src_resource_type = src.get("metadata", {}).get("resource_type")

# Strip apprn from identifiers — CommitteeApprovalComponent regenerates it
# from committee_approval.reportnumber on every update_draft / publish.
# Strip cdsrn too: CDS report numbers are unique per record and stay on
# the restricted record. They are added below as related identifiers.
new_identifiers = [
i
for i in src.get("metadata", {}).get("identifiers", [])
if i.get("scheme") != "apprn"
i for i in src_identifiers if i.get("scheme") not in ("apprn", "cdsrn")
]

new_related = list(src.get("metadata", {}).get("related_identifiers", []))
Expand All @@ -190,11 +202,25 @@ def publish_public_record(pid_value):
"scheme": "cds",
"relation_type": {"id": "isversionof"},
}
src_resource_type = src.get("metadata", {}).get("resource_type")
if src_resource_type:
isversionof_entry["resource_type"] = src_resource_type
new_related.append(isversionof_entry)

# Keep the report numbers findable on the public record, in the same
# shape the INSPIRE harvester writes and matches (related cdsrn,
# isvariantformof).
for identifier in src_identifiers:
if identifier.get("scheme") != "cdsrn":
continue
report_number_entry = {
"identifier": identifier["identifier"],
"scheme": "cdsrn",
"relation_type": {"id": "isvariantformof"},
}
if src_resource_type:
report_number_entry["resource_type"] = src_resource_type
new_related.append(report_number_entry)

new_metadata = {
**copy.deepcopy(src.get("metadata", {})),
"identifiers": new_identifiers,
Expand Down
23 changes: 23 additions & 0 deletions site/tests/test_committee_approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,29 @@ def test_committee_approval_submit_permissions(
assert request.data["status"] == "submitted"


def test_committee_approval_submit_rejects_other_receiver_group(
record_in_enrolled_community,
community_manager,
ep_referee_group,
ep_request_payload,
app,
db,
):
"""A request sent to any group but the enrollment's referee group is refused."""
ds = app.extensions["invenio-accounts"].datastore
ds.create_role(id="not-the-referees", name="not-the-referees")
db.session.commit()

with pytest.raises(ValidationError, match="referee group"):
current_requests_service.create(
identity=community_manager.identity,
data=ep_request_payload,
request_type=current_request_type_registry.lookup("committee-approval"),
receiver={"group": "not-the-referees"},
topic={"record": record_in_enrolled_community.id},
)


# ---------------------------------------------------------------------------
# Permissions: who can read an EP approval request
# ---------------------------------------------------------------------------
Expand Down
Loading