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

(-)a/Koha/Patrons.pm (+39 lines)
Lines 520-525 sub filter_by_amount_owed { Link Here
520
    return $self->search( $where, $attrs );
520
    return $self->search( $where, $attrs );
521
}
521
}
522
522
523
=head3 filter_by_have_subpermission
524
525
    my $patrons = Koha::Patrons->search->filter_by_have_subpermission('suggestions.suggestions_manage');
526
527
Filter patrons who have a given subpermission
528
529
=cut
530
531
sub filter_by_have_subpermission {
532
    my ($self, $subpermission) = @_;
533
534
    my ($p, $sp) = split '\.', $subpermission;
535
536
    my $perm = Koha::Database->new()->schema()->resultset('Userflag')->find({flag => $p});
537
538
    Koha::Exceptions::ObjectNotFound->throw( sprintf( "Permission %s not found", $p ) )
539
      unless $perm;
540
541
    my $bit = $perm->bit;
542
543
    return $self->search(
544
        {
545
            -and => [
546
                -or => [
547
                    \"me.flags & (1 << $bit)",
548
                    { 'me.flags' => 1 },
549
                    {
550
                        -and => [
551
                            { 'user_permissions.module_bit' => $bit },
552
                            { 'user_permissions.code'       => $sp }
553
                        ]
554
                    }
555
                ]
556
            ]
557
        },
558
        { prefetch => 'user_permissions' }
559
    );
560
}
561
523
=head3 _type
562
=head3 _type
524
563
525
=cut
564
=cut
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +77 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 43;
22
use Test::More tests => 44;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 2346-2349 subtest 'filter_by_amount_owed' => sub { Link Here
2346
2346
2347
};
2347
};
2348
2348
2349
subtest 'filter_by_have_subpermission' => sub {
2350
    plan tests => 4;
2351
2352
    $schema->storage->txn_begin;
2353
2354
    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
2355
    my $patron_1 = $builder->build_object(
2356
        {
2357
            class => 'Koha::Patrons',
2358
            value => { flags => 1, branchcode => $library->branchcode }
2359
        }
2360
    );
2361
2362
    my $patron_2 = $builder->build_object( # 4096 = 1 << 12 for suggestions
2363
        {
2364
            class => 'Koha::Patrons',
2365
            value => { flags => 4096, branchcode => $library->branchcode }
2366
        }
2367
    );
2368
2369
    my $patron_3 = $builder->build_object(
2370
        {
2371
            class => 'Koha::Patrons',
2372
            value => { flags => 0, branchcode => $library->branchcode }
2373
        }
2374
    );
2375
    $builder->build(
2376
        {
2377
            source => 'UserPermission',
2378
            value  => {
2379
                borrowernumber => $patron_3->borrowernumber,
2380
                module_bit     => 11,
2381
                code           => 'order_manage',
2382
            },
2383
        }
2384
    );
2385
2386
    is_deeply(
2387
        [
2388
            Koha::Patrons->search( { branchcode => $library->branchcode } )
2389
              ->filter_by_have_subpermission('suggestions.suggestions_manage')
2390
              ->get_column('borrowernumber')
2391
        ],
2392
        [ $patron_1->borrowernumber, $patron_2->borrowernumber ],
2393
        'Superlibrarian and patron with suggestions.suggestions_manage'
2394
    );
2395
2396
    is_deeply(
2397
        [
2398
            Koha::Patrons->search( { branchcode => $library->branchcode } )
2399
              ->filter_by_have_subpermission('acquisition.order_manage')
2400
              ->get_column('borrowernumber')
2401
        ],
2402
        [ $patron_1->borrowernumber, $patron_3->borrowernumber ],
2403
        'Superlibrarian and patron with acquisition.order_manage'
2404
    );
2405
2406
    is_deeply(
2407
        [
2408
            Koha::Patrons->search( { branchcode => $library->branchcode } )
2409
              ->filter_by_have_subpermission('parameters.manage_cities')
2410
              ->get_column('borrowernumber')
2411
        ],
2412
        [ $patron_1->borrowernumber ],
2413
        'Only Superlibrarian is returned'
2414
    );
2415
2416
    throws_ok {
2417
        Koha::Patrons->search( { branchcode => $library->branchcode } )
2418
          ->filter_by_have_subpermission('dont_exist.subperm');
2419
    } 'Koha::Exceptions::ObjectNotFound';
2420
2421
2422
    $schema->storage->txn_rollback;
2423
};
2424
2349
$schema->storage->txn_rollback;
2425
$schema->storage->txn_rollback;
2350
- 

Return to bug 30058