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

(-)a/Koha/Object.pm (+35 lines)
Lines 893-898 sub _get_object_class { Link Here
893
    return ${type};
893
    return ${type};
894
}
894
}
895
895
896
sub _get_objects_class {
897
    my ( $self ) = @_;
898
    return unless $self;
899
900
    if ( $self->_result->can('koha_objects_class') ) {
901
        return $self->_result->koha_objects_class;
902
    }
903
    my $type = ref($self);
904
905
    $type =~ s|Schema::Result::||;
906
    return $type . "s";
907
}
908
896
=head3 AUTOLOAD
909
=head3 AUTOLOAD
897
910
898
The autoload method is used only to get and set values for an objects properties.
911
The autoload method is used only to get and set values for an objects properties.
Lines 972-977 sub _handle_to_api_child { Link Here
972
    return $res;
985
    return $res;
973
}
986
}
974
987
988
=head3 accessible
989
990
    if ( $object->accessible ) { ... }
991
992
Whether the object should be accessible in the current context (requesting user).
993
It relies on the plural class properly implementing the I<search_limited> method.
994
995
=cut
996
997
sub accessible {
998
    my ($self) = @_;
999
1000
    return $self->_get_objects_class->search_limited(
1001
        {
1002
            map { $_ => $self->$_ }
1003
              $self->_result->result_source->primary_columns
1004
        }
1005
      )->count > 0
1006
      ? 1
1007
      : 0;
1008
}
1009
975
sub DESTROY { }
1010
sub DESTROY { }
976
1011
977
=head1 AUTHOR
1012
=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 1112-1114 subtest 'messages() and add_message() tests' => sub { Link Here
1112
1112
1113
    $schema->storage->txn_rollback;
1113
    $schema->storage->txn_rollback;
1114
};
1114
};
1115
- 
1115
1116
subtest 'accessible() tests' => sub {
1117
1118
    plan tests => 2;
1119
1120
    $schema->storage->txn_begin;
1121
1122
    my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
1123
    my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
1124
1125
    my $patron = $builder->build_object(
1126
        {
1127
            class => 'Koha::Patrons',
1128
            value => {
1129
                flags      => 2**2,            # only has catalogue permissions
1130
                branchcode => $library_1->id
1131
            }
1132
        }
1133
    );
1134
1135
    my $patron_1 = $builder->build_object(
1136
        { class => 'Koha::Patrons', value => { branchcode => $library_1->id } }
1137
    );
1138
    my $patron_2 = $builder->build_object(
1139
        { class => 'Koha::Patrons', value => { branchcode => $library_2->id } }
1140
    );
1141
1142
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1143
1144
    ok( $patron_1->accessible,  'Has access to the patron' );
1145
    ok( !$patron_2->accessible, 'Does not have access to the patron' );
1146
1147
    $schema->storage->txn_rollback;
1148
};

Return to bug 29523