From 5e6e322bb102296b84c45919513efd56fc238e62 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 31 Aug 2023 14:13:54 +0200 Subject: [PATCH] Bug 29523: Add redaction for inaccessible objects This patch switches from removing inaccessible items from the responses to instead redacting all the core fields in innaccessible responses. This allows for embed traversal and keeps counts etc correct but also hides the data we want to hide. --- Koha/Object.pm | 17 ++++++++++++++++- Koha/Patron.pm | 2 +- t/db_dependent/Koha/Object.t | 5 ++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index c05a923cfee..529ec503436 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -406,6 +406,10 @@ sub TO_JSON { and looks_like_number( $unblessed->{$col} ) ) { + if ( $self->{_redact} ) { + $unblessed->{$col} = 0; + } + # TODO: Remove once the solution for # https://github.com/perl5-dbi/DBD-mysql/issues/212 # is ported to whatever distro we support by that time @@ -415,6 +419,9 @@ sub TO_JSON { elsif ( _decimal_column_type( $columns_info->{$col}->{data_type} ) and looks_like_number( $unblessed->{$col} ) ) { + if ( $self->{_redact} ) { + $unblessed->{$col} = 0.00; + } # TODO: Remove once the solution for # https://github.com/perl5-dbi/DBD-mysql/issues/212 @@ -423,6 +430,9 @@ sub TO_JSON { $unblessed->{$col} += 0.00; } elsif ( _datetime_column_type( $columns_info->{$col}->{data_type} ) ) { + if ( $self->{_redact} ) { + $unblessed->{$col} = "0000-01-01 00:00:01"; + } eval { return unless $unblessed->{$col}; $unblessed->{$col} = output_pref({ @@ -431,6 +441,9 @@ sub TO_JSON { }); }; } + elsif ( $self->{_redact} ) { + $unblessed->{$col} = '*****'; + } } return $unblessed; } @@ -552,7 +565,9 @@ Returns a representation of the object, suitable for API output. sub to_api { my ( $self, $params ) = @_; - return unless $self->is_accessible($params); + unless ( $self->is_accessible($params) ) { + $self->{_redact} = 1; + } my $json_object = $self->TO_JSON; diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 0bffc050241..2006ad5087f 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -2289,7 +2289,7 @@ sub set_default_messaging_preferences { if ( $patron->is_accessible({ user => $logged_in_user }) ) { ... } -This overloaded method validates wether the current I object can be accessed +This overloaded method validates whether the current I object can be accessed by the logged in user. Returns 0 if the I parameter is missing. diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index ba1b82de5ef..cd1425983e3 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -555,7 +555,6 @@ subtest "to_api() tests" => sub { } ); - my $patron_1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library_1->id } } ); @@ -565,8 +564,8 @@ subtest "to_api() tests" => sub { t::lib::Mocks::mock_userenv( { patron => $patron } ); - is( ref($patron_1->to_api({ user => $patron })), 'HASH', 'Returns the object hash' ); - is( $patron_2->to_api({ user => $patron }), undef, 'Not accessible, returns undef' ); + is( $patron_1->to_api({ user => $patron })->{firstname}, $patron_1->firstname, 'Returns unredacted object hash' ); + is( $patron_2->to_api({ user => $patron })->{firstname}, "*****", 'Returns redacted object hash' ); $schema->storage->txn_rollback; }; -- 2.41.0