From 9d18ebf1dded145da46b26a40813b1f24ab3372c Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 26 Nov 2019 19:45:13 +0100 Subject: [PATCH] Bug 24151: Pseudonymize transactions on the fly Sponsored-by: Association KohaLa - https://koha-fr.org/ --- C4/Stats.pm | 39 ++++++++++------- Koha/Anonymized/Transaction.pm | 92 +++++++++++++++++++++++++++++++++++++++++ Koha/Anonymized/Transactions.pm | 53 ++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 16 deletions(-) create mode 100644 Koha/Anonymized/Transaction.pm create mode 100644 Koha/Anonymized/Transactions.pm diff --git a/C4/Stats.pm b/C4/Stats.pm index 8a8835c346..c7552d6553 100644 --- a/C4/Stats.pm +++ b/C4/Stats.pm @@ -18,12 +18,15 @@ package C4::Stats; # You should have received a copy of the GNU General Public License # along with Koha; if not, see . -use strict; -use warnings; +use Modern::Perl; require Exporter; use Carp; use C4::Context; use C4::Debug; + +use Koha::DateUtils qw( dt_from_string ); +use Koha::Statistics; +use Koha::Anonymized::Transactions; use vars qw(@ISA @EXPORT); our $debug; @@ -124,20 +127,24 @@ sub UpdateStats { my $location = exists $params->{location} ? $params->{location} : undef; my $ccode = exists $params->{ccode} ? $params->{ccode} : ''; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare( - "INSERT INTO statistics - (datetime, - branch, type, value, - other, itemnumber, itemtype, location, - borrowernumber, ccode) - VALUES (now(),?,?,?,?,?,?,?,?,?)" - ); - $sth->execute( - $branch, $type, $amount, $other, - $itemnumber, $itemtype, $location, $borrowernumber, - $ccode - ); + my $statistic = Koha::Statistic->new( + { + datetime => dt_from_string, + branch => $branch, + type => $type, + value => $amount, + other => $other, + itemnumber => $itemnumber, + itemtype => $itemtype, + location => $location, + borrowernumber => $borrowernumber, + ccode => $ccode, + } + )->store; + + Koha::Anonymized::Transaction->new_from_statistic($statistic)->store + if C4::Context->preference('Pseudonymization') + && grep { $_ eq $params->{type} } qw(renew issue return onsite_checkout); } 1; diff --git a/Koha/Anonymized/Transaction.pm b/Koha/Anonymized/Transaction.pm new file mode 100644 index 0000000000..e536674629 --- /dev/null +++ b/Koha/Anonymized/Transaction.pm @@ -0,0 +1,92 @@ +package Koha::Anonymized::Transaction; + +# 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 Modern::Perl; + +use Carp; +use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64); + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Anonymized::Transaction - Koha Anonymized::Transaction Object class + +=head1 API + +=head2 Class methods + +=head3 new + +=cut + +sub get_hash { + my ( $class, $borrowernumber) = @_; + return bcrypt($borrowernumber, C4::Context->config('key')); +} + + +sub new_from_statistic { + my ( $class, $statistic ) = @_; + + my $values = { + hashed_borrowernumber => $class->get_hash($statistic->borrowernumber), + }; + + my @fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || ''; + + if ( grep { $_ eq 'branchcode' } @fields_to_copy ) { + $values->{branchcode} = $statistic->branch; + } + if ( grep { $_ eq 'holdingbranch' } @fields_to_copy ) { + $values->{holdingbranch} = $statistic->item->holdingbranch; + } + if ( grep { $_ eq 'transaction_type' } @fields_to_copy ) { + $values->{transaction_type} = $statistic->type; + } + if ( grep { $_ eq 'itemcallnumber' } @fields_to_copy ) { + $values->{itemcallnumber} = $statistic->item->itemcallnumber; + } + + + @fields_to_copy = grep { + $_ ne 'branchcode' + && $_ ne 'holdingbranch' + && $_ ne 'transaction_type' + && $_ ne 'itemcallnumber' + } @fields_to_copy; + + $values = { + %$values, + map { $_ => $statistic->$_ } @fields_to_copy}; + + return $class->SUPER::new($values); +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'AnonymizedTransaction'; +} + +1; diff --git a/Koha/Anonymized/Transactions.pm b/Koha/Anonymized/Transactions.pm new file mode 100644 index 0000000000..27cd4fb5fc --- /dev/null +++ b/Koha/Anonymized/Transactions.pm @@ -0,0 +1,53 @@ +package Koha::Anonymized::Transactions; + +# 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 Modern::Perl; + +use Carp; +use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64); + +use Koha::Database; + +use Koha::Anonymized::Transaction; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Anonymized::Transactions - Koha Anonymized Transaction Object set class + +=head1 API + +=cut + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'AnonymizedTransaction'; +} + +sub object_class { + return 'Koha::Anonymized::Transaction'; +} + +1; -- 2.11.0