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

(-)a/Koha/Object.pm (+35 lines)
Lines 843-848 sub _get_object_class { Link Here
843
    return ${type};
843
    return ${type};
844
}
844
}
845
845
846
sub _get_objects_class {
847
    my ( $self ) = @_;
848
    return unless $self;
849
850
    if ( $self->_result->can('koha_objects_class') ) {
851
        return $self->_result->koha_objects_class;
852
    }
853
    my $type = ref($self);
854
855
    $type =~ s|Schema::Result::||;
856
    return $type . "s";
857
}
858
846
=head3 AUTOLOAD
859
=head3 AUTOLOAD
847
860
848
The autoload method is used only to get and set values for an objects properties.
861
The autoload method is used only to get and set values for an objects properties.
Lines 922-927 sub _handle_to_api_child { Link Here
922
    return $res;
935
    return $res;
923
}
936
}
924
937
938
=head3 accessible
939
940
    if ( $object->accessible ) { ... }
941
942
Whether the object should be accessible in the current context (requesting user).
943
It relies on the plural class properly implementing the I<search_limited> method.
944
945
=cut
946
947
sub accessible {
948
    my ($self) = @_;
949
950
    return $self->_get_objects_class->search_limited(
951
        {
952
            map { $_ => $self->$_ }
953
              $self->_result->result_source->primary_columns
954
        }
955
      )->count > 0
956
      ? 1
957
      : 0;
958
}
959
925
sub DESTROY { }
960
sub DESTROY { }
926
961
927
=head1 AUTHOR
962
=head1 AUTHOR
(-)a/t/db_dependent/Koha/Object.t (-2 / +35 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 21;
20
use Test::More tests => 22;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
use DateTime;
23
use DateTime;
Lines 996-998 subtest 'messages() and add_message() tests' => sub { Link Here
996
996
997
    $schema->storage->txn_rollback;
997
    $schema->storage->txn_rollback;
998
};
998
};
999
- 
999
1000
subtest 'accessible() tests' => sub {
1001
1002
    plan tests => 2;
1003
1004
    $schema->storage->txn_begin;
1005
1006
    my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
1007
    my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
1008
1009
    my $patron = $builder->build_object(
1010
        {
1011
            class => 'Koha::Patrons',
1012
            value => {
1013
                flags      => 2**2,            # only has catalogue permissions
1014
                branchcode => $library_1->id
1015
            }
1016
        }
1017
    );
1018
1019
    my $patron_1 = $builder->build_object(
1020
        { class => 'Koha::Patrons', value => { branchcode => $library_1->id } }
1021
    );
1022
    my $patron_2 = $builder->build_object(
1023
        { class => 'Koha::Patrons', value => { branchcode => $library_2->id } }
1024
    );
1025
1026
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1027
1028
    ok( $patron_1->accessible,  'Has access to the patron' );
1029
    ok( !$patron_2->accessible, 'Does not have access to the patron' );
1030
1031
    $schema->storage->txn_rollback;
1032
};

Return to bug 29523