line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::ContextAuth::DB::Resource; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Resource object for the ContextAuth database |
4
|
|
|
|
|
|
|
|
5
|
51
|
|
|
51
|
|
397
|
use Mojo::Base -base, -signatures; |
|
51
|
|
|
|
|
127
|
|
|
51
|
|
|
|
|
364
|
|
6
|
|
|
|
|
|
|
|
7
|
51
|
|
|
51
|
|
9978
|
use Data::UUID; |
|
51
|
|
|
|
|
187
|
|
|
51
|
|
|
|
|
3414
|
|
8
|
51
|
|
|
51
|
|
355
|
use List::Util qw(any); |
|
51
|
|
|
|
|
120
|
|
|
51
|
|
|
|
|
2889
|
|
9
|
51
|
|
|
51
|
|
336
|
use Try::Tiny; |
|
51
|
|
|
|
|
138
|
|
|
51
|
|
|
|
|
2848
|
|
10
|
|
|
|
|
|
|
|
11
|
51
|
|
|
51
|
|
334
|
use feature 'postderef'; |
|
51
|
|
|
|
|
124
|
|
|
51
|
|
|
|
|
1872
|
|
12
|
51
|
|
|
51
|
|
314
|
no warnings 'experimental::postderef'; |
|
51
|
|
|
|
|
155
|
|
|
51
|
|
|
|
|
63187
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has [qw'dbh resource_id resource_name resource_description resource_label error']; |
15
|
|
|
|
|
|
|
|
16
|
36
|
|
|
36
|
1
|
4632
|
sub load ($self, $id) { |
|
35
|
|
|
|
|
87
|
|
|
35
|
|
|
|
|
103
|
|
|
35
|
|
|
|
|
67
|
|
17
|
35
|
|
|
|
|
154
|
$self->error(''); |
18
|
|
|
|
|
|
|
|
19
|
35
|
100
|
|
|
|
294
|
if ( !$id ) { |
20
|
2
|
|
|
|
|
7
|
$self->error( "Need id" ); |
21
|
2
|
|
|
|
|
12
|
return; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
33
|
|
|
|
|
122
|
my $result = $self->dbh->db->select( |
25
|
|
|
|
|
|
|
corbac_resources => [qw/resource_id resource_name resource_description resource_label/], { |
26
|
|
|
|
|
|
|
resource_id => $id, |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
33
|
|
|
|
|
27874
|
my $data = $result->hash; |
31
|
33
|
|
|
|
|
1615
|
$result->finish; |
32
|
|
|
|
|
|
|
|
33
|
33
|
100
|
|
|
|
372
|
return if !$result->rows; |
34
|
|
|
|
|
|
|
|
35
|
28
|
|
|
|
|
349
|
my $resource = __PACKAGE__->new( |
36
|
|
|
|
|
|
|
dbh => $self->dbh, |
37
|
|
|
|
|
|
|
$data->%*, |
38
|
|
|
|
|
|
|
resource_id => $id, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
28
|
|
|
|
|
571
|
return $resource; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
26
|
|
|
26
|
1
|
12726
|
sub add ($self, %params) { |
|
26
|
|
|
|
|
67
|
|
|
26
|
|
|
|
|
93
|
|
|
26
|
|
|
|
|
57
|
|
45
|
26
|
|
|
|
|
163
|
$self->error(''); |
46
|
|
|
|
|
|
|
|
47
|
26
|
100
|
|
|
|
285
|
if ( !$params{resource_name} ) { |
48
|
2
|
|
|
|
|
9
|
$self->error('Need resource_name'); |
49
|
2
|
|
|
|
|
14
|
return; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
24
|
|
|
|
|
85
|
for my $key ( qw/resource_name resource_description/ ) { |
53
|
47
|
100
|
100
|
|
|
334
|
if ( exists $params{$key} && length $params{$key} > 255 ) { |
54
|
1
|
|
|
|
|
5
|
$self->error( 'Invalid parameter' ); |
55
|
1
|
|
|
|
|
8
|
return; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
23
|
100
|
|
|
|
221
|
if ( length $params{resource_name} < 3 ) { |
60
|
1
|
|
|
|
|
4
|
$self->error( 'Invalid parameter' ); |
61
|
1
|
|
|
|
|
7
|
return; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
22
|
|
|
|
|
5662
|
$params{resource_id} = Data::UUID->new->create_str; |
65
|
|
|
|
|
|
|
|
66
|
22
|
|
|
|
|
44481
|
my $error; |
67
|
|
|
|
|
|
|
try { |
68
|
22
|
|
|
22
|
|
1610
|
$self->dbh->db->insert( corbac_resources => \%params); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
catch { |
71
|
2
|
|
|
2
|
|
2179
|
$self->error( 'Invalid parameter' ); |
72
|
2
|
|
|
|
|
19
|
$error = $_; |
73
|
22
|
|
|
|
|
455
|
}; |
74
|
|
|
|
|
|
|
|
75
|
22
|
100
|
|
|
|
106367
|
return if $error; |
76
|
|
|
|
|
|
|
|
77
|
20
|
|
|
|
|
144
|
my $resource = $self->load( $params{resource_id} ); |
78
|
20
|
|
|
|
|
1253
|
return $resource; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
6
|
|
|
6
|
1
|
5156
|
sub delete ($self, $id = $self->resource_id) { |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
16
|
|
|
6
|
|
|
|
|
19
|
|
82
|
6
|
|
|
|
|
18
|
$self->error(''); |
83
|
|
|
|
|
|
|
|
84
|
6
|
100
|
|
|
|
37
|
if ( !$id ) { |
85
|
1
|
|
|
|
|
4
|
$self->error( "Need resource id" ); |
86
|
1
|
|
|
|
|
8
|
return; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
5
|
100
|
|
|
|
15
|
if ( ref $id ) { |
90
|
1
|
|
|
|
|
5
|
$self->error( "Invalid resource id" ); |
91
|
1
|
|
|
|
|
8
|
return; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
4
|
|
|
|
|
9
|
my $error; |
95
|
|
|
|
|
|
|
my $result; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
try { |
98
|
4
|
|
|
4
|
|
187
|
my $tx = $self->dbh->db->begin; |
99
|
|
|
|
|
|
|
|
100
|
4
|
|
|
|
|
457
|
$self->dbh->db->delete( |
101
|
|
|
|
|
|
|
corbac_role_permissions => { resource_id => $id }, |
102
|
|
|
|
|
|
|
); |
103
|
|
|
|
|
|
|
|
104
|
3
|
|
|
|
|
1430
|
$self->dbh->db->delete( |
105
|
|
|
|
|
|
|
corbac_permissions => { resource_id => $id } |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
|
108
|
3
|
|
|
|
|
1272
|
$result = $self->dbh->db->delete( |
109
|
|
|
|
|
|
|
corbac_resources => { |
110
|
|
|
|
|
|
|
resource_id => $id, |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
); |
113
|
|
|
|
|
|
|
|
114
|
3
|
|
|
|
|
1379
|
$tx->commit; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
catch { |
117
|
1
|
|
|
1
|
|
1088
|
$self->error( "Cannot delete resource: " . $_ ); |
118
|
1
|
|
|
|
|
8
|
$error = 1; |
119
|
4
|
|
|
|
|
33
|
}; |
120
|
|
|
|
|
|
|
|
121
|
4
|
100
|
|
|
|
395
|
return if $error; |
122
|
3
|
|
|
|
|
11
|
return $result->rows; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
7
|
|
|
7
|
1
|
7682
|
sub update ($self, @params) { |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
19
|
|
|
7
|
|
|
|
|
13
|
|
126
|
7
|
|
|
|
|
26
|
$self->error(''); |
127
|
|
|
|
|
|
|
|
128
|
7
|
100
|
|
|
|
64
|
my $id = @params % 2 ? shift @params : $self->resource_id; |
129
|
7
|
|
|
|
|
40
|
my %to_update = @params; |
130
|
|
|
|
|
|
|
|
131
|
7
|
100
|
100
|
|
|
42
|
if ( exists $to_update{resource_name} && ( |
|
|
|
100
|
|
|
|
|
132
|
|
|
|
|
|
|
length $to_update{resource_name} > 255 || |
133
|
|
|
|
|
|
|
length $to_update{resource_name} < 3 |
134
|
|
|
|
|
|
|
) ) { |
135
|
3
|
|
|
|
|
10
|
$self->error( 'Invalid parameter' ); |
136
|
3
|
|
|
|
|
60
|
return; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
4
|
|
|
|
|
8
|
delete $to_update{resource_id}; |
140
|
|
|
|
|
|
|
|
141
|
4
|
|
|
|
|
8
|
my $result; |
142
|
|
|
|
|
|
|
my $error; |
143
|
|
|
|
|
|
|
try { |
144
|
4
|
|
|
4
|
|
187
|
$result = $self->dbh->db->update( |
145
|
|
|
|
|
|
|
corbac_resources => \%to_update, |
146
|
|
|
|
|
|
|
{ resource_id => $id } |
147
|
|
|
|
|
|
|
); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
catch { |
150
|
1
|
|
|
1
|
|
1169
|
$self->error( 'Invalid parameter' ); |
151
|
1
|
|
|
|
|
8
|
$error = $_; |
152
|
4
|
|
|
|
|
31
|
}; |
153
|
|
|
|
|
|
|
|
154
|
4
|
100
|
|
|
|
2400
|
return if $error; |
155
|
3
|
|
|
|
|
10
|
return $self->load( $id ); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
7
|
|
|
7
|
1
|
7264
|
sub search ($self, %params) { |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
11
|
|
159
|
7
|
|
|
|
|
26
|
$self->error(''); |
160
|
|
|
|
|
|
|
|
161
|
7
|
|
|
|
|
48
|
my $error; |
162
|
|
|
|
|
|
|
my @resource_ids; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
try { |
165
|
7
|
|
|
7
|
|
307
|
my $result = $self->dbh->db->select( |
166
|
|
|
|
|
|
|
corbac_resources => ['resource_id'] => \%params, |
167
|
|
|
|
|
|
|
); |
168
|
|
|
|
|
|
|
|
169
|
6
|
|
|
|
|
4266
|
while ( my $next = $result->hash ) { |
170
|
6
|
|
|
|
|
270
|
push @resource_ids, $next->{resource_id}; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
catch { |
174
|
1
|
|
|
1
|
|
1024
|
$self->error('Cannot search for resources'); |
175
|
1
|
|
|
|
|
8
|
$error = $_; |
176
|
7
|
|
|
|
|
55
|
}; |
177
|
|
|
|
|
|
|
|
178
|
7
|
100
|
|
|
|
609
|
return if $error; |
179
|
6
|
|
|
|
|
29
|
return @resource_ids; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
1; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=pod |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=encoding UTF-8 |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 NAME |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Mojolicious::Plugin::ContextAuth::DB::Resource - Resource object for the ContextAuth database |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 VERSION |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
version 0.01 |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 SYNOPSIS |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
my $db = Mojolicious::Plugin::ContextAuth::DB->new( |
199
|
|
|
|
|
|
|
dsn => 'sqlite:' . $file, |
200
|
|
|
|
|
|
|
); |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
my $resource = Mojolicious::Plugin::ContextAuth::DB::resource->new( |
203
|
|
|
|
|
|
|
dbh => $db->dbh, |
204
|
|
|
|
|
|
|
); |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
my $new_resource = $resource->add( |
207
|
|
|
|
|
|
|
resource_name => 'test', |
208
|
|
|
|
|
|
|
resource_description => 'hallo', |
209
|
|
|
|
|
|
|
); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
my $updated_resource = $new_resource->update( |
212
|
|
|
|
|
|
|
resource_name => 'ernie', |
213
|
|
|
|
|
|
|
resource_description => 'bert', |
214
|
|
|
|
|
|
|
); |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# create resource object with data for resource id 1 |
217
|
|
|
|
|
|
|
my $found_resource = $resource->load( 1 ); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# delete resource |
220
|
|
|
|
|
|
|
$new_resource->delete; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=over 4 |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=item * dbh |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item * resource_name |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=item * resource_description |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item * resource_id |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=item * error |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=back |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 METHODS |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head2 load |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
# create resource object with data for resource id 1 |
243
|
|
|
|
|
|
|
my $found_resource = $resource->load( 1 ); |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 add |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
my $new_resource = $resource->add( |
248
|
|
|
|
|
|
|
resourcename => 'test', |
249
|
|
|
|
|
|
|
resource_password => 'hallo', |
250
|
|
|
|
|
|
|
); |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head2 update |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
my $updated_resource = $new_resource->update( |
255
|
|
|
|
|
|
|
resourcename => 'ernie', |
256
|
|
|
|
|
|
|
resource_password => 'bert', |
257
|
|
|
|
|
|
|
); |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=head2 delete |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
$resource->delete; |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=head2 search |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head1 AUTHOR |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
Renee Baecker |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
This software is Copyright (c) 2020 by Renee Baecker. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
This is free software, licensed under: |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=cut |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
__END__ |