Preferences/RolesFor/ResultSet/PrfOwner.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Preferences/RolesFor/Result/PrfOwner.pm ↑ Summary Preferences.pm »
1  
2 package OpusVL::Preferences::RolesFor::ResultSet::PrfOwner;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use OpusVL::FB11::Hive;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 sub _schema {
12 state $schema = OpusVL::FB11::Hive
13 ->fancy_hat('preferences')
14 ->schema;
15 }
16  
17 sub prf_get_default
18 {
19 my $self = shift;
20 my $name = shift;
21  
22 my $defaults = $self->prf_defaults;
23  
24 return
25 unless defined $defaults;
26  
27 my $def = $defaults->find ({ name => $name });
28  
29 return
30 unless defined $def;
31  
32 return $def->default_value;
33 }
34  
35 sub prf_set_default
36 {
37 my $self = shift;
38 my $name = shift;
39 my $value = shift;
40
41 $self->setup_owner_type;
42 $self->prf_defaults->update_or_create
43 ({
44 name => $name,
45 default_value => $value
46 });
47 }
48  
49 sub setup_owner_type
50 {
51 my $self = shift;
52 my $source = $self->result_source;
53  
54 return _schema->resultset ('PrfOwnerType')->setup_from_source ($source);
55 }
56  
57 sub get_owner_type
58 {
59 my $self = shift;
60 my $source = $self->result_source;
61  
62 return _schema->resultset ('PrfOwnerType')->get_from_source ($source);
63 }
64  
65 sub prf_defaults
66 {
67 my $self = shift;
68 my $type = $self->setup_owner_type; # we always want a result here
69  
70 return $type->prf_defaults;
71 }
72  
73 sub prf_preferences
74 {
75 my $self = shift;
76 my $owner_subquery = $self->_hacky_prf_owner_subquery;
77 return _schema->resultset('PrfPreference')->search({
78 prf_owner_id => {
79 -in => $owner_subquery->get_column('prf_owner_id')->as_query
80 }
81 });
82 }
83  
84 sub with_fields
85 {
86 my ($self, $args) = @_;
87  
88 my @params;
89 my @joins;
90 my $x = 1;
91 # well this sucks, we need to figure out if these are encrypted fields.
92 # we can't do this entirely at the DB layer.
93 my $crypto = _schema->encryption_client;
94 if($crypto)
95 {
96 # no point in checking for encryption unless we have a crypto object setup.
97 my $fields = $self->prf_defaults->search({
98 name => { -in => [keys %$args] },
99 encrypted => 1,
100 });
101 for my $f ($fields->all)
102 {
103 # encrypt the values.
104 # since this is a search do it deterministicly
105 # this won't find values for fields that weren't encrypted deterministicly
106 # but we can't find them anyway, so this will effectively fail closed.
107 # which is about as good as it gets.
108 # we could emit a warning when they try to search one of those fields though.
109 unless($f->unique_field || $f->searchable)
110 {
111 my $name = $f->name;
112 carp "Field $name is being searched for it's encrypted and does not have the searchable flag set so we will probably not find any results.";
113 }
114 $self->_encrypt_query_values($crypto, $args, $f->name);
115 }
116 }
117 for my $name (keys %$args)
118 {
119 my $alias = $x == 1 ? "prf_preferences" : "prf_preferences_$x";
120 my $value = $args->{$name};
121 push @params, {
122 "$alias.name" => $name,
123 "$alias.value" => $value,
124 };
125 push @joins, 'prf_preferences';
126 $x++;
127 }
128  
129 # FIXME: This runs many queries but I'm too overwhelmed right now
130  
131 # We want to find all PrfOwner rows whose PrfPreference values match the
132 # query. This is enough information to filter the existing resultset.
133 # We can't use join any more, because the existing resultset won't have any
134 # relationships into this code.
135  
136 my $local_query = $self->_hacky_prf_owner_subquery;
137  
138 # Join on the PrfPreferences
139 $local_query = $local_query->search({
140 -and => \@params
141 }, {
142 join => \@joins
143 });
144  
145 # Now add the resulting prf_owner_ids back as a filter on the original ID
146 # column.
147 return $self->search({
148 "me.id" => {
149 -in => $local_query->get_column('prf_owner_id')->as_query
150 }
151 });
152 }
153  
154 sub _hacky_prf_owner_subquery {
155 my $self = shift;
156 _schema->resultset('PrfOwner')->search({
157 # They will all have the same ID here. This is a silly architecture.
158 "me.prf_owner_type_id" => $self->get_column('prf_owner_type_id')->first,
159  
160 # Assume the id column is the id column because this is a hack
161 "me.prf_owner_id" => {
162 -in => $self->get_column('id')->as_query,
163 },
164 });
165 }
166  
167 sub select_extra_fields
168 {
169 my ($self, @names) = @_;
170  
171 my @params;
172 my @joins;
173 my $x = 1;
174 my %aliases;
175 for my $name (@names)
176 {
177 my $alias = $x == 1 ? "_by_name" : "_by_name_$x";
178 push @params, $name;
179 push @joins, '_by_name';
180 $aliases{$name} = $alias;
181 $x++;
182 }
183 my $rs = $self->search(undef, {
184 bind => \@params,
185 join => { prf_owner => \@joins },
186 });
187 return { rs => $rs, aliases => \%aliases };
188 }
189  
190 sub prefetch_extra_fields
191 {
192 my ($self, @names) = @_;
193  
194 my @params;
195 my @joins;
196 my $x = 1;
197 my %aliases;
198 my @columns;
199 for my $name (@names)
200 {
201 my $alias = $x == 1 ? "_by_name" : "_by_name_$x";
202 push @params, $name;
203 push @columns, { "extra_$name" => "$alias.value" };
204 push @joins, '_by_name';
205 $aliases{$name} = $alias;
206 $x++;
207 }
208 my $rs = $self->search(undef, {
209 bind => \@params,
210 # Doing this manually since prefetch tries to be too clever
211 # by collapsing stuff and then providing no way to get to the dat
212 # as it doesn't consider multiple joins of the same relationship
213 # to be sane.
214 # Also our data should be flat (there should only be 1 or 0 row we're joining to
215 # so we don't need to do that collapse business.
216 join => { prf_owner => \@joins },
217 '+columns' => \@columns,
218 });
219 return { rs => $rs, aliases => \%aliases };
220 }
221  
222 sub join_by_name
223 {
224 my $self = shift;
225 my $name = shift;
226 $self->search(undef, {
227 join => [{ 'prf_owner' => '_by_name' }],
228 bind => [ $name ],
229 });
230 }
231  
232 sub validate_extra_parameter
233 {
234 my $self = shift;
235 my $field = shift;
236 my $params = shift;
237 my $unique_validator = shift;
238 my $id = shift;
239  
240 if($field->required)
241 {
242 return 'Must specify ' . $field->name unless exists $params->{$field->name};
243 }
244 if($field->unique_field)
245 {
246 # check to see if it's unique
247 my $p = {
248 prf_owner_type_id => $field->prf_owner_type_id,
249 };
250 $p->{id} = $id if $id;
251 my $error = $unique_validator->validate('global_fields_' . $field->name,
252 $params->{$field->name}, $p,
253 { label => $field->comment });
254 return $error if $error;
255 }
256 # FIXME: ought to check types.
257 }
258  
259 sub validate_extra_parameters
260 {
261 my $self = shift;
262 my $params = shift;
263 my $unique_validator = shift;
264 my $id = shift;
265  
266 # check them against their defaults.
267 my @fields = $self->prf_defaults->active;
268 for my $field (@fields)
269 {
270 my $error = $self->validate_extra_parameter($field, $params, $unique_validator, $id);
271 return $error if $error;
272 }
273 }
274  
275 sub _encrypt_query_values
276 {
277 my $self = shift;
278 my $crypto = shift;
279 my $hash = shift;
280 my $new_key = shift;
281 my $val = $hash->{$new_key};
282  
283 if(ref $val eq 'HASH')
284 {
285 my @ops = keys %$val;
286 for my $op (@ops)
287 {
288 if($op =~ /-?ident/)
289 {
290 # skip this.
291 return;
292 }
293 if(ref $val->{$op} eq 'ARRAY')
294 {
295 my @encrypted = map {
296 $crypto->encrypt_deterministic($_)
297 } @{$val->{$op}};
298 $val->{$op} = \@encrypted;
299 }
300 elsif(ref $val->{$op} eq 'HASHREF')
301 {
302 # I have no idea what to do with this.
303 # going to stop here.
304 carp 'Unrecognised search query, not encrypting possible reference to token number';
305 }
306 elsif(!ref $val->{$op})
307 {
308 # convert what we assume is a single value.
309 my $value = $val->{$op};
310 if($op =~ /like/i)
311 {
312 # NOTE:
313 # this could cause some fun and games.
314 $value =~ s/[%?]//g;
315 }
316 my $enc = $crypto->encrypt_deterministic($value);
317 $val->{$op} = $enc;
318 }
319 }
320 }
321 else
322 {
323 my $new_value = $crypto->encrypt_deterministic($val);
324 $hash->{$new_key} = $new_value;
325 }
326 }
327  
328  
329 return 1;
330  
331  
332 =head1 DESCRIPTION
333  
334 =head1 METHODS
335  
336 =head2 prf_get_default
337  
338 =head2 prf_set_default
339  
340 =head2 setup_owner_type
341  
342 =head2 get_owner_type
343  
344 =head2 prf_defaults
345  
346 =head2 with_fields
347  
348 Searches the objecs with fields that match. Pass it a hash of
349 name => value pairs and it will return a resultset of all
350 the owners that match all the requirements. If you want to use
351 ilikes, you can, just like regular DBIC searches. It will figure
352 out the hard relationship stuff for you.
353  
354 my $rs = Owner->with_fields({
355 'simple_test' => 'test',
356 'second_test' => { -ilike => 'test2' },
357 });
358  
359 =head2 validate_extra_parameters
360  
361 =head2 validate_extra_parameter
362  
363 =head2 join_by_name
364  
365 Returns a resultset joined to the preferences with the name specified.
366  
367 $rs->join_by_name('test');
368  
369 =head2 select_extra_fields
370  
371 Returns a resultset joined to the preferences with the names specified.
372 Similar to join_by_name but it makes multiple joins for each name.
373  
374 It returns the new resultset and a list of the field -> aliases so that
375 you can then do whatever you want with them.
376  
377 my $info = $rs->select_extra_fields('test', 'test2');
378 my $new_rs = $info->{rs};
379 my $aliases = $info->{aliases};
380  
381 =head2 prefetch_extra_fields
382  
383 Select the extra fields when searching the resultset.
384 It select's them as C<extra_$fieldname>. These values are
385 accessible via C<get_column>
386  
387 It returns a hashref like L<select_extra_fields> with rs and an alias map.
388  
389 my $info = $rs->prefetch_extra_fields('field1', 'field2');
390 my $new_rs = $info->{rs};
391 my @all = $new_rs->all;
392 my $field1 = $all[0]->get_column('extra_field1');
393  
394 =head2 prf_preferences
395  
396 Returns a resultset of all the preferences relating to this type of PrfOwner.
397  
398 =head1 ATTRIBUTES
399  
400  
401 =head1 LICENSE AND COPYRIGHT
402  
403 Copyright 2012 OpusVL.
404  
405 This software is licensed according to the "IP Assignment Schedule" provided with the development project.
406  
407 =cut
« Preferences/RolesFor/Result/PrfOwner.pm ↑ Summary Preferences.pm »