Preferences/RolesFor/Result/PrfOwner.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Preferences/Schema.pm ↑ Summary Preferences/RolesFor/ResultSet/PrfOwner.pm »
1  
2 package OpusVL::Preferences::RolesFor::Result::PrfOwner;
3  
4 =head1 DESCRIPTION
5  
6 If you are using DBIx::Class::Schema::Loader add the necessary link fields manually, otherwise
7 add the following line to add the fields to your result class.
8  
9 __PACKAGE__->prf_owner_init;
10  
11 =head1 SYNOPSIS
12  
13 =head1 METHODS
14  
15 =head2 prf_owner_init
16  
17 Tries to add the columns and relationships for your result class. Call it like this,
18  
19 __PACKAGE__->prf_owner_init;
20  
21 Your mileage may vary.
22  
23 =head2 prf_defaults
24  
25 ResultSet for the defaults.
26  
27 =head2 prf_preferences
28  
29 ResultSet of the preference values.
30  
31 =head2 prf_get
32  
33 Gets the setting. If the object doesn't have the setting specified but there is a
34 default, the default will be returned.
35  
36 =head2 prf_set
37  
38 Sets the setting for the object.
39  
40 =head2 prf_reset
41  
42 Resets the settings against the object. prf_get may still return a value if there is a default
43 for the setting.
44  
45 =head2 preferences_to_array
46  
47 Returns an array of the current results preferences.
48  
49 $object->preferences_to_array();
50 # [{
51 # name => $_->name,
52 # value => $_->value,
53 # param => # assocaited PrfDefault parameter definition.
54 # } ];
55  
56 =head2 safe_preferences_in_array
57  
58 Returns the same as preferences_to_array but instead of the param object it returns the
59 field label. The safe refers to the fact that all the items in the hash are base types
60 and therefore are trivially serializable.
61  
62 =head2 safe_prefs_to_hash
63  
64 Returns the same as safe_prefs_to_hash but converts it to a hash for easier use.
65  
66 =head1 COPYRIGHT and LICENSE
67  
68 Copyright (C) 2011 OpusVL
69  
70 This software is licensed according to the "IP Assignment Schedule" provided with the development project.
71  
72 =cut
73  
74 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 0 ▶use Scalar::IfDefined qw/$ifdef/;
Subroutine BEGIN: called CC=1 SCAR=2.6
79  
80 sub _schema {
81 state $schema = OpusVL::FB11::Hive
82 ->fancy_hat('preferences')
83 ->schema;
84 }
85  
86 =head2 prf_id_column
87  
88 Provides the default column that contains the preferences identifier.
89  
90 If your Result doesn't have a standard integer primary key called 'id', override
91 this with the name of another column that I<is> an identifying integer
92  
93 =cut
94  
95 sub prf_id_column {'id'}
96  
97 sub prf_owner_init
98 {
99 my $class = shift;
100  
101 $class->add_columns
102 (
103 prf_owner_type_id =>
104 {
105 data_type => 'integer',
106 is_nullable => 1,
107 is_foreign_key => 1
108 }
109 );
110 }
111  
112 sub prf_owner {
113 my $self = shift;
114 return $self->_schema->resultset('PrfOwner')->find({
115 prf_owner_type_id => $self->prf_owner_type_id,
116 prf_owner_id => $self->${ \$self->prf_id_column },
117 });
118 }
119  
120 sub prf_owner_type {
121 my $self = shift;
122 return $self->_schema->resultset('PrfOwnerType')->find({
123 prf_owner_type_id => $self->prf_owner_type_id
124 });
125 }
126  
127 after insert => sub
128 {
129 my $self = shift;
130 my $schema = _schema;
131 my $type = $schema->resultset ('PrfOwnerType')->setup_from_source ($self->result_source);
132  
133 # Ensure that any auto-generated values have been populated (in case the
134 # prf_id column is not a primary key)
135 $self->discard_changes;
136 my $prf_id_column = $self->prf_id_column;
137 $schema->resultset('PrfOwner')->create
138 ({
139 prf_owner_id => $self->$prf_id_column,
140 prf_owner_type_id => $type->prf_owner_type_id
141 });
142
143 $self->update ({ prf_owner_type_id => $type->prf_owner_type_id });
144 };
145  
146 sub prf_defaults
147 {
148 my $self = shift;
149  
150 return $self->prf_owner_type->prf_defaults;
151 }
152  
153 sub prf_preferences
154 {
155 # this could maybe be achieved with a proper DBIx::Class relationship, but
156 # this will do for now
157  
158 my $self = shift;
159  
160 return $self->prf_owner->$ifdef('prf_preferences');
161 }
162  
163 sub preferences_to_array
164 {
165 my $self = shift;
166  
167 my $preferences = $self->prf_preferences
168 or return [];
169  
170 my @expanded;
171 for my $pref ($preferences->all)
172 {
173 my $param = $self->prf_defaults->find({ name => $pref->name });
174 push @expanded, {
175 name => $pref->name,
176 value => $param->decryption_routine->($pref->value),
177 param => $param,
178 };
179 }
180 my @d = sort {
181 $a->{param}->display_order <=> $b->{param}->display_order
182 } @expanded;
183 return \@d;
184 }
185  
186 sub safe_preferences_in_array
187 {
188 my $self = shift;
189 my $extra_params = $self->preferences_to_array;
190 my @cleaned_up = map { {
191 name => $_->{name},
192 value => $_->{value},
193 label => $_->{param}->comment,
194 } } @$extra_params;
195 return \@cleaned_up;
196 }
197  
198 sub safe_prefs_to_hash
199 {
200 my $self = shift;
201 my $info = $self->safe_preferences_in_array;
202 my %hash = map { $_->{name} => $_->{value} } @$info;
203 return \%hash;
204 }
205  
206 sub prf_get
207 {
208 my $self = shift;
209 my $name = shift;
210  
211 my $default = $self->prf_defaults->search ({ name => $name })->first;
212 die "Field $name not setup" unless $default;
213  
214 my $pref = $self->prf_preferences->search ({ name => $name })->first;
215 my $value;
216 $value = $pref->value if $pref;
217 if($default->encrypted)
218 {
219 if($pref)
220 {
221 my $schema = _schema;
222 my $crypto = $schema->encryption_client;
223 if($crypto)
224 {
225 $value = $crypto->decrypt($value);
226 }
227 }
228 }
229 return $value if defined $value;
230  
231 # FIXME: should probably look at encrypting defaults,
232 # although, then again, do we need to?
233 return $default->default_value
234 if defined $default;
235  
236 return;
237 }
238  
239 sub _clear_out_inactive_unique_values
240 {
241 my $self = shift;
242 my $prefname = shift;
243 my $field = shift;
244  
245 my $schema = $self->_schema;
246 my $obj_rs = $schema->resultset($self->prf_owner_type->owner_resultset);
247 if($obj_rs->can('inactive_for_unique_params'))
248 {
249 my $rs = $obj_rs->inactive_for_unique_params;
250 $schema->resultset('PrfOwner')->search({
251 "prf_preferences.prf_owner_id" => {
252 -in => [ $rs->get_column($self->prf_id_column)->all ],
253 },
254 "prf_preferences.prf_owner_type_id" => $self->prf_owner_type_id
255 })
256 ->search_related('prf_preferences',
257 {
258 "prf_preferences.name" => $prefname,
259 "prf_preferences.prf_owner_type_id" => $field->prf_owner_type_id,
260 }
261 )->search_related('unique_value')->delete;
262 }
263 }
264  
265 sub prf_set
266 {
267 my $self = shift;
268 my $prefname = shift;
269 my $value = shift;
270  
271 my $allprefs = $self->prf_preferences;
272
273 my $pref = $allprefs->search ({ name => $prefname })->first;
274 my $field = $self->prf_defaults->search ({ name => $prefname })->first;
275 unless($field)
276 {
277 die "Field $prefname not setup.";
278 }
279  
280 if($field->encrypted)
281 {
282 my $schema = $self->result_source->schema;
283 my $crypto = $schema->encryption_client;
284  
285 # if we need to search or ensure unique values,
286 # then we have to use deterministic encryption
287 # which is less secure, but still encrypted.
288  
289 if($crypto)
290 {
291 if($field->unique_field || $field->searchable)
292 {
293 $value = $crypto->encrypt_deterministic($value);
294 }
295 else
296 {
297 $value = $crypto->encrypt($value);
298 }
299 }
300 }
301 if ($pref)
302 {
303 $pref->update ({ value => $value });
304  
305 if($field->unique_field)
306 {
307 $self->_clear_out_inactive_unique_values($prefname, $field);
308 my $unique_val = $pref->unique_value;
309 if($unique_val)
310 {
311 my $place_holder = $value;
312 if($field->data_type eq 'email')
313 {
314 $place_holder = lc $value;
315 }
316 $unique_val->value($place_holder);
317 $unique_val->update;
318 }
319 else
320 {
321 $pref->create_related('unique_value', { value => $value });
322 }
323 }
324 }
325 else
326 {
327 my $data = {
328 name => $prefname,
329 value => $value
330 };
331 if($field->unique_field)
332 {
333 $self->_clear_out_inactive_unique_values($prefname, $field);
334 my $place_holder = $value;
335 if($field->data_type eq 'email')
336 {
337 $place_holder = lc $value;
338 }
339 $data->{unique_value} = { value => $place_holder };
340 }
341 $allprefs->create($data);
342 }
343 }
344  
345 sub prf_reset
346 {
347 my $self = shift;
348 my $name = shift;
349  
350 my $val = $self->prf_preferences->search ({ 'me.name' => $name });
351 $val->search_related('unique_value')->delete;
352 $val->delete;
353 }
354  
355 return 1;
356  
« Preferences/Schema.pm ↑ Summary Preferences/RolesFor/ResultSet/PrfOwner.pm »