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 2358-2361 subtest 'filter_by_amount_owed' => sub { Link Here
2358
2358
2359
};
2359
};
2360
2360
2361
subtest 'filter_by_have_subpermission' => sub {
2362
    plan tests => 4;
2363
2364
    $schema->storage->txn_begin;
2365
2366
    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
2367
    my $patron_1 = $builder->build_object(
2368
        {
2369
            class => 'Koha::Patrons',
2370
            value => { flags => 1, branchcode => $library->branchcode }
2371
        }
2372
    );
2373
2374
    my $patron_2 = $builder->build_object( # 4096 = 1 << 12 for suggestions
2375
        {
2376
            class => 'Koha::Patrons',
2377
            value => { flags => 4096, branchcode => $library->branchcode }
2378
        }
2379
    );
2380
2381
    my $patron_3 = $builder->build_object(
2382
        {
2383
            class => 'Koha::Patrons',
2384
            value => { flags => 0, branchcode => $library->branchcode }
2385
        }
2386
    );
2387
    $builder->build(
2388
        {
2389
            source => 'UserPermission',
2390
            value  => {
2391
                borrowernumber => $patron_3->borrowernumber,
2392
                module_bit     => 11,
2393
                code           => 'order_manage',
2394
            },
2395
        }
2396
    );
2397
2398
    is_deeply(
2399
        [
2400
            Koha::Patrons->search( { branchcode => $library->branchcode } )
2401
              ->filter_by_have_subpermission('suggestions.suggestions_manage')
2402
              ->get_column('borrowernumber')
2403
        ],
2404
        [ $patron_1->borrowernumber, $patron_2->borrowernumber ],
2405
        'Superlibrarian and patron with suggestions.suggestions_manage'
2406
    );
2407
2408
    is_deeply(
2409
        [
2410
            Koha::Patrons->search( { branchcode => $library->branchcode } )
2411
              ->filter_by_have_subpermission('acquisition.order_manage')
2412
              ->get_column('borrowernumber')
2413
        ],
2414
        [ $patron_1->borrowernumber, $patron_3->borrowernumber ],
2415
        'Superlibrarian and patron with acquisition.order_manage'
2416
    );
2417
2418
    is_deeply(
2419
        [
2420
            Koha::Patrons->search( { branchcode => $library->branchcode } )
2421
              ->filter_by_have_subpermission('parameters.manage_cities')
2422
              ->get_column('borrowernumber')
2423
        ],
2424
        [ $patron_1->borrowernumber ],
2425
        'Only Superlibrarian is returned'
2426
    );
2427
2428
    throws_ok {
2429
        Koha::Patrons->search( { branchcode => $library->branchcode } )
2430
          ->filter_by_have_subpermission('dont_exist.subperm');
2431
    } 'Koha::Exceptions::ObjectNotFound';
2432
2433
2434
    $schema->storage->txn_rollback;
2435
};
2436
2361
$schema->storage->txn_rollback;
2437
$schema->storage->txn_rollback;
2362
- 

Return to bug 30058