@@ -, +, @@ --- Koha/Object.pm | 17 ++++++++++++++++- Koha/Patron.pm | 2 +- t/db_dependent/Koha/Object.t | 5 ++--- 3 files changed, 19 insertions(+), 5 deletions(-) --- a/Koha/Object.pm +++ a/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; --- a/Koha/Patron.pm +++ a/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. --- a/t/db_dependent/Koha/Object.t +++ a/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; }; --