@@ -, +, @@ --- Koha/Object.pm | 20 ++++++-------------- Koha/Patron.pm | 24 ++++++++++++++++++++++++ Koha/REST/Plugin/Objects.pm | 4 ++-- t/db_dependent/Koha/Object.t | 10 +++++----- 4 files changed, 37 insertions(+), 21 deletions(-) --- a/Koha/Object.pm +++ a/Koha/Object.pm @@ -552,7 +552,7 @@ Returns a representation of the object, suitable for API output. sub to_api { my ( $self, $params ) = @_; - return unless $self->accessible; + return unless $self->is_accessible($params); my $json_object = $self->TO_JSON; @@ -938,26 +938,18 @@ sub _handle_to_api_child { return $res; } -=head3 accessible +=head3 is_accessible - if ( $object->accessible ) { ... } + if ( $object->is_accessible ) { ... } -Whether the object should be accessible in the current context (requesting user). -It relies on the plural class properly implementing the I method. +Stub method that is expected to be overloaded (if required) by implementing classes. =cut -sub accessible { +sub is_accessible { my ($self) = @_; - return $self->_get_objects_class->search_limited( - { - map { $_ => $self->$_ } - $self->_result->result_source->primary_columns - } - )->count > 0 - ? 1 - : 0; + return 1; } sub DESTROY { } --- a/Koha/Patron.pm +++ a/Koha/Patron.pm @@ -1844,6 +1844,30 @@ sub get_extended_attribute { return $attribute->next; } +=head3 is_accessible + + if ( $patron->is_accessible({ user => $logged_in_user }) ) { ... } + +This overloaded method validates wether the current I object can be accessed +by the logged in user. + +Returns 0 if the I parameter is missing. + +=cut + +sub is_accessible { + my ( $self, $params ) = @_; + + # FIXME? It felt tempting to return 0 instead + # but it would mean needing to explicitly add the 'user' + # param in all tests... + return 1 + unless $params->{user}; + + my $consumer = $params->{user}; + return $consumer->can_see_patron_infos($self); +} + =head3 to_api my $json = $patron->to_api; --- a/Koha/REST/Plugin/Objects.pm +++ a/Koha/REST/Plugin/Objects.pm @@ -66,7 +66,7 @@ the requested object. It passes through any embeds if specified. return unless $object; - return $object->to_api({ embed => $embed }); + return $object->to_api({ embed => $embed, user => $c->stash('koha.user') }); } ); @@ -172,7 +172,7 @@ shouldn't be called twice in it. } ); - return $objects->to_api({ embed => $embed, public => $is_public }); + return $objects->to_api({ embed => $embed, public => $is_public, user => $c->stash('koha.user') }); } ); } --- a/t/db_dependent/Koha/Object.t +++ a/t/db_dependent/Koha/Object.t @@ -448,8 +448,8 @@ subtest "to_api() tests" => sub { t::lib::Mocks::mock_userenv( { patron => $patron } ); - is( ref($patron_1->to_api), 'HASH', 'Returns the object hash' ); - is( $patron_2->to_api, undef, 'Not accessible, returns undef' ); + 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' ); $schema->storage->txn_rollback; }; @@ -1031,7 +1031,7 @@ subtest 'messages() and add_message() tests' => sub { $schema->storage->txn_rollback; }; -subtest 'accessible() tests' => sub { +subtest 'is_accessible() tests' => sub { plan tests => 2; @@ -1059,8 +1059,8 @@ subtest 'accessible() tests' => sub { t::lib::Mocks::mock_userenv( { patron => $patron } ); - ok( $patron_1->accessible, 'Has access to the patron' ); - ok( !$patron_2->accessible, 'Does not have access to the patron' ); + ok( $patron_1->is_accessible({ user => $patron }), 'Has access to the patron' ); + ok( !$patron_2->is_accessible({ user => $patron }), 'Does not have access to the patron' ); $schema->storage->txn_rollback; }; --