File Coverage

blib/lib/OpusVL/Preferences/Schema/Result/PrfDefault.pm
Criterion Covered Total %
statement 18 48 37.5
branch 0 12 0.0
condition 0 3 0.0
subroutine 6 16 37.5
pod 5 5 100.0
total 29 84 34.5


line stmt bran cond sub pod time code
1              
2             package OpusVL::Preferences::Schema::Result::PrfDefault;
3              
4 1     1   3268 use strict;
  1         2  
  1         30  
5 1     1   6 use warnings;
  1         1  
  1         26  
6 1     1   34 use Moose;
  1         2  
  1         7  
7 1     1   5570 use MooseX::NonMoose;
  1         2  
  1         7  
8 1     1   4494 use namespace::autoclean;
  1         3  
  1         8  
9 1     1   380 use OpusVL::Text::Util qw/mask_text/;
  1         1396  
  1         597  
10              
11             extends 'DBIx::Class::Core';
12              
13             __PACKAGE__->table("prf_defaults");
14              
15             __PACKAGE__->add_columns
16             (
17             prf_owner_type_id =>
18             {
19             data_type => "integer",
20             is_nullable => 0,
21             },
22              
23             name =>
24             {
25             data_type => "varchar",
26             is_nullable => 0,
27             },
28              
29             default_value =>
30             {
31             data_type => "varchar",
32             is_nullable => 0,
33             },
34              
35             data_type =>
36             {
37             data_type => 'varchar',
38             is_nullable => 1
39             },
40              
41             comment =>
42             {
43             data_type => 'varchar',
44             is_nullable => 1
45             },
46              
47             required =>
48             {
49             data_type => 'boolean',
50             is_nullable => 1,
51             default_value => 0,
52             },
53             active =>
54             {
55             data_type => 'boolean',
56             is_nullable => 1,
57             default_value => 1,
58             },
59             hidden =>
60             {
61             data_type => 'boolean',
62             is_nullable => 1,
63             },
64              
65             audit => {
66             data_type => 'boolean',
67             is_nullable => 1,
68             },
69              
70             display_on_search =>
71             {
72             data_type => 'boolean',
73             is_nullable => 1,
74             },
75             searchable =>
76             {
77             data_type => 'boolean',
78             default_value => 1,
79             is_nullable => 0,
80             },
81             # note: this isn't stricly enforced by the module.
82             # NOTE: might need to switch this to validator class
83             unique_field =>
84             {
85             data_type => 'boolean',
86             is_nullable => 1,
87             },
88             ajax_validate =>
89             {
90             data_type => 'boolean',
91             is_nullable => 1,
92             },
93              
94             display_order =>
95             {
96             data_type => 'int',
97             is_nullable => 0,
98             default_value => 1,
99             },
100             confirmation_required =>
101             {
102             data_type => 'boolean',
103             is_nullable => 1,
104             },
105             encrypted =>
106             {
107             data_type => 'boolean',
108             is_nullable => 1,
109             },
110              
111             display_mask =>
112             {
113             data_type => 'varchar',
114             is_nullable => 0,
115             default_value => '(.*)',
116             },
117             mask_char =>
118             {
119             data_type => 'varchar',
120             is_nullable => 0,
121             default_value => '*',
122             },
123             );
124              
125              
126             __PACKAGE__->set_primary_key(qw/prf_owner_type_id name/);
127             __PACKAGE__->has_many
128             (
129             values => "OpusVL::Preferences::Schema::Result::PrfDefaultValues",
130             {
131             "foreign.name" => "self.name",
132             "foreign.prf_owner_type_id" => "self.prf_owner_type_id",
133             },
134             );
135              
136             __PACKAGE__->has_many
137             (
138             preferences => "OpusVL::Preferences::Schema::Result::PrfPreference",
139             {
140             "foreign.name" => "self.name",
141             "foreign.prf_owner_type_id" => "self.prf_owner_type_id",
142             },
143             );
144              
145              
146             __PACKAGE__->belongs_to
147             (
148             owner_type => 'OpusVL::Preferences::Schema::Result::PrfOwnerType',
149             {
150             'foreign.prf_owner_type_id' => 'self.prf_owner_type_id'
151             }
152             );
153              
154             sub form_options
155             {
156 0     0 1   my $self = shift;
157 0           my @options = map { [ $_->value, $_->value ] } $self->values->sorted;
  0            
158 0           return \@options;
159             }
160              
161             sub hash_key
162             {
163 0     0 1   my $self = shift;
164 0           return $self->name;
165             }
166              
167             # FIXME: deal with encryption.
168              
169             around update => sub {
170             my $orig = shift;
171             my $self = shift;
172             my $update = shift;
173             $update //= {};
174             $self->set_inflated_columns($update);
175              
176             my $schema = $self->result_source->schema;
177             my $txn = $schema->txn_scope_guard;
178             my %updated_columns = ($self->get_dirty_columns);
179             $self->$orig;
180             if(exists $updated_columns{unique_field})
181             {
182             my $obj_rs = $schema->resultset($self->owner_type->owner_resultset);
183             if($self->unique_field)
184             {
185             # create the unique values
186             my $rs = $obj_rs;
187             if($obj_rs->can('active_for_unique_params'))
188             {
189             $rs = $obj_rs->active_for_unique_params;
190             }
191             my $params = $rs->search_related('prf_owner')->search_related('prf_preferences',
192             {
193             "prf_preferences.name" => $self->name,
194             }
195             );
196             # this kind of sucks, it would be a lot neater to do an insert based on the query.
197             # perhaps I could do a select and get the query then do the insert simply?
198             map { $_->create_related('unique_value', { value => $_->value }) } $params->all;
199             }
200             else
201             {
202             # wipe them out.
203             $self->preferences->search_related('unique_value')->delete;
204             }
205             }
206             $txn->commit;
207             };
208              
209             sub decryption_routine
210             {
211 0     0 1   my $self = shift;
212 0 0         if($self->encrypted)
213             {
214 0           my $schema = $self->result_source->schema;
215 0           my $crypto = $schema->encryption_client;
216 0 0         if($crypto)
217             {
218 0     0     return sub { return $crypto->decrypt(shift) };
  0            
219             }
220             }
221 0     0     return sub { shift; };
  0            
222             }
223              
224             sub encryption_routine
225             {
226 0     0 1   my $self = shift;
227 0 0         if($self->encrypted)
228             {
229 0           my $schema = $self->result_source->schema;
230 0           my $crypto = $schema->encryption_client;
231 0 0         if($crypto)
232             {
233 0 0 0       if($self->unique_value || $self->searchable)
234             {
235 0     0     return sub { return $crypto->decrypt(shift) };
  0            
236             }
237             }
238             }
239 0     0     return sub { shift; };
  0            
240             }
241              
242             sub mask_function
243             {
244 0     0 1   my $self = shift;
245             return sub {
246 0     0     my $val = shift;
247 0 0         return $val unless length $self->mask_char;
248 0           return mask_text($self->mask_char, $self->display_mask, $val);
249 0           };
250             }
251              
252             return 1;
253              
254             __END__
255              
256             =pod
257              
258             =encoding UTF-8
259              
260             =head1 NAME
261              
262             OpusVL::Preferences::Schema::Result::PrfDefault
263              
264             =head1 VERSION
265              
266             version 0.27
267              
268             =head1 DESCRIPTION
269              
270             =head1 METHODS
271              
272             =head2 form_options
273              
274             =head1 ATTRIBUTES
275              
276             =head2 values
277              
278             =head2 prf_owner_type_id
279              
280             =head2 name
281              
282             =head2 default_value
283              
284             =head2 data_type
285              
286             =head2 comment
287              
288             =head2 required
289              
290             =head2 active
291              
292             =head2 hidden
293              
294             =head2 hash_key
295              
296             Returns a string convenient for use in hashes based on the parameter name.
297              
298             =head2 encrypted
299              
300             A flag indicating if the field is encrypted. This requires the symmetric encryption
301             keys to be setup on the Schema object.
302              
303             Note that methods like C<prefetch_extra_fields> and C<select_extra_fields> will return
304             the value encrypted and you will need to decrypt the values yourself.
305              
306             This can be done like this,
307              
308             $schema->crypto->decrypt($r->value);
309              
310             Searches using with_fields will work if the field has either of the
311             properties, C<searchable> or C<unique_field> set. They will switch
312             the encryption to use a deterministic mode which will allow searches of full
313             values to work. Partial value searching will not.
314              
315             If you're searching the dataset manually you will need to encrypt your search
316             term with the C<encrypt_deterministic> function.
317              
318             $schema->crypto->encrypt_deterministic($val);
319              
320             The prf_get and prf_set functions will deal with the encryption seamlessly.
321              
322             Changing this flag on an existing dataset, or the other flags will not cause
323             any data to be encrypted or decrypted. You will need to do that sort of
324             maintenance manually.
325              
326             =head2 decryption_routine
327              
328             Returns a subref with code to decrypt a value for storage. If encryption is
329             not turned on or configured this will simply return the raw value.
330              
331             =head2 encryption_routine
332              
333             Returns a subref with code to encrypt a value for storage. If encryption is
334             not turned on or configured this will simply return the raw value.
335              
336             =head2 display_mask
337              
338             Display mask for sensitive fields. A regex specifying which characters to display
339             and which to mask out. Use captures to identify the characters to display, the rest
340             will be masked out.
341              
342             For instance, '(\d{3}).*(\d{4})' will display the first 3 digits, and the last 4.
343              
344             Note that if the regex does not match at all it will blank out the whole string.
345              
346             The default is set to (.*) which means no characters are hidden out of the box.
347              
348             Note that this won't save you from security bugs in your own code, only leaks
349             of valid outputs from your programs.
350              
351             =head2 mask_char
352              
353             Character to use when masking out sensitive data.
354              
355             Defaults to *
356              
357             =head2 mask_function
358              
359             Provides a subref that will mask values of this field.
360              
361             my $mask = $field->mask_function;
362             $mask->($value->value);
363              
364             =head1 AUTHOR
365              
366             OpusVL - www.opusvl.com
367              
368             =head1 COPYRIGHT AND LICENSE
369              
370             This software is copyright (c) 2011 by OpusVL - www.opusvl.com.
371              
372             This is free software; you can redistribute it and/or modify it under
373             the same terms as the Perl 5 programming language system itself.
374              
375             =cut