line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::ContextAuth::DB::Permission; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Permission object for the ContextAuth database |
4
|
|
|
|
|
|
|
|
5
|
51
|
|
|
51
|
|
376
|
use Mojo::Base -base, -signatures; |
|
51
|
|
|
|
|
117
|
|
|
51
|
|
|
|
|
782
|
|
6
|
|
|
|
|
|
|
|
7
|
51
|
|
|
51
|
|
9913
|
use Data::UUID; |
|
51
|
|
|
|
|
114
|
|
|
51
|
|
|
|
|
2967
|
|
8
|
51
|
|
|
51
|
|
323
|
use List::Util qw(any); |
|
51
|
|
|
|
|
120
|
|
|
51
|
|
|
|
|
2682
|
|
9
|
51
|
|
|
51
|
|
327
|
use Try::Tiny; |
|
51
|
|
|
|
|
114
|
|
|
51
|
|
|
|
|
2702
|
|
10
|
|
|
|
|
|
|
|
11
|
51
|
|
|
51
|
|
325
|
use feature 'postderef'; |
|
51
|
|
|
|
|
109
|
|
|
51
|
|
|
|
|
1821
|
|
12
|
51
|
|
|
51
|
|
344
|
no warnings 'experimental::postderef'; |
|
51
|
|
|
|
|
151
|
|
|
51
|
|
|
|
|
88104
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has [qw'dbh permission_id permission_name permission_label permission_description resource_id error']; |
15
|
|
|
|
|
|
|
|
16
|
44
|
|
|
44
|
1
|
4796
|
sub load ($self, $id) { |
|
43
|
|
|
|
|
90
|
|
|
43
|
|
|
|
|
83
|
|
|
43
|
|
|
|
|
74
|
|
17
|
43
|
|
|
|
|
228
|
$self->error(''); |
18
|
|
|
|
|
|
|
|
19
|
43
|
100
|
|
|
|
379
|
if ( !$id ) { |
20
|
1
|
|
|
|
|
7
|
$self->error( "Need id" ); |
21
|
1
|
|
|
|
|
7
|
return; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
42
|
|
|
|
|
201
|
my $result = $self->dbh->db->select( |
25
|
|
|
|
|
|
|
corbac_permissions => [qw/permission_id permission_name permission_label permission_description resource_id/], { |
26
|
|
|
|
|
|
|
permission_id => $id, |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
42
|
|
|
|
|
25013
|
my $data = $result->hash; |
31
|
42
|
|
|
|
|
1724
|
$result->finish; |
32
|
|
|
|
|
|
|
|
33
|
42
|
100
|
|
|
|
415
|
return if !$result->rows; |
34
|
|
|
|
|
|
|
|
35
|
35
|
|
|
|
|
382
|
my $permission = __PACKAGE__->new( |
36
|
|
|
|
|
|
|
dbh => $self->dbh, |
37
|
|
|
|
|
|
|
$data->%*, |
38
|
|
|
|
|
|
|
permission_id => $id, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
35
|
|
|
|
|
727
|
return $permission; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
20
|
|
|
20
|
1
|
6689
|
sub add ($self, %params) { |
|
20
|
|
|
|
|
47
|
|
|
20
|
|
|
|
|
79
|
|
|
20
|
|
|
|
|
40
|
|
45
|
20
|
|
|
|
|
105
|
$self->error(''); |
46
|
|
|
|
|
|
|
|
47
|
20
|
100
|
|
38
|
|
289
|
if ( any{ !$params{$_} }qw(permission_name resource_id) ) { |
|
38
|
|
|
|
|
185
|
|
48
|
3
|
|
|
|
|
9
|
$self->error('Need permission_name and resource_id'); |
49
|
3
|
|
|
|
|
22
|
return; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
17
|
100
|
100
|
|
|
220
|
if ( length $params{permission_name} > 255 || length $params{permission_name} < 3 ) { |
53
|
2
|
|
|
|
|
9
|
$self->error( 'Invalid parameter' ); |
54
|
2
|
|
|
|
|
15
|
return; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
15
|
|
|
|
|
3046
|
$params{permission_id} = Data::UUID->new->create_str; |
58
|
|
|
|
|
|
|
|
59
|
15
|
|
|
|
|
15985
|
my $error; |
60
|
|
|
|
|
|
|
try { |
61
|
15
|
|
|
15
|
|
923
|
$self->dbh->db->insert( corbac_permissions => \%params); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
catch { |
64
|
1
|
|
|
1
|
|
1282
|
$self->error( 'Invalid parameter' ); |
65
|
1
|
|
|
|
|
9
|
$error = $_; |
66
|
15
|
|
|
|
|
279
|
}; |
67
|
|
|
|
|
|
|
|
68
|
15
|
100
|
|
|
|
13404
|
return if $error; |
69
|
|
|
|
|
|
|
|
70
|
14
|
|
|
|
|
87
|
my $permission = $self->load( $params{permission_id} ); |
71
|
14
|
|
|
|
|
857
|
return $permission; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
6
|
|
|
6
|
1
|
5439
|
sub delete ($self, $id = $self->permission_id) { |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
19
|
|
|
6
|
|
|
|
|
20
|
|
75
|
6
|
|
|
|
|
21
|
$self->error(''); |
76
|
|
|
|
|
|
|
|
77
|
6
|
100
|
|
|
|
43
|
if ( !$id ) { |
78
|
1
|
|
|
|
|
5
|
$self->error( "Need permission id" ); |
79
|
1
|
|
|
|
|
10
|
return; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
5
|
100
|
|
|
|
15
|
if ( ref $id ) { |
83
|
1
|
|
|
|
|
6
|
$self->error( "Invalid permission id" ); |
84
|
1
|
|
|
|
|
10
|
return; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
4
|
|
|
|
|
8
|
my $error; |
88
|
|
|
|
|
|
|
my $result; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
try { |
91
|
4
|
|
|
4
|
|
180
|
my $tx = $self->dbh->db->begin; |
92
|
|
|
|
|
|
|
|
93
|
4
|
|
|
|
|
514
|
$self->dbh->db->delete( |
94
|
|
|
|
|
|
|
corbac_role_permissions => { permission_id => $id } |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
|
97
|
3
|
|
|
|
|
2096
|
$result = $self->dbh->db->delete( |
98
|
|
|
|
|
|
|
corbac_permissions => { |
99
|
|
|
|
|
|
|
permission_id => $id, |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
); |
102
|
|
|
|
|
|
|
|
103
|
3
|
|
|
|
|
1697
|
$tx->commit; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
catch { |
106
|
1
|
|
|
1
|
|
1000
|
$self->error( "Cannot delete permission: " . $_ ); |
107
|
1
|
|
|
|
|
9
|
$error = 1; |
108
|
4
|
|
|
|
|
32
|
}; |
109
|
|
|
|
|
|
|
|
110
|
4
|
100
|
|
|
|
332
|
return if $error; |
111
|
|
|
|
|
|
|
|
112
|
3
|
|
|
|
|
11
|
return $result->rows; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
9
|
|
|
9
|
1
|
10358
|
sub update ($self, @params) { |
|
9
|
|
|
|
|
20
|
|
|
9
|
|
|
|
|
23
|
|
|
9
|
|
|
|
|
15
|
|
116
|
9
|
|
|
|
|
37
|
$self->error(''); |
117
|
|
|
|
|
|
|
|
118
|
9
|
100
|
|
|
|
78
|
my $id = @params % 2 ? shift @params : $self->permission_id; |
119
|
9
|
|
|
|
|
46
|
my %to_update = @params; |
120
|
|
|
|
|
|
|
|
121
|
9
|
100
|
100
|
|
|
60
|
if ( exists $to_update{permission_name} && ( |
|
|
|
100
|
|
|
|
|
122
|
|
|
|
|
|
|
length $to_update{permission_name} > 255 || |
123
|
|
|
|
|
|
|
length $to_update{permission_name} < 3 |
124
|
|
|
|
|
|
|
)) { |
125
|
3
|
|
|
|
|
11
|
$self->error( 'Invalid parameter' ); |
126
|
3
|
|
|
|
|
41
|
return; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
6
|
|
|
|
|
11
|
delete $to_update{permission_id}; |
130
|
|
|
|
|
|
|
|
131
|
6
|
|
|
|
|
12
|
my $result; |
132
|
|
|
|
|
|
|
my $error; |
133
|
|
|
|
|
|
|
try { |
134
|
6
|
|
|
6
|
|
275
|
$result = $self->dbh->db->update( |
135
|
|
|
|
|
|
|
corbac_permissions => \%to_update, |
136
|
|
|
|
|
|
|
{ permission_id => $id } |
137
|
|
|
|
|
|
|
); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
catch { |
140
|
1
|
|
|
1
|
|
1152
|
$self->error( 'Invalid parameter' ); |
141
|
1
|
|
|
|
|
9
|
$error = $_; |
142
|
6
|
|
|
|
|
46
|
}; |
143
|
|
|
|
|
|
|
|
144
|
6
|
100
|
|
|
|
3452
|
return if $error; |
145
|
|
|
|
|
|
|
|
146
|
5
|
100
|
|
|
|
17
|
if ( !$result->rows ) { |
147
|
1
|
|
|
|
|
13
|
$self->error( 'No permission updated' ); |
148
|
1
|
|
|
|
|
23
|
return; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
4
|
|
|
|
|
44
|
return $self->load( $id ); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
7
|
|
|
7
|
1
|
7295
|
sub search ($self, %params) { |
|
7
|
|
|
|
|
27
|
|
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
16
|
|
155
|
7
|
|
|
|
|
26
|
$self->error(''); |
156
|
|
|
|
|
|
|
|
157
|
7
|
|
|
|
|
46
|
my $error; |
158
|
|
|
|
|
|
|
my @permission_ids; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
try { |
161
|
7
|
|
|
7
|
|
318
|
my $result = $self->dbh->db->select( |
162
|
|
|
|
|
|
|
corbac_permissions => ['permission_id'] => \%params, |
163
|
|
|
|
|
|
|
); |
164
|
|
|
|
|
|
|
|
165
|
6
|
|
|
|
|
4287
|
while ( my $next = $result->hash ) { |
166
|
6
|
|
|
|
|
252
|
push @permission_ids, $next->{permission_id}; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
catch { |
170
|
1
|
|
|
1
|
|
1058
|
$self->error('Cannot search for permissions'); |
171
|
1
|
|
|
|
|
10
|
$error = $_; |
172
|
7
|
|
|
|
|
53
|
}; |
173
|
|
|
|
|
|
|
|
174
|
7
|
100
|
|
|
|
553
|
return if $error; |
175
|
6
|
|
|
|
|
29
|
return @permission_ids; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
11
|
|
|
11
|
1
|
10395
|
sub set_roles ($self, %params) { |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
31
|
|
|
11
|
|
|
|
|
20
|
|
179
|
11
|
|
|
|
|
44
|
$self->error(''); |
180
|
|
|
|
|
|
|
|
181
|
11
|
100
|
|
|
|
88
|
if ( !$params{roles} ) { |
182
|
2
|
|
|
|
|
7
|
$self->error("Need roles"); |
183
|
2
|
|
|
|
|
26
|
return; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
9
|
100
|
|
|
|
31
|
if ( !$self->permission_id ) { |
187
|
1
|
|
|
|
|
9
|
$self->error("Need permission id"); |
188
|
1
|
|
|
|
|
9
|
return; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
8
|
|
|
|
|
45
|
my $count = 0; |
192
|
8
|
|
|
|
|
17
|
my $error; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
try { |
195
|
8
|
|
|
8
|
|
393
|
my $tx = $self->dbh->db->begin; |
196
|
|
|
|
|
|
|
|
197
|
8
|
|
|
|
|
1082
|
$self->dbh->db->delete( |
198
|
|
|
|
|
|
|
corbac_role_permissions => { |
199
|
|
|
|
|
|
|
permission_id => $self->permission_id, |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
); |
202
|
|
|
|
|
|
|
|
203
|
8
|
100
|
|
|
|
4898
|
$count = -1 if !$params{roles}->@*; |
204
|
|
|
|
|
|
|
|
205
|
7
|
|
|
|
|
408
|
for my $role_id ( $params{roles}->@* ) { |
206
|
8
|
|
|
|
|
200
|
my $result = $self->dbh->db->insert( |
207
|
|
|
|
|
|
|
corbac_role_permissions => { |
208
|
|
|
|
|
|
|
role_id => $role_id, |
209
|
|
|
|
|
|
|
permission_id => $self->permission_id, |
210
|
|
|
|
|
|
|
resource_id => $self->resource_id, |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
); |
213
|
|
|
|
|
|
|
|
214
|
8
|
|
|
|
|
5655
|
$count += $result->rows; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
7
|
|
|
|
|
491
|
$tx->commit; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
catch { |
220
|
1
|
|
|
1
|
|
129
|
$self->error( "Transaction error: $_" ); |
221
|
1
|
|
|
|
|
9
|
$error = $_; |
222
|
8
|
|
|
|
|
87
|
}; |
223
|
|
|
|
|
|
|
|
224
|
8
|
100
|
|
|
|
928
|
return if $error; |
225
|
7
|
|
|
|
|
48
|
return $count; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
10
|
|
|
10
|
1
|
7116
|
sub roles ($self) { |
|
10
|
|
|
|
|
24
|
|
|
10
|
|
|
|
|
45
|
|
229
|
10
|
|
|
|
|
40
|
$self->error(''); |
230
|
|
|
|
|
|
|
|
231
|
10
|
100
|
|
|
|
85
|
if ( !$self->permission_id ) { |
232
|
1
|
|
|
|
|
9
|
$self->error("Need permission id"); |
233
|
1
|
|
|
|
|
8
|
return; |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
9
|
100
|
|
|
|
61
|
if ( ref $self->permission_id ) { |
237
|
1
|
|
|
|
|
7
|
$self->error("Invalid permission id"); |
238
|
1
|
|
|
|
|
9
|
return; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
8
|
|
|
|
|
46
|
my @roles; |
242
|
|
|
|
|
|
|
|
243
|
8
|
|
|
|
|
25
|
my $tx = $self->dbh->db->begin; |
244
|
|
|
|
|
|
|
|
245
|
8
|
|
|
|
|
1108
|
my $result = $self->dbh->db->select( |
246
|
|
|
|
|
|
|
corbac_role_permissions => ['role_id'] => { |
247
|
|
|
|
|
|
|
permission_id => $self->permission_id, |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
); |
250
|
|
|
|
|
|
|
|
251
|
8
|
|
|
|
|
5045
|
while ( my $next = $result->hash ) { |
252
|
7
|
|
|
|
|
227
|
push @roles, $next->{role_id}; |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
8
|
|
|
|
|
201
|
$tx->commit; |
256
|
|
|
|
|
|
|
|
257
|
8
|
|
|
|
|
303
|
return @roles; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
1; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=pod |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=encoding UTF-8 |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=head1 NAME |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Mojolicious::Plugin::ContextAuth::DB::Permission - Permission object for the ContextAuth database |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head1 VERSION |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
version 0.01 |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=head1 SYNOPSIS |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
my $db = Mojolicious::Plugin::ContextAuth::DB->new( |
277
|
|
|
|
|
|
|
dsn => 'sqlite:' . $file, |
278
|
|
|
|
|
|
|
); |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
my $permission = Mojolicious::Plugin::ContextAuth::DB::permission->new( |
281
|
|
|
|
|
|
|
dbh => $db->dbh, |
282
|
|
|
|
|
|
|
); |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
my $new_permission = $permission->add( |
285
|
|
|
|
|
|
|
permission_name => 'test', |
286
|
|
|
|
|
|
|
permission_description => 'hallo', |
287
|
|
|
|
|
|
|
); |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
my $updated_permission = $new_permission->update( |
290
|
|
|
|
|
|
|
permission_name => 'ernie', |
291
|
|
|
|
|
|
|
permission_description => 'bert', |
292
|
|
|
|
|
|
|
); |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
# create permission object with data for permission id 1 |
295
|
|
|
|
|
|
|
my $found_permission = $permission->load( 1 ); |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
# delete permission |
298
|
|
|
|
|
|
|
$new_permission->delete; |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=over 4 |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=item * dbh |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=item * permission_name |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=item * permission_description |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=item * permission_id |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=item * error |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=back |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head1 METHODS |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=head2 load |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
# create permission object with data for permission id 1 |
321
|
|
|
|
|
|
|
my $found_permission = $permission->load( 1 ); |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=head2 add |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
my $new_permission = $permission->add( |
326
|
|
|
|
|
|
|
permissionname => 'test', |
327
|
|
|
|
|
|
|
permission_password => 'hallo', |
328
|
|
|
|
|
|
|
); |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=head2 update |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
my $updated_permission = $new_permission->update( |
333
|
|
|
|
|
|
|
permissionname => 'ernie', |
334
|
|
|
|
|
|
|
permission_password => 'bert', |
335
|
|
|
|
|
|
|
); |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=head2 delete |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
$permission->delete; |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
=head2 set_roles |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
=head2 roles |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
=head2 search |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
=head1 AUTHOR |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
Renee Baecker |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
This software is Copyright (c) 2020 by Renee Baecker. |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
This is free software, licensed under: |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=cut |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
__END__ |