View | Details | Raw Unified | Return to bug 29523
Collapse All | Expand All

(-)a/Koha/Object.pm (-14 / +6 lines)
Lines 552-558 Returns a representation of the object, suitable for API output. Link Here
552
sub to_api {
552
sub to_api {
553
    my ( $self, $params ) = @_;
553
    my ( $self, $params ) = @_;
554
554
555
    return unless $self->accessible;
555
    return unless $self->is_accessible($params);
556
556
557
    my $json_object = $self->TO_JSON;
557
    my $json_object = $self->TO_JSON;
558
558
Lines 938-963 sub _handle_to_api_child { Link Here
938
    return $res;
938
    return $res;
939
}
939
}
940
940
941
=head3 accessible
941
=head3 is_accessible
942
942
943
    if ( $object->accessible ) { ... }
943
    if ( $object->is_accessible ) { ... }
944
944
945
Whether the object should be accessible in the current context (requesting user).
945
Stub method that is expected to be overloaded (if required) by implementing classes.
946
It relies on the plural class properly implementing the I<search_limited> method.
947
946
948
=cut
947
=cut
949
948
950
sub accessible {
949
sub is_accessible {
951
    my ($self) = @_;
950
    my ($self) = @_;
952
951
953
    return $self->_get_objects_class->search_limited(
952
    return 1;
954
        {
955
            map { $_ => $self->$_ }
956
              $self->_result->result_source->primary_columns
957
        }
958
      )->count > 0
959
      ? 1
960
      : 0;
961
}
953
}
962
954
963
sub DESTROY { }
955
sub DESTROY { }
(-)a/Koha/Patron.pm (+24 lines)
Lines 1844-1849 sub get_extended_attribute { Link Here
1844
    return $attribute->next;
1844
    return $attribute->next;
1845
}
1845
}
1846
1846
1847
=head3 is_accessible
1848
1849
    if ( $patron->is_accessible({ user => $logged_in_user }) ) { ... }
1850
1851
This overloaded method validates wether the current I<Koha::Patron> object can be accessed
1852
by the logged in user.
1853
1854
Returns 0 if the I<user> parameter is missing.
1855
1856
=cut
1857
1858
sub is_accessible {
1859
    my ( $self, $params ) = @_;
1860
1861
    # FIXME? It felt tempting to return 0 instead
1862
    # but it would mean needing to explicitly add the 'user'
1863
    # param in all tests...
1864
    return 1
1865
      unless $params->{user};
1866
1867
    my $consumer = $params->{user};
1868
    return $consumer->can_see_patron_infos($self);
1869
}
1870
1847
=head3 to_api
1871
=head3 to_api
1848
1872
1849
    my $json = $patron->to_api;
1873
    my $json = $patron->to_api;
(-)a/Koha/REST/Plugin/Objects.pm (-2 / +2 lines)
Lines 66-72 the requested object. It passes through any embeds if specified. Link Here
66
66
67
            return unless $object;
67
            return unless $object;
68
68
69
            return $object->to_api({ embed => $embed });
69
            return $object->to_api({ embed => $embed, user => $c->stash('koha.user') });
70
        }
70
        }
71
    );
71
    );
72
72
Lines 172-178 shouldn't be called twice in it. Link Here
172
                }
172
                }
173
            );
173
            );
174
174
175
            return $objects->to_api({ embed => $embed, public => $is_public });
175
            return $objects->to_api({ embed => $embed, public => $is_public, user => $c->stash('koha.user') });
176
        }
176
        }
177
    );
177
    );
178
}
178
}
(-)a/t/db_dependent/Koha/Object.t (-6 / +5 lines)
Lines 448-455 subtest "to_api() tests" => sub { Link Here
448
448
449
        t::lib::Mocks::mock_userenv( { patron => $patron } );
449
        t::lib::Mocks::mock_userenv( { patron => $patron } );
450
450
451
        is( ref($patron_1->to_api), 'HASH', 'Returns the object hash' );
451
        is( ref($patron_1->to_api({ user => $patron })), 'HASH', 'Returns the object hash' );
452
        is( $patron_2->to_api, undef, 'Not accessible, returns undef' );
452
        is( $patron_2->to_api({ user => $patron }), undef, 'Not accessible, returns undef' );
453
453
454
        $schema->storage->txn_rollback;
454
        $schema->storage->txn_rollback;
455
    };
455
    };
Lines 1031-1037 subtest 'messages() and add_message() tests' => sub { Link Here
1031
    $schema->storage->txn_rollback;
1031
    $schema->storage->txn_rollback;
1032
};
1032
};
1033
1033
1034
subtest 'accessible() tests' => sub {
1034
subtest 'is_accessible() tests' => sub {
1035
1035
1036
    plan tests => 2;
1036
    plan tests => 2;
1037
1037
Lines 1059-1066 subtest 'accessible() tests' => sub { Link Here
1059
1059
1060
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1060
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1061
1061
1062
    ok( $patron_1->accessible,  'Has access to the patron' );
1062
    ok( $patron_1->is_accessible({ user => $patron }),  'Has access to the patron' );
1063
    ok( !$patron_2->accessible, 'Does not have access to the patron' );
1063
    ok( !$patron_2->is_accessible({ user => $patron }), 'Does not have access to the patron' );
1064
1064
1065
    $schema->storage->txn_rollback;
1065
    $schema->storage->txn_rollback;
1066
};
1066
};
1067
- 

Return to bug 29523