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

(-)a/Koha/Patrons.pm (+39 lines)
Lines 559-564 sub filter_by_amount_owed { Link Here
559
    return $self->search( $where, $attrs );
559
    return $self->search( $where, $attrs );
560
}
560
}
561
561
562
=head3 filter_by_have_subpermission
563
564
    my $patrons = Koha::Patrons->search->filter_by_have_subpermission('suggestions.suggestions_manage');
565
566
Filter patrons who have a given subpermission
567
568
=cut
569
570
sub filter_by_have_subpermission {
571
    my ($self, $subpermission) = @_;
572
573
    my ($p, $sp) = split '\.', $subpermission;
574
575
    my $perm = Koha::Database->new()->schema()->resultset('Userflag')->find({flag => $p});
576
577
    Koha::Exceptions::ObjectNotFound->throw( sprintf( "Permission %s not found", $p ) )
578
      unless $perm;
579
580
    my $bit = $perm->bit;
581
582
    return $self->search(
583
        {
584
            -and => [
585
                -or => [
586
                    \"me.flags & (1 << $bit)",
587
                    { 'me.flags' => 1 },
588
                    {
589
                        -and => [
590
                            { 'user_permissions.module_bit' => $bit },
591
                            { 'user_permissions.code'       => $sp }
592
                        ]
593
                    }
594
                ]
595
            ]
596
        },
597
        { prefetch => 'user_permissions' }
598
    );
599
}
600
562
=head3 _type
601
=head3 _type
563
602
564
=cut
603
=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 2324-2327 subtest 'filter_by_amount_owed' => sub { Link Here
2324
2324
2325
};
2325
};
2326
2326
2327
subtest 'filter_by_have_subpermission' => sub {
2328
    plan tests => 4;
2329
2330
    $schema->storage->txn_begin;
2331
2332
    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
2333
    my $patron_1 = $builder->build_object(
2334
        {
2335
            class => 'Koha::Patrons',
2336
            value => { flags => 1, branchcode => $library->branchcode }
2337
        }
2338
    );
2339
2340
    my $patron_2 = $builder->build_object( # 4096 = 1 << 12 for suggestions
2341
        {
2342
            class => 'Koha::Patrons',
2343
            value => { flags => 4096, branchcode => $library->branchcode }
2344
        }
2345
    );
2346
2347
    my $patron_3 = $builder->build_object(
2348
        {
2349
            class => 'Koha::Patrons',
2350
            value => { flags => 0, branchcode => $library->branchcode }
2351
        }
2352
    );
2353
    $builder->build(
2354
        {
2355
            source => 'UserPermission',
2356
            value  => {
2357
                borrowernumber => $patron_3->borrowernumber,
2358
                module_bit     => 11,
2359
                code           => 'order_manage',
2360
            },
2361
        }
2362
    );
2363
2364
    is_deeply(
2365
        [
2366
            Koha::Patrons->search( { branchcode => $library->branchcode } )
2367
              ->filter_by_have_subpermission('suggestions.suggestions_manage')
2368
              ->get_column('borrowernumber')
2369
        ],
2370
        [ $patron_1->borrowernumber, $patron_2->borrowernumber ],
2371
        'Superlibrarian and patron with suggestions.suggestions_manage'
2372
    );
2373
2374
    is_deeply(
2375
        [
2376
            Koha::Patrons->search( { branchcode => $library->branchcode } )
2377
              ->filter_by_have_subpermission('acquisition.order_manage')
2378
              ->get_column('borrowernumber')
2379
        ],
2380
        [ $patron_1->borrowernumber, $patron_3->borrowernumber ],
2381
        'Superlibrarian and patron with acquisition.order_manage'
2382
    );
2383
2384
    is_deeply(
2385
        [
2386
            Koha::Patrons->search( { branchcode => $library->branchcode } )
2387
              ->filter_by_have_subpermission('parameters.manage_cities')
2388
              ->get_column('borrowernumber')
2389
        ],
2390
        [ $patron_1->borrowernumber ],
2391
        'Only Superlibrarian is returned'
2392
    );
2393
2394
    throws_ok {
2395
        Koha::Patrons->search( { branchcode => $library->branchcode } )
2396
          ->filter_by_have_subpermission('dont_exist.subperm');
2397
    } 'Koha::Exceptions::ObjectNotFound';
2398
2399
2400
    $schema->storage->txn_rollback;
2401
};
2402
2327
$schema->storage->txn_rollback;
2403
$schema->storage->txn_rollback;
2328
- 

Return to bug 30058