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 977-982 sub _handle_to_api_child { Link Here
977
    return $res;
990
    return $res;
978
}
991
}
979
992
993
=head3 accessible
994
995
    if ( $object->accessible ) { ... }
996
997
Whether the object should be accessible in the current context (requesting user).
998
It relies on the plural class properly implementing the I<search_limited> method.
999
1000
=cut
1001
1002
sub accessible {
1003
    my ($self) = @_;
1004
1005
    return $self->_get_objects_class->search_limited(
1006
        {
1007
            map { $_ => $self->$_ }
1008
              $self->_result->result_source->primary_columns
1009
        }
1010
      )->count > 0
1011
      ? 1
1012
      : 0;
1013
}
1014
980
sub DESTROY { }
1015
sub DESTROY { }
981
1016
982
=head1 AUTHOR
1017
=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
23
Lines 1120-1122 subtest 'messages() and add_message() tests' => sub { Link Here
1120
1120
1121
    $schema->storage->txn_rollback;
1121
    $schema->storage->txn_rollback;
1122
};
1122
};
1123
- 
1123
1124
subtest 'accessible() tests' => sub {
1125
1126
    plan tests => 2;
1127
1128
    $schema->storage->txn_begin;
1129
1130
    my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
1131
    my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
1132
1133
    my $patron = $builder->build_object(
1134
        {
1135
            class => 'Koha::Patrons',
1136
            value => {
1137
                flags      => 2**2,            # only has catalogue permissions
1138
                branchcode => $library_1->id
1139
            }
1140
        }
1141
    );
1142
1143
    my $patron_1 = $builder->build_object(
1144
        { class => 'Koha::Patrons', value => { branchcode => $library_1->id } }
1145
    );
1146
    my $patron_2 = $builder->build_object(
1147
        { class => 'Koha::Patrons', value => { branchcode => $library_2->id } }
1148
    );
1149
1150
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1151
1152
    ok( $patron_1->accessible,  'Has access to the patron' );
1153
    ok( !$patron_2->accessible, 'Does not have access to the patron' );
1154
1155
    $schema->storage->txn_rollback;
1156
};

Return to bug 29523