diff --git a/Koha/REST/V1/ArticleRequests.pm b/Koha/REST/V1/ArticleRequests.pm index d28b8aaa37..f5add11e89 100644 --- a/Koha/REST/V1/ArticleRequests.pm +++ b/Koha/REST/V1/ArticleRequests.pm @@ -73,22 +73,23 @@ Controller function that handles cancelling a patron's Koha::ArticleRequest obje sub patron_cancel { my $c = shift->openapi->valid_input or return; - my $patron = Koha::Patrons->find( $c->param('patron_id') ); - - return $c->render_resource_not_found("Patron") - unless $patron; - - # patron_id has been validated by the allow-owner check, so the following call to related - # article requests covers the case of article requests not belonging to the patron - my $article_request = $patron->article_requests->find( $c->param('article_request_id') ); - - return $c->render_resource_not_found("Article request") - unless $article_request; - - my $reason = $c->param('cancellation_reason'); - my $notes = $c->param('notes'); - + my $patron_id = $c->param('patron_id'); return try { + $c->auth->public($patron_id); + my $patron = Koha::Patrons->find($patron_id); + + return $c->render_resource_not_found("Patron") + unless $patron; + + # patron_id has been validated by the $c->auth->public check, so the following call to related + # article requests covers the case of article requests not belonging to the patron + my $article_request = $patron->article_requests->find( $c->param('article_request_id') ); + + return $c->render_resource_not_found("Article request") + unless $article_request; + + my $reason = $c->param('cancellation_reason'); + my $notes = $c->param('notes'); $article_request->cancel( { diff --git a/api/v1/swagger/paths/article_requests.yaml b/api/v1/swagger/paths/article_requests.yaml index d0fc698557..12196f8456 100644 --- a/api/v1/swagger/paths/article_requests.yaml +++ b/api/v1/swagger/paths/article_requests.yaml @@ -112,6 +112,4 @@ "503": description: Under maintenance schema: - $ref: "../swagger.yaml#/definitions/error" - x-koha-authorization: - allow-owner: true + $ref: "../swagger.yaml#/definitions/error" \ No newline at end of file diff --git a/t/db_dependent/api/v1/article_requests.t b/t/db_dependent/api/v1/article_requests.t index 55208a91ac..28a8fe004f 100755 --- a/t/db_dependent/api/v1/article_requests.t +++ b/t/db_dependent/api/v1/article_requests.t @@ -83,7 +83,7 @@ subtest 'cancel() tests' => sub { subtest 'patron_cancel() tests' => sub { - plan tests => 14; + plan tests => 17; t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 ); t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); @@ -116,10 +116,14 @@ subtest 'patron_cancel() tests' => sub { my $article_request_2 = $builder->build_object( { class => 'Koha::ArticleRequests', value => { borrowernumber => $another_patron_id } } ); + # delete another patron's request when unauthorized + $t->delete_ok( "/api/v1/public/patrons/$another_patron_id/article_requests/" . $article_request_2->id ) + ->status_is(401)->json_is( '/error' => "Authentication failure." ); + # delete another patron's request $t->delete_ok( "//$userid:$password@/api/v1/public/patrons/$another_patron_id/article_requests/" . $article_request_2->id ) - ->status_is(403)->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); + ->status_is(403)->json_is( '/error' => "Unprivileged user cannot access another user's resources" ); my $another_article_request = $builder->build_object( {