From 80e69bafae5a61cde114f768eeb4cd4d8c602be1 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Thu, 6 Aug 2015 15:17:00 +0300 Subject: [PATCH] Bug 14656 - Delete Bibliographic Records REST API route & permission Adds a new permission 'editcatalogue' => 'delete_catalogue' and changes the /cgi-bin/koha/catalogue/detail.pl?biblionumber=XXX -view to use the REST API to delete a Biblio. Includes PageObject tests and REST API tests and atomicupdate-script TEST PLAN: -1. Reload the REST API daemon. For Hypnotoad, start the service again to hot-swap code changes. For other daemonizers follow vendor instructions ;) 0. Grant delete_catalogue-permission to the logged in user. 1. Find a Biblio. 2. Delete it. 3. Confirm that the biblio is no longer found. --- Koha/REST/V1/Biblios.pm | 28 +++++++ api/v1/swagger.json | 50 +++++++++++ .../Bug14656-DeleteBibRecordsRESTAPI+Permission.pl | 33 ++++++++ .../data/mysql/en/mandatory/userpermissions.sql | 1 + .../intranet-tmpl/prog/en/includes/cat-toolbar.inc | 40 +++++++-- t/db_dependent/Api/V1/Biblios.pm | 96 ++++++++++++++++++++++ t/db_dependent/Catalogue/detail.t | 89 ++++++++++++++++++++ 7 files changed, 328 insertions(+), 9 deletions(-) create mode 100644 Koha/REST/V1/Biblios.pm create mode 100644 installer/data/mysql/atomicupdate/Bug14656-DeleteBibRecordsRESTAPI+Permission.pl create mode 100644 t/db_dependent/Api/V1/Biblios.pm create mode 100644 t/db_dependent/Catalogue/detail.t diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm new file mode 100644 index 0000000..8cc125b --- /dev/null +++ b/Koha/REST/V1/Biblios.pm @@ -0,0 +1,28 @@ +package Koha::REST::V1::Biblios; + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Koha::Database; + +sub delete_biblio { + my ($c, $args, $cb) = @_; + + my $schema = Koha::Database->new->schema; + + my $biblio = $schema->resultset('Biblio')->find({biblionumber => $args->{biblionumber}}); + unless ($biblio) { + return $c->$cb({error => "Biblio not found"}, 404); + } + + my $itemCount = $schema->resultset('Item')->search({biblionumber => $args->{biblionumber}})->count(); + if ($itemCount) { + return $c->$cb({error => "Biblio has Items attached. Delete them first."}, 400); + } + + $biblio->delete(); + return $c->$cb('', 204); +} + +1; diff --git a/api/v1/swagger.json b/api/v1/swagger.json index 47d8a94..bf56a1d 100644 --- a/api/v1/swagger.json +++ b/api/v1/swagger.json @@ -14,6 +14,49 @@ }, "basePath": "/v1", "paths": { + "/biblios/{biblionumber}": { + "delete": { + "x-mojo-controller": "Koha::REST::V1::Biblios", + "x-koha-permission": { + "editcatalogue": "delete_catalogue" + }, + "operationId": "deleteBiblio", + "tags": ["biblios"], + "summary": "Deletes the given Bibliographic Record", + "description": "Can fail if there are dependecies to the Biblio.", + "produces": [ + "" + ], + "parameters": [ + { + "$ref": "#/parameters/biblionumberPathParam" + } + ], + "responses": { + "204": { + "description": "Deleting the Record succeeded.", + "schema": { + "type": "string" + } + }, + "404": { + "description": "No such Bibliographic record found", + "schema": { + "$ref": "#/definitions/error" + } + }, + "400": { + "description": "Cannot delete the Bibliographic Record due to constraints. A constraint can be for example the presence of dependant Items.", + "schema": { + "$ref": "#/definitions/error" + } + } + }, + "security": [ + { "multi_key_auth": [] } + ] + } + }, "/borrowers": { "get": { "x-mojo-controller": "Koha::REST::V1::Borrowers", @@ -459,6 +502,13 @@ } }, "parameters": { + "biblionumberPathParam": { + "name": "biblionumber", + "in": "path", + "description": "Internal biblio identifier", + "required": true, + "type": "integer" + }, "borrowernumberPathParam": { "name": "borrowernumber", "in": "path", diff --git a/installer/data/mysql/atomicupdate/Bug14656-DeleteBibRecordsRESTAPI+Permission.pl b/installer/data/mysql/atomicupdate/Bug14656-DeleteBibRecordsRESTAPI+Permission.pl new file mode 100644 index 0000000..5d48e54 --- /dev/null +++ b/installer/data/mysql/atomicupdate/Bug14656-DeleteBibRecordsRESTAPI+Permission.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +# Copyright Open Source Freedom Fighters +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use C4::Context; +use Koha::AtomicUpdater; + +my $dbh = C4::Context->dbh(); +my $atomicUpdater = Koha::AtomicUpdater->new(); + +unless($atomicUpdater->find('Bug14656')) { + + use Koha::Auth::PermissionManager; + my $pm = Koha::Auth::PermissionManager->new(); + $pm->addPermission({module => 'editcatalogue', code => 'delete_catalogue', description => "Allow deleting bibliographic records"}); + + print "Upgrade done (Bug 14656: Add biblio delete (delete_catalogue) permission.)\n"; +} diff --git a/installer/data/mysql/en/mandatory/userpermissions.sql b/installer/data/mysql/en/mandatory/userpermissions.sql index c8cae26..c7f8b66 100644 --- a/installer/data/mysql/en/mandatory/userpermissions.sql +++ b/installer/data/mysql/en/mandatory/userpermissions.sql @@ -13,6 +13,7 @@ INSERT INTO permissions (module, code, description) VALUES ( 'reserveforothers','place_holds', 'Place holds for patrons'), ( 'reserveforothers','modify_holds_priority', 'Modify holds priority'), ( 'editcatalogue', 'edit_catalogue', 'Edit catalog (Modify bibliographic/holdings data)'), + ( 'editcatalogue', 'delete_catalogue', 'Allow deleting bibliographic records'), ( 'editcatalogue', 'fast_cataloging', 'Fast cataloging'), ( 'editcatalogue', 'edit_items', 'Edit items'), ( 'editcatalogue', 'edit_items_restricted', 'Limit item modification to subfields defined in the SubfieldsToAllowForRestrictedEditing preference (please note that edit_item is still required)'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/cat-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/cat-toolbar.inc index ee39f31..888bcce 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/cat-toolbar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/cat-toolbar.inc @@ -27,7 +27,7 @@ function addToShelf() { window.open('/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?biblionumber=[% biblionumber %]','Add_to_virtualshelf','width=500,height=400,toolbar=false,scrollbars=yes'); } function printBiblio() {window.print(); } -[% IF CAN_user_editcatalogue_edit_catalogue or ( frameworkcode == 'FA' and CAN_user_editcatalogue_fast_cataloging ) %] +[% IF CAN_user_editcatalogue_edit_catalogue or CAN_user_editcatalogue_delete_catalogue or ( frameworkcode == 'FA' and CAN_user_editcatalogue_fast_cataloging ) %] function confirm_deletion() { var count = [% count %]; var holdcount = [% holdcount %]; @@ -60,12 +60,34 @@ if ( count > 0 || holdcount > 0 ){ return false; } else { - window.location="/cgi-bin/koha/cataloguing/addbiblio.pl?op=delete&biblionumber=[% biblionumber %]"; + deleteBiblio([% biblionumber %]); } } else { return false; } } + function deleteBiblio (biblionumber) { + $.ajax({ + "cache": false, + "headers": {}, + "method": "delete", + "type":"delete", + "url": "/v1/biblios/"+biblionumber, + "dataType": "json", + "success": function (data, textStatus, jqXHR) { + window.location="/cgi-bin/koha/catalogue/search.pl"; + }, + "error": function (jqXHR, textStatus, errorThrown) { + var errorObject = $.parseJSON( jqXHR.responseText ); + if (!errorObject || !errorObject.error) { + alert(jqXHR.responseText); + } + else { + alert(errorObject.error); + } + } + }); + } [% END %] [% IF CAN_user_editcatalogue_edit_items or ( frameworkcode == 'FA' and CAN_user_editcatalogue_fast_cataloging ) %] @@ -135,7 +157,7 @@ [% IF ( CAN_user_editcatalogue_edit_catalogue || CAN_user_editcatalogue_edit_items || CAN_user_serials_create_subscription ) %] -
+
[% END %] -[% IF ( CAN_user_editcatalogue_edit_catalogue || CAN_user_editcatalogue_edit_items || CAN_user_tools_items_batchmod || CAN_user_tools_items_batchdel ) or ( frameworkcode == 'FA' and CAN_user_editcatalogue_fast_cataloging ) %] -
+[% IF ( CAN_user_editcatalogue_edit_catalogue || CAN_user_editcatalogue_delete_catalogue || CAN_user_editcatalogue_edit_items || CAN_user_tools_items_batchmod || CAN_user_tools_items_batchdel ) or ( frameworkcode == 'FA' and CAN_user_editcatalogue_fast_cataloging ) %] +
[% END %] -
+
[% IF ( virtualshelves && intranetbookbag ) %] -
+