File Coverage

blib/lib/OpusVL/Preferences/RolesFor/Result/PrfOwner.pm
Criterion Covered Total %
statement 9 101 8.9
branch 0 36 0.0
condition 0 3 0.0
subroutine 3 14 21.4
pod 10 10 100.0
total 22 164 13.4


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