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

(-)a/C4/Serials.pm (-12 / +59 lines)
Lines 2849-2866 sub can_edit_subscription { Link Here
2849
    return 0 unless C4::Context->userenv;
2849
    return 0 unless C4::Context->userenv;
2850
    my $flags = C4::Context->userenv->{flags};
2850
    my $flags = C4::Context->userenv->{flags};
2851
    $userid ||= C4::Context->userenv->{'id'};
2851
    $userid ||= C4::Context->userenv->{'id'};
2852
    my $independent_branches = C4::Context->preference('IndependentBranches');
2852
2853
    return 1 unless $independent_branches;
2853
    if ( C4::Context->preference('IndependentBranches') ) {
2854
    if( C4::Context->IsSuperLibrarian()
2854
        return 1
2855
        or C4::Auth::haspermission( $userid, {serials => 'superserials'}),
2855
          if C4::Context->IsSuperLibrarian()
2856
        or C4::Auth::haspermission( $userid, {serials => 'edit_subscription'}),
2856
              or
2857
        or not defined $subscription->{branchcode}
2857
              C4::Auth::haspermission( $userid, { serials => 'superserials' } )
2858
        or $subscription->{branchcode} eq ''
2858
              or (
2859
        or $subscription->{branchcode} eq C4::Context->userenv->{'branch'}
2859
                  C4::Auth::haspermission( $userid,
2860
    ) {
2860
                      { serials => 'edit_subscription' } )
2861
        return 1;
2861
                  and (  not defined $subscription->{branchcode}
2862
    }
2862
                      or $subscription->{branchcode} eq ''
2863
     return 0;
2863
                      or $subscription->{branchcode} eq
2864
                      C4::Context->userenv->{'branch'} )
2865
              );
2866
    }
2867
    else {
2868
        return 1
2869
          if C4::Context->IsSuperLibrarian()
2870
              or
2871
              C4::Auth::haspermission( $userid, { serials => 'superserials' } )
2872
              or C4::Auth::haspermission(
2873
                  $userid, { serials => 'edit_subscription' }
2874
              ),
2875
        ;
2876
    }
2877
    return 0;
2878
}
2879
2880
sub can_show_subscription {
2881
    my ( $subscription, $userid ) = @_;
2882
    return 0 unless C4::Context->userenv;
2883
    my $flags = C4::Context->userenv->{flags};
2884
    $userid ||= C4::Context->userenv->{'id'};
2885
2886
    if ( C4::Context->preference('IndependentBranches') ) {
2887
        return 1
2888
          if C4::Context->IsSuperLibrarian()
2889
              or
2890
              C4::Auth::haspermission( $userid, { serials => 'superserials' } )
2891
              or (
2892
                  C4::Auth::haspermission( $userid,
2893
                      { serials => '*' } )
2894
                  and (  not defined $subscription->{branchcode}
2895
                      or $subscription->{branchcode} eq ''
2896
                      or $subscription->{branchcode} eq
2897
                      C4::Context->userenv->{'branch'} )
2898
              );
2899
    }
2900
    else {
2901
        return 1
2902
          if C4::Context->IsSuperLibrarian()
2903
              or
2904
              C4::Auth::haspermission( $userid, { serials => 'superserials' } )
2905
              or C4::Auth::haspermission(
2906
                  $userid, { serials => '*' }
2907
              ),
2908
        ;
2909
    }
2910
    return 0;
2864
}
2911
}
2865
2912
2866
1;
2913
1;
(-)a/t/db_dependent/Serials_2.t (-24 / +200 lines)
Lines 1-15 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
use Modern::Perl;
2
use Modern::Perl;
3
3
4
use Test::More tests => 5;
4
use Test::More;# tests => 5; #FIXME uncomment me
5
5
6
use MARC::Record;
6
use MARC::Record;
7
7
8
use C4::Biblio qw( AddBiblio );
8
use C4::Biblio qw( AddBiblio );
9
use C4::Context;
9
use C4::Members qw( AddMember );
10
use t::lib::Mocks;
10
use_ok('C4::Serials');
11
use_ok('C4::Serials');
11
use_ok('C4::Budgets');
12
use_ok('C4::Budgets');
12
13
14
# Mock userenv
15
local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
16
my $userenv;
17
*C4::Context::userenv = \&Mock_userenv;
18
13
my $dbh = C4::Context->dbh;
19
my $dbh = C4::Context->dbh;
14
$dbh->{AutoCommit} = 0;
20
$dbh->{AutoCommit} = 0;
15
$dbh->{RaiseError} = 1;
21
$dbh->{RaiseError} = 1;
Lines 24-33 $record->append_fields( Link Here
24
);
30
);
25
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
31
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
26
32
33
my $my_branch = 'CPL';
34
my $another_branch = 'MPL';
27
my $budgetid;
35
my $budgetid;
28
my $bpid = AddBudgetPeriod({
36
my $bpid = AddBudgetPeriod({
29
    budget_period_startdate => '01-01-2015',
37
    budget_period_startdate => '2015-01-01',
30
    budget_period_enddate   => '12-31-2015',
38
    budget_period_enddate   => '2015-12-31',
31
    budget_description      => "budget desc"
39
    budget_description      => "budget desc"
32
});
40
});
33
41
Lines 41-78 my $budget_id = AddBudget({ Link Here
41
    budget_period_id   => $bpid
49
    budget_period_id   => $bpid
42
});
50
});
43
51
44
my $subscriptionid = NewSubscription(
52
my $subscriptionid_from_my_branch = NewSubscription(
45
    undef,      "",     undef, undef, $budget_id, $biblionumber,
53
    undef,      $my_branch,     undef, undef, $budget_id, $biblionumber,
54
    '2013-01-01', undef, undef, undef,  undef,
55
    undef,      undef,  undef, undef, undef, undef,
56
    1,          "notes",undef, '2013-01-01', undef, undef,
57
    undef,       undef,  0,    "intnotes",  0,
58
    undef, undef, 0,          undef,         '2013-12-31', 0
59
);
60
die unless $subscriptionid_from_my_branch;
61
62
my $subscriptionid_from_another_branch = NewSubscription(
63
    undef,      $another_branch,     undef, undef, $budget_id, $biblionumber,
46
    '2013-01-01', undef, undef, undef,  undef,
64
    '2013-01-01', undef, undef, undef,  undef,
47
    undef,      undef,  undef, undef, undef, undef,
65
    undef,      undef,  undef, undef, undef, undef,
48
    1,          "notes",undef, '2013-01-01', undef, undef,
66
    1,          "notes",undef, '2013-01-01', undef, undef,
49
    undef,       undef,  0,    "intnotes",  0,
67
    undef,       undef,  0,    "intnotes",  0,
50
    undef, undef, 0,          undef,         '2013-12-31', 0
68
    undef, undef, 0,          undef,         '2013-12-31', 0
51
);
69
);
52
die unless $subscriptionid;
53
70
54
71
55
my $subscription = GetSubscription( $subscriptionid );
72
my $subscription_from_my_branch = GetSubscription( $subscriptionid_from_my_branch );
56
is( C4::Serials::can_edit_subscription($subscription), 0, "cannot edit a subscription without userenv set");
73
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0, "cannot edit a subscription without userenv set");
57
74
58
my @USERENV = (
75
my $userid = 'my_userid';
59
    1,
76
my $borrowernumber = C4::Members::AddMember(
60
    'test',
77
    firstname =>  'my fistname',
61
    'MASTERTEST',
78
    surname => 'my surname',
62
    'Test',
79
    categorycode => 'S',
63
    'Test',
80
    branchcode => $my_branch,
64
    't',
81
    userid => $userid,
65
    0,
66
    0,
67
);
82
);
68
83
69
C4::Context->_new_userenv ('DUMMY_SESSION_ID');
84
$userenv = { flags => 1, id => $borrowernumber, branch => '' }; # FIXME Not sure about this test
70
C4::Context->set_userenv ( @USERENV );
71
85
72
# Can edit a subscription
86
# Can edit a subscription
73
my $userenv = C4::Context->userenv;
74
87
75
is( C4::Serials::can_edit_subscription($subscription), 1, "User can edit a subscription with an empty branchcode");
88
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
76
#TODO add UT when C4::Auth->set_permissions (or setuserflags) will exist.
89
90
my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
91
92
$userenv->{id} = $userid;
93
$userenv->{branch} = $my_branch;
94
95
# Branches are independent
96
t::lib::Mocks::mock_preference( "IndependentBranches", 1 );
97
set_flags( 'superlibrarian', $borrowernumber );
98
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
99
"With IndependentBranches, superlibrarian can edit a subscription from his branch"
100
);
101
is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
102
"With IndependentBranches, superlibrarian can edit a subscription from another branch"
103
);
104
is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
105
"With IndependentBranches, superlibrarian can show a subscription from his branch"
106
);
107
is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
108
"With IndependentBranches, superlibrarian can show a subscription from another branch"
109
);
110
111
set_flags( 'superserials', $borrowernumber );
112
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
113
"With IndependentBranches, superserials can edit a subscription from his branch"
114
);
115
is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
116
"With IndependentBranches, superserials can edit a subscription from another branch"
117
);
118
is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
119
"With IndependentBranches, superserials can show a subscription from his branch"
120
);
121
is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
122
"With IndependentBranches, superserials can show a subscription from another branch"
123
);
124
125
126
set_flags( 'edit_subscription', $borrowernumber );
127
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
128
"With IndependentBranches, edit_subscription can edit a subscription from his branch"
129
);
130
is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
131
"With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
132
);
133
is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
134
"With IndependentBranches, show_subscription can show a subscription from his branch"
135
);
136
is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
137
"With IndependentBranches, show_subscription cannot show a subscription from another branch"
138
);
139
140
set_flags( 'renew_subscription', $borrowernumber );
141
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
142
"With IndependentBranches, renew_subscription cannot edit a subscription from his branch"
143
);
144
is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
145
"With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
146
);
147
is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
148
"With IndependentBranches, renew_subscription can show a subscription from his branch"
149
);
150
is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
151
"With IndependentBranches, renew_subscription cannot show a subscription from another branch"
152
);
153
154
155
# Branches are not independent
156
t::lib::Mocks::mock_preference( "IndependentBranches", 0 );
157
set_flags( 'superlibrarian', $borrowernumber );
158
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
159
"Without IndependentBranches, superlibrarian can edit a subscription from his branch"
160
);
161
is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
162
"Without IndependentBranches, superlibrarian can edit a subscription from another branch"
163
);
164
is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
165
"Without IndependentBranches, superlibrarian can show a subscription from his branch"
166
);
167
is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
168
"Without IndependentBranches, superlibrarian can show a subscription from another branch"
169
);
170
171
set_flags( 'superserials', $borrowernumber );
172
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
173
"Without IndependentBranches, superserials can edit a subscription from his branch"
174
);
175
is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
176
"Without IndependentBranches, superserials can edit a subscription from another branch"
177
);
178
is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
179
"Without IndependentBranches, superserials can show a subscription from his branch"
180
);
181
is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
182
"Without IndependentBranches, superserials can show a subscription from another branch"
183
);
184
185
set_flags( 'edit_subscription', $borrowernumber );
186
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
187
"Without IndependentBranches, edit_subscription can edit a subscription from his branch"
188
);
189
is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
190
"Without IndependentBranches, edit_subscription can edit a subscription from another branch"
191
);
192
is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
193
"Without IndependentBranches, show_subscription can show a subscription from his branch"
194
);
195
is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
196
"Without IndependentBranches, show_subscription can show a subscription from another branch"
197
);
198
199
set_flags( 'renew_subscription', $borrowernumber );
200
is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
201
"Without IndependentBranches, renew_subscription cannot edit a subscription from his branch"
202
);
203
is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
204
"Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
205
);
206
is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
207
"Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
208
);
209
is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
210
"Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
211
);
212
213
77
214
78
$dbh->rollback;
215
$dbh->rollback;
79
- 
216
217
done_testing;
218
219
# C4::Context->userenv
220
sub Mock_userenv {
221
    return $userenv;
222
}
223
224
sub set_flags {
225
    my ( $flags, $borrowernumber ) = @_;
226
    my $superlibrarian_flags = 1;
227
    if ( $flags eq 'superlibrarian' ) {
228
        $dbh->do(
229
            q|
230
            UPDATE borrowers SET flags=? WHERE borrowernumber=?
231
        |, {}, $superlibrarian_flags, $borrowernumber
232
        );
233
        $userenv->{flags} = $superlibrarian_flags;
234
    }
235
    else {
236
        $dbh->do(
237
            q|
238
            UPDATE borrowers SET flags=? WHERE borrowernumber=?
239
        |, {}, 0, $borrowernumber
240
        );
241
        $userenv->{flags} = 0;
242
        my ( $module_bit, $code ) = ( '15', $flags );
243
        $dbh->do(
244
            q|
245
            DELETE FROM user_permissions where borrowernumber=?
246
        |, {}, $borrowernumber
247
        );
248
249
        $dbh->do(
250
            q|
251
            INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
252
        |, {}, $borrowernumber, $module_bit, $code
253
        );
254
    }
255
}

Return to bug 12080