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_in_context($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_in_context
942
942
943
    if ( $object->accessible ) { ... }
943
    if ( $object->is_accessible_in_context ) { ... }
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_in_context {
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 1752-1757 sub get_extended_attribute { Link Here
1752
    return $attribute->next;
1752
    return $attribute->next;
1753
}
1753
}
1754
1754
1755
=head3 is_accessible_in_context
1756
1757
    if ( $patron->is_accessible_in_context({ user => $logged_in_user }) ) { ... }
1758
1759
This overloaded method validates wether the current I<Koha::Patron> object can be accessed
1760
by the logged in user.
1761
1762
Returns 0 if the I<user> parameter is missing.
1763
1764
=cut
1765
1766
sub is_accessible_in_context {
1767
    my ( $self, $params ) = @_;
1768
1769
    # FIXME? It felt tempting to return 0 instead
1770
    # but it would mean needing to explicitly add the 'user'
1771
    # param in all tests...
1772
    return 1
1773
      unless $params->{user};
1774
1775
    my $consumer = $params->{user};
1776
    return $consumer->can_see_patron_infos($self);
1777
}
1778
1755
=head3 to_api
1779
=head3 to_api
1756
1780
1757
    my $json = $patron->to_api;
1781
    my $json = $patron->to_api;
(-)a/Koha/REST/Plugin/Objects.pm (-3 / +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 164-170 for API rendering. Link Here
164
                }
164
                }
165
            );
165
            );
166
166
167
            return $objects->to_api({ embed => $embed, public => $is_public });
167
            return $objects->to_api({ embed => $embed, public => $is_public, user => $c->stash('koha.user') });
168
        }
168
        }
169
    );
169
    );
170
}
170
}
171
- 

Return to bug 29523