From bdfafa8e292255462a803356ae816e45af4dcda9 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Wed, 14 Aug 2024 04:02:28 +0000 Subject: [PATCH] Bug 37640: Allow patron REST API endpoint to accept cardnumber This enhancement adds a new system preference RESTPatronIDField. It is set to borrowernumber by default, to support the standard behaviour of providing a borrowernumber to the Koha REST API endpoints to identify a patron. When the syspref is set to cardnumber, Koha will identify the patron using a provided cardnumber and not match on borrowernumber at all. This is useful in cases where organisations may want to use the API endpoints to do patron actions, but don't have the borrowernumber, as this is a unique ID generated within Koha. To test: 1. Create a patron in the staff interface. Take note of their borrowernumber. Set the cardnumber to something - a string or an integer - and take note of this too. 2. In another tab, go to the API endpoint for getPatron your OPAC URL/api/v1/patrons/{patron_id} 3. Replace {patron_id} in the URL with the borrowernumber. Confirm your patron's data is returned successfully. 4. Replace {patron_id} in the URL with the cardnumber. Notice an error is thrown. 5. Apply the patch. Install database updates. You may need to regenerate the API spec to make the API changes take effect: https://wiki.koha-community.org/wiki/Rest_Api_HowTo#Step_7:_Regenerate_API_spec . Then restart services. 6. In the staff interface tab, go to Koha Administration -> system preferences. Search for the new preference RESTPatronIDField. Notice it is set to borrowernumber by default. 7. In your API endpoint tab, again replace {patron_id} in the URL with the borrowernumber. Confirm your patron's data is returned successfully. 8. Change the RESTPatronIDField system preference to cardnumber and save. 9. Refresh your API endpoint tab. Replace {patron_id} in the URL with the cardnumber. Confirm your patron's data is returned successfully. Note: it's possible that giving a borrowernumber when the API endpoint is expecting a cardnumber will still return results, and vice versa - if your cardnumber is an integer and happens to also be a borrowernumber. It's important in this case that only one or the other is used to ensure expected results. Sponsored-by: Chartered Accountants Australia and New Zealand --- Koha/REST/V1/Patrons.pm | 9 ++++++++- api/v1/swagger/paths/patrons.yaml | 2 +- api/v1/swagger/swagger.yaml | 4 ++-- .../bug_37640_-_add_RESTPatronIDField_syspref.pl | 16 ++++++++++++++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../modules/admin/preferences/web_services.pref | 7 +++++++ 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_37640_-_add_RESTPatronIDField_syspref.pl diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index ca620109a00..eb3403b15af 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -77,7 +77,14 @@ sub get { return try { my $patron_id = $c->param('patron_id'); - my $patron = $c->objects->find( Koha::Patrons->search_limited, $patron_id ); + my $patron; + + if ( C4::Context->preference('RESTPatronIDField') eq 'cardnumber' ) { + my $patron_obj = Koha::Patrons->search_limited({ cardnumber => $patron_id })->next; + $patron = $c->objects->find( Koha::Patrons->search_limited, $patron_obj->id ) if $patron_obj; + } else { + $patron = $c->objects->find( Koha::Patrons->search_limited, $patron_id ); + } return $c->render_resource_not_found("Patron") unless $patron; diff --git a/api/v1/swagger/paths/patrons.yaml b/api/v1/swagger/paths/patrons.yaml index cf05d748d8b..a564988e7ad 100644 --- a/api/v1/swagger/paths/patrons.yaml +++ b/api/v1/swagger/paths/patrons.yaml @@ -487,7 +487,7 @@ - name: x-koha-embed in: header required: false - description: Embed list sent as a request header + description: Embed list sent as a request header hello type: array items: type: string diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index dd72361380e..bac1cd21b32 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -829,11 +829,11 @@ parameters: required: false type: integer patron_id_pp: - description: Internal patron identifier + description: Internal patron identifier. May be borrowernumber or cardnumber. in: path name: patron_id required: true - type: integer + type: string patron_id_qp: description: Internal patron identifier in: query diff --git a/installer/data/mysql/atomicupdate/bug_37640_-_add_RESTPatronIDField_syspref.pl b/installer/data/mysql/atomicupdate/bug_37640_-_add_RESTPatronIDField_syspref.pl new file mode 100755 index 00000000000..73e1fcdf92a --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_37640_-_add_RESTPatronIDField_syspref.pl @@ -0,0 +1,16 @@ +use Modern::Perl; + +return { + bug_number => "37640", + description => "Allow patron REST API endpoint to accept cardnumber", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + $dbh->do( + q{ INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES ('RESTPatronIDField', 'borrowernumber', 'borrowernumber|cardnumber', 'Allow patrons to be identified by cardnumber instead of borrowernumber', 'Choice') } + ); + + say $out "Added system preference 'RESTPatronIDField'"; + }, +}; diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 1a7cd033e90..1d1156ffe7b 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -666,6 +666,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('RESTBasicAuth','0',NULL,'If enabled, Basic authentication is enabled for the REST API.','YesNo'), ('RESTdefaultPageSize','20','','Default page size for endpoints listing objects','Integer'), ('RESTOAuth2ClientCredentials','0',NULL,'If enabled, the OAuth2 client credentials flow is enabled for the REST API.','YesNo'), +('RESTPatronIDField', 'borrowernumber', 'borrowernumber|cardnumber', 'Allow patrons to be identified by cardnumber instead of borrowernumber', 'Choice'), ('RESTPublicAnonymousRequests','1',NULL,'If enabled, the API will allow anonymous access to public routes that don\'t require authenticated access.','YesNo'), ('RESTPublicAPI','1',NULL,'If enabled, the REST API will expose the /public endpoints.','YesNo'), ('RestrictedPageContent','',NULL,'HTML content of the restricted page','TextArea'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/web_services.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/web_services.pref index d5c7c1feb24..525cb597f3d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/web_services.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/web_services.pref @@ -46,6 +46,13 @@ Web services: none: "NULL" apirenew: "'APIRenew'" - as branchcode to store in the statistics table. + - + - Patrons will be identified using the + - pref: RESTPatronIDFIeld + choices: + borrowernumber: borrowernumber + cardnumber: cardnumber + - field. OAI-PMH: - - pref: OAI-PMH -- 2.39.2