diff --git a/site/cds_rdm/requests/committee_approval.py b/site/cds_rdm/requests/committee_approval.py index 13c2cb47..686355d2 100644 --- a/site/cds_rdm/requests/committee_approval.py +++ b/site/cds_rdm/requests/committee_approval.py @@ -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}: + 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", {}) diff --git a/site/cds_rdm/requests/views.py b/site/cds_rdm/requests/views.py index d26b8453..c0591e81 100644 --- a/site/cds_rdm/requests/views.py +++ b/site/cds_rdm/requests/views.py @@ -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) 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={ @@ -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", [])) @@ -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, diff --git a/site/tests/test_committee_approval.py b/site/tests/test_committee_approval.py index a3113af3..24078a3d 100644 --- a/site/tests/test_committee_approval.py +++ b/site/tests/test_committee_approval.py @@ -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 # ---------------------------------------------------------------------------