File Coverage

blib/lib/RapidApp/DBIC/Component/PassphraseColumn.pm
Criterion Covered Total %
statement 55 56 98.2
branch 10 16 62.5
condition 3 8 37.5
subroutine 13 13 100.0
pod 1 3 33.3
total 82 96 85.4


line stmt bran cond sub pod time code
1 1     1   7 use strict;
  1         3  
  1         25  
2 1     1   5 use warnings;
  1         2  
  1         37  
3              
4             package # hide from PAUSE
5             RapidApp::DBIC::Component::PassphraseColumn;
6            
7             # Temp copy of DBIx::Class::PassphraseColumn with fix for null columns.
8             # will stop using this as soon as the real module merges that fix
9             # https://github.com/rafl/dbix-class-passphrasecolumn/pull/3
10              
11 1     1   5 use Class::Load 'load_class';
  1         1  
  1         71  
12 1     1   6 use Sub::Name 'subname';
  1         3  
  1         49  
13 1     1   6 use namespace::clean;
  1         2  
  1         7  
14              
15 1     1   203 use parent 'DBIx::Class';
  1         2  
  1         7  
16              
17              
18             __PACKAGE__->load_components(qw(InflateColumn::Authen::Passphrase));
19              
20             __PACKAGE__->mk_classdata('_passphrase_columns');
21              
22             sub register_column {
23 7     7 1 3535 my ($self, $column, $info, @rest) = @_;
24              
25 7 100       24 if (my $encoding = $info->{passphrase}) {
26 1         3 $info->{inflate_passphrase} = $encoding;
27              
28             $self->throw_exception(q['passphrase_class' is a required argument])
29             unless exists $info->{passphrase_class}
30 1 50 33     8 && defined $info->{passphrase_class};
31              
32 1         3 my $class = 'Authen::Passphrase::' . $info->{passphrase_class};
33 1         7 load_class $class;
34              
35 1   50     20941 my $args = $info->{passphrase_args} || {};
36 1 50       6 $self->throw_exception(q['passphrase_args' must be a hash reference])
37             unless ref $args eq 'HASH';
38              
39             my $encoder = sub {
40 3     3   10 my ($val) = @_;
41 3         8 $class->new(%{ $args }, passphrase => $val)->${\"as_${encoding}"};
  3         36  
  3         143605  
42 1         8 };
43              
44             $self->_passphrase_columns({
45 1 50       2 %{ $self->_passphrase_columns || {} },
  1         41  
46             $column => $encoder,
47             });
48              
49 1 50       183 if (defined(my $meth = $info->{passphrase_check_method})) {
50             my $checker = sub {
51 5     5   21532 my ($row, $val) = @_;
        5      
52 5 50       85 my $ppr = $row->get_inflated_column($column) or return 0;
53 5         1561 return $ppr->match($val);
54 1         5 };
55              
56 1         32 my $name = join q[::] => $self->result_class, $meth;
57              
58             {
59 1     1   326 no strict 'refs';
  1         2  
  1         248  
  1         630  
60 1         11 *$name = subname $name => $checker;
61             }
62             }
63             }
64              
65 7         20 $self->next::method($column, $info, @rest);
66             }
67              
68             sub set_column {
69 9     9 0 114 my ($self, $col, $val, @rest) = @_;
70              
71 9         222 my $ppr_cols = $self->_passphrase_columns;
72             return $self->next::method($col, $ppr_cols->{$col}->($val), @rest)
73 9 100       391 if exists $ppr_cols->{$col};
74              
75 6         30 return $self->next::method($col, $val, @rest);
76             }
77              
78             sub new {
79 1     1 0 20 my ($self, $attr, @rest) = @_;
80              
81 1         59 my $ppr_cols = $self->_passphrase_columns;
82 1         41 for my $col (keys %{ $ppr_cols }) {
  1         5  
83 1 50 33     5 next unless exists $attr->{$col} && !ref $attr->{$col};
84 0         0 $attr->{$col} = $ppr_cols->{$col}->( $attr->{$col} );
85             }
86              
87 1         6 return $self->next::method($attr, @rest);
88             }
89              
90              
91             1;