3 # Tests for C4::Auth::haspermission
5 # This file is part of Koha.
7 # Copyright 2016 Rijksmuseum
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use Test::More tests => 13;
26 use t::lib::TestBuilder;
27 use C4::Auth qw(haspermission);
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
32 # Adding two borrowers and granular permissions for the second borrower
33 my $builder = t::lib::TestBuilder->new();
34 my $borr1 = $builder->build({
37 surname => 'Superlib',
41 my $borr2 = $builder->build({
45 flags => 2 + 4 + 2**11, # circulate, catalogue, acquisition
49 source => 'UserPermission',
51 borrowernumber => $borr2->{borrowernumber},
52 module_bit => 13, # tools
53 code => 'upload_local_cover_images',
57 source => 'UserPermission',
59 borrowernumber => $borr2->{borrowernumber},
60 module_bit => 13, # tools
61 code => 'batch_upload_patron_images',
65 # Check top level permission for superlibrarian
66 my $r = haspermission( $borr1->{userid},
67 { circulate => 1, editcatalogue => 1 } );
68 is( ref($r), 'HASH', 'Superlibrarian/circulate' );
70 # Check specific top level permission(s) for borr2
71 $r = haspermission( $borr2->{userid},
72 { circulate => 1, catalogue => 1 } );
73 is( ref($r), 'HASH', 'Borrower2/circulate' );
74 $r = haspermission( $borr2->{userid}, { updatecharges => 1 } );
75 is( $r, 0, 'Borrower2/updatecharges should fail' );
77 # Check granular permission with 1: means all subpermissions
78 $r = haspermission( $borr1->{userid}, { tools => 1 } );
79 is( ref($r), 'HASH', 'Superlibrarian/tools granular all' );
80 $r = haspermission( $borr2->{userid}, { tools => 1 } );
81 is( $r, 0, 'Borrower2/tools granular all should fail' );
83 # Check granular permission with *: means at least one subpermission
84 $r = haspermission( $borr1->{userid}, { tools => '*' } );
85 is( ref($r), 'HASH', 'Superlibrarian/tools granular *' );
86 $r = haspermission( $borr2->{userid}, { acquisition => '*' } );
87 is( ref($r), 'HASH', 'Borrower2/acq granular *' );
88 $r = haspermission( $borr2->{userid}, { tools => '*' } );
89 is( ref($r), 'HASH', 'Borrower2/tools granular *' );
90 $r = haspermission( $borr2->{userid}, { serials => '*' } );
91 is( $r, 0, 'Borrower2/serials granular * should fail' );
93 # Check granular permission with one or more specific subperms
94 $r = haspermission( $borr1->{userid}, { tools => 'edit_news' } );
95 is( ref($r), 'HASH', 'Superlibrarian/tools edit_news' );
96 $r = haspermission( $borr2->{userid}, { acquisition => 'budget_manage' } );
97 is( ref($r), 'HASH', 'Borrower2/acq budget_manage' );
98 $r = haspermission( $borr2->{userid},
99 { acquisition => 'budget_manage', tools => 'edit_news' } );
100 is( $r, 0, 'Borrower2/two granular should fail' );
101 $r = haspermission( $borr2->{userid}, {
102 tools => 'upload_local_cover_images',
103 tools => 'batch_upload_patron_images',
105 is( ref($r), 'HASH', 'Borrower2/tools granular two upload subperms' );
108 $schema->storage->txn_rollback;