blib/lib/DBIx/Class/DigestColumns.pm

filter: Statementstmt 88.4% 84 / 95 Branchbran 59.0% 26 / 44 Conditioncond 66.6% 10 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 93.7% 15 / 16 Podpod 100% 7 / 7 totaltotal 78.4% 146 / 186 CC 31 SCAR 21.7
CC
31
Cov
78%
digestcolumns
42.0
_digest_column_values
6.0
_get_digest_string
5.8
CRAP 8.8
↑ Summary
1 package DBIx::Class::DigestColumns;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 __PACKAGE__->mk_classdata( 'digest_auto_columns' => [] );
11 5 __PACKAGE__->mk_classdata( 'digest_auto' => 1 );
12 5 __PACKAGE__->mk_classdata( 'digest_dirty' => 0 );
13 5 __PACKAGE__->mk_classdata( 'digest_maker' );
14 5 __PACKAGE__->mk_classdata( 'encoding' );
15  
16 5 __PACKAGE__->digest_algorithm('MD5');
17 5 __PACKAGE__->digest_encoding('hex');
18  
19 # Always remember to do all digits for the version even if they're 0
20 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
21 # brain damage and presumably various other packaging systems too
22  
23 5 $VERSION = '0.06001_01';
24  
25 =head1 NAME
26  
27 DBIx::Class::DigestColumns - Automatic digest columns
28  
29 =head1 SYNOPSIS
30  
31 In your L<DBIx::Class> table class:
32  
33 __PACKAGE__->load_components(qw/DigestColumns ... Core/);
34  
35 #automatically generate a method "check_password" in result class
36 __PACKAGE__->add_columns(
37 'password' => {
38 data_type => 'char',
39 size => 32,
40 digest_check_method => 'check_password',
41 }
42 __PACKAGE__->digestcolumns(
43 columns => [qw/ password /],
44 algorithm => 'MD5',
45 encoding => 'base64',
46 dirty => 1,
47 auto => 1,
48 );
49  
50 B<Note:> The component needs to be loaded I<before> Core.
51  
52 Alternatively you could call each method individually
53  
54 __PACKAGE__->digest_columns(qw/ password /);
55 __PACKAGE__->digest_algorithm('MD5');
56 __PACKAGE__->digest_encoding('base64');
57 __PACKAGE__->digest_dirty(1);
58 __PACKAGE__->digest_auto(1);
59  
60  
61  
62 =head1 DESCRIPTION
63  
64 This L<DBIx::Class> component can be used to automatically insert a message
65 digest of selected columns. By default DigestColumns will use
66 L<Digest::MD5> to insert a 128-bit hexadecimal message digest of the column
67 value.
68  
69 The length of the inserted string will be 32 and it will only contain characters
70 from this set: '0'..'9' and 'a'..'f'.
71  
72 If you would like to use a specific digest module to create your message
73 digest, you can set L</digest_algorithm>:
74  
75 __PACKAGE__->digest_algorithm('SHA-1');
76  
77 =head1 Options added to add_column
78  
79 =head2 digest_check_method => $method_name
80  
81 By using the digest_check_method attribute when you declare a column you
82 can create a check method for that column. The check method accepts a
83 plain text string, performs the correct digest on it and returns a boolean
84 value indicating whether this method matches the currently_stored value.
85  
86 $row->password('old_password');
87 $row->update;
88 $row->password('new_password');
89 $row->check_password('new_password'); #returns true
90 $row->check_password('old_password'); #returns false
91 $row->update;
92  
93 =head1 METHODS
94  
95 =head2 digestcolumns
96  
97 __PACKAGE__->digestcolumns(
98 columns => [qw/ password /],
99 algorithm => $algorithm',
100 encoding => $encoding,
101 dirty => 1,
102 auto => 1,
103 );
104  
105 Calls L</digest_columns>, L</digest_algorithm>, and L</digest_encoding> and L</digest_auto> if the
106 corresponding argument is defined.
107  
108 =cut
109  
110 20 ▶sub register_column {
Subroutine register_column: called CC=4 SCAR=14.0
Pod documented
111 20 my ($self, $column, $info, @rest) = @_;
112 20 $self->next::method($column, $info, @rest);
113  
114 20 ▶ return unless defined $info->{'digest_check_method'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"digest_check_method"} 16 4 20
115  
116 4 my $method_name = $info->{'digest_check_method'};
117 4 my $result_class = $self->result_class;
118 #don't overwrite another method
119 4 ▶ $self->throw_exception("Can't create digest_check_method ${method_name}. ".
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method_name) or $result_class->can($method_name) 0 4 4
Condition33%1/3
$self->can($method_name) or $result_class->can($method_name)
l!l&&r!l&&!r
004
MC/DC0%0/2
$self->can($method_name) or $result_class->can($method_name)
$self->can($method_name) $result_class->can($method_name)
Truth table33%1/3
A or B
A= $self->can($method_name)B= $result_class->can($method_name)
ABresult
1X1
011
000
120 "{$method_name} already exists.")
121 if $self->can($method_name) || $result_class->can($method_name);;
122 4 my $class_method_name = $result_class."::".$method_name;
123  
124 {
125 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
126 3 ▶ *$class_method_name = sub{
Subroutine __ANON__: called CC=3 SCAR=11.0
127 3 my ($self, $value) = @_;
128 3 my $col_value = $self->get_column($column);
129 #make sure we DTRT if column is dirty
130 3 ▶ $col_value = $self->_get_digest_string($col_value)
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_column_changed($column) and $self->digest_auto 1 2 3
Condition66%2/3
$self->is_column_changed($column) and $self->digest_auto
!ll&&!rl&&r
201
MC/DC50%1/2
$self->is_column_changed($column) and $self->digest_auto
$self->is_column_changed($column) $self->digest_auto
Truth table66%2/3
A and B
A= $self->is_column_changed($column)B= $self->digest_auto
ABresult
0X0
100
111
131 if $self->is_column_changed($column) && $self->digest_auto;
132  
133 3 return $col_value eq $self->_get_digest_string($value);
134 4 };
135 }
136 }
137  
138 =head2 register_column
139  
140 Override the original register_column to handle the creation of
141 check methods.
142  
143 =cut
144  
145 0 ▶sub digestcolumns {
Subroutine digestcolumns: not called CC=6 SCAR=37.4
Pod documented
146 0 my $self = shift;
147 0 my %args = @_;
148 0 ▶ $self->digest_columns( @{$args{columns}} ) if exists $args{columns};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"columns"} 0 0 0
149 0 ▶ $self->digest_algorithm( $args{algorithm} ) if exists $args{algorithm};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"algorithm"} 0 0 0
150 0 ▶ $self->digest_encoding( $args{encoding} ) if exists $args{encoding};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"encoding"} 0 0 0
151 0 ▶ $self->digest_auto( $args{auto} ) if exists $args{auto};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"auto"} 0 0 0
152 0 ▶ $self->digest_dirty( $args{dirty} ) if exists $args{dirty};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"dirty"} 0 0 0
153 }
154  
155 =head2 digest_columns
156  
157 Takes a list of columns to be convert to a message digest during insert.
158  
159 __PACKAGE__->digest_columns(qw/ password /);
160  
161 =cut
162  
163 8 ▶sub digest_columns {
Subroutine digest_columns: called CC=3 SCAR=11.1
Pod documented
164 8 my $self = shift;
165 8 for (@_) {
166 12 ▶ $self->throw_exception("column $_ doesn't exist") unless $self->has_column($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_column($_) 0 12 12
167 }
168 8 $self->digest_auto_columns(\@_);
169 }
170  
171 =head2 digest_algorithm
172  
173 Takes the name of a digest algorithm to be used to calculate the message digest.
174  
175 __PACKAGE__->digest_algorithm('SHA-1');
176  
177 If a suitible digest module could not be loaded an exception will be thrown.
178  
179 Supported digest algorithms are:
180  
181 MD5
182 MD4
183 MD2
184 SHA-1
185 SHA-256
186 SHA-384
187 SHA-512
188 CRC-16
189 CRC-32
190 CRC-CCITT
191 HMAC-SHA-1
192 HMAC-MD5
193 Whirlpool
194 Adler-32
195  
196 digest_algorithm defaults to C<MD5>.
197  
198 =cut
199  
200 20 ▶sub digest_algorithm {
Subroutine digest_algorithm: called CC=3 SCAR=11.0
Pod documented
201 20 my ($self, $class) = @_;
202  
203 20 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) 20 0 20
204 20 ▶ if (!eval { Digest->new($class) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not eval { do { 'Digest'->new($class) } }) { } 1 19 20
205 1 $self->throw_exception("$class could not be used as a digest algorithm: $@");
206 } else {
207 19 $self->digest_maker(Digest->new($class));
208 };
209 };
210 19 return ref $self->digest_maker;
211 }
212  
213 =head2 digest_encoding
214  
215 Selects the encoding to use for the message digest.
216  
217 __PACKAGE__->digest_encoding('base64');
218  
219 Possilbe encoding schemes are:
220  
221 binary
222 hex
223 base64
224  
225 digest_encoding defaults to C<hex>.
226  
227 =cut
228  
229 20 ▶sub digest_encoding {
Subroutine digest_encoding: called CC=3 SCAR=11.0
Pod documented
230 20 my ($self, $encoding) = @_;
231 20 ▶ if ($encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) 20 0 20
232 20 ▶ if ($encoding =~ /^(?:binary|hex|base64)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding =~ /^(?:binary|hex|base64)$/) { } 19 1 20
233 19 $self->encoding($encoding);
234 } else {
235 1 $self->throw_exception("$encoding is not a supported encoding scheme");
236 };
237 };
238 19 return $self->encoding;
239 }
240  
241 =head2 _get_digest_string $value
242  
243 Handles the actual encoding of column values into digests.
244 When given a C<$value> it will return the digest string for
245 that value. This is the method used by C<_digest_column_values>
246 So you can use it to create an identical digest if you need one
247 for comparison (e.g. password authentication).
248  
249 =cut
250  
251 25 ▶sub _get_digest_string {
Subroutine _get_digest_string: called CC=5 SCAR=17.6
252 25 my ($self, $value) = @_;
253 25 my $digest_string;
254  
255 25 $self->digest_maker->add($value);
256  
257 25 ▶ if ($self->encoding eq 'binary') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->encoding eq 'binary') { } 0 25 25
elsif ($self->encoding eq 'hex') { } 18 7 25
258 0 $digest_string = eval { $self->digest_maker->digest };
259  
260 } elsif ($self->encoding eq 'hex') {
261 18 $digest_string = eval { $self->digest_maker->hexdigest };
262  
263 } else {
264 7 $digest_string = eval { $self->digest_maker->b64digest } ||
265 0 ▶ eval {$self->digest_maker->base64digest };
Condition33%1/3
eval { do { $self->digest_maker->b64digest } } || eval { do { $self->digest_maker->base64digest } }
l!l&&r!l&&!r
700
MC/DC0%0/2
eval { do { $self->digest_maker->b64digest } } || eval { do { $self->digest_maker->base64digest } }
eval { do { $self->digest_maker->b64digest } } eval { do { $self->digest_maker->base64digest } }
Truth table33%1/3
A || B
A= eval { do { $self->digest_maker->b64digest } }B= eval { do { $self->digest_maker->base64digest } }
ABresult
1X1
011
000
266 };
267  
268 25 ▶ $self->throw_exception("could not get a digest string: $@")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $digest_string 0 25 25
269 unless defined( $digest_string );
270  
271 25 return $digest_string;
272 }
273  
274 =head2 _digest_column_values
275  
276 Go through the columns and digest the values that need it.
277  
278 This method is called by insert and update when automatic digests
279 are turned on. If dirty is enabled it will only digest the values
280 of dirtied columns.
281  
282 =cut
283  
284 20 ▶sub _digest_column_values{
Subroutine _digest_column_values: called CC=6 SCAR=17.9
285 20 my $self = shift;
286  
287 20 for my $col (@{$self->digest_auto_columns}) {
288 #if dirty is required then don't update unchanged columns
289 23 ▶ next if $self->digest_dirty &&
Branch: 1 branch
BranchTrueFalseTotal
if $self->digest_dirty and not $self->is_column_changed($col) and $self->in_storage 2 21 23
Condition100%6/6
$self->digest_dirty and not $self->is_column_changed($col) and $self->in_storage
!ll&&!rl&&r
1922
$self->digest_dirty and not $self->is_column_changed($col)
!ll&&!rl&&r
1724
MC/DC100%3/3
$self->digest_dirty and not $self->is_column_changed($col) and $self->in_storage
$self->digest_dirty not $self->is_column_changed($col) $self->in_storage
Truth table100%4/4
A and not(B) and C
A= $self->digest_dirtyB= not $self->is_column_changed($col)C= $self->in_storage
ABCresult
0XX0
10X0
1100
1111
290 !$self->is_column_changed( $col ) && $self->in_storage;
291  
292  
293 #don't digest null columns
294 21 my $col_v = $self->get_column( $col );
295 21 ▶ next unless defined $col_v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_v 0 21 21
296  
297 #update column value with encoded value if needed
298 21 $self->set_column( $col, $self->_get_digest_string( $col_v ) );
299 }
300 }
301  
302 =head2 digest_auto
303  
304 __PACKAGE__->digest_auto(1);
305  
306 Turns on and off automatic digest columns. When on, this feature makes all
307 UPDATEs and INSERTs automatically insert a message digest of selected columns.
308  
309 The default is for digest_auto is to be on.
310  
311 =head2 digest_dirty
312  
313 __PACKAGE__->digest_dirty(1);
314  
315 Turns on and off the limiting of automatic digests to only dirty columns.
316 When on, only columns that have been dirtied will have their values digested
317 during UPDATEs and INSERTs. If auto is set to off this option does nothing.
318  
319 The default is for digest_dirty is to be off to mantain compatibility with older
320 versions of this module.
321  
322 =head1 EXTENDED METHODS
323  
324 The following L<DBIx::Class::Row> methods are extended by this module:-
325  
326 =over 4
327  
328 =item insert
329  
330 =cut
331  
332 16 ▶sub insert {
Subroutine insert: called CC=2 SCAR=7.1
Pod documented
333 16 my $self = shift;
334 16 ▶ $self->_digest_column_values if $self->digest_auto;
Branch: 1 branch
BranchTrueFalseTotal
if $self->digest_auto 16 0 16
335 16 $self->next::method(@_);
336 }
337  
338 =item update
339  
340 =cut
341  
342 5 ▶sub update {
Subroutine update: called CC=5 SCAR=16.1
Pod documented
343 5 my ( $self, $upd, @rest ) = @_;
344 5 ▶ if ( ref $upd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $upd) 2 3 5
345 2 for my $col ( @{$self->digest_auto_columns} ) {
346 $self->set_column($col => delete $upd->{$col})
347 3 ▶ if ( exists $upd->{$col} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $upd->{$col} 2 1 3
348 }
349 }
350 5 ▶ $self->_digest_column_values if $self->digest_auto;
Branch: 1 branch
BranchTrueFalseTotal
if $self->digest_auto 4 1 5
351 5 $self->next::method($upd, @rest);
352 }
353  
354 5 1;
355 __END__
↑ Summary