DBIC/User.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 151 CC 9 SCAR 15.1
CC
9
Cov
0%
obj
12.0
canonical_id
6.0
password
6.0
CRAP 4.5
1 package Catalyst::Plugin::Authentication::Store::DBIC::User;
2  
3 use strict;
4 use warnings;
5 use base qw/Catalyst::Plugin::Authentication::User Class::Accessor::Fast/;
6 use Set::Object ();
7 use Carp qw/confess/;
8 use Data::Dumper;
9  
10 use overload '""' => sub { shift->id }, 'bool' => sub { 1 }, fallback => 1;
11  
12 __PACKAGE__->mk_accessors(qw/id config store _obj/);
13  
14  
15 sub new {
16 my ( $class, $id, $config ) = @_;
17 bless {
18 id => $id,
19 config => $config
20 }, $class;
21 }
22  
23 sub obj {
24 my $self=shift;
25 my $config=$self->config;
26 my $id=$self->id;
27 unless (ref $self->_obj) {
28 my $query = @{$config->{auth}{user_field}} > 1
29 ? { -or => [ map { { $_ => $id } } @{$config->{auth}{user_field}} ] }
30 : { $config->{auth}{user_field}[0] => $id };
31 $self->_obj($config->{auth}{user_class}->search($query)->first);
32 }
33 return $self->_obj;
34 }
35  
36 sub canonical_id {
37 my $self=shift;
38 return undef unless $self->obj();
39 my $field = $self->config->{auth}{user_field}[0];
40 return $self->obj->$field,
41 }
42  
43  
44 *user = \&obj;
45 *crypted_password = \&password;
46 *hashed_password = \&password;
47  
48 sub hash_algorithm { shift->config->{auth}{password_hash_type} }
49  
50 sub password_pre_salt { shift->config->{auth}{password_pre_salt} }
51  
52 sub password_post_salt { shift->config->{auth}{password_post_salt} }
53  
54 sub password_salt_len { shift->config->{auth}{password_salt_len} }
55  
56 sub password {
57 my $self = shift;
58  
59 return undef unless defined $self->obj;
60 my $password_field = $self->config->{auth}{password_field};
61 return $self->obj->$password_field;
62 }
63  
64 sub supported_features {
65 my $self = shift;
66 $self->config->{auth}{password_type} ||= 'clear';
67  
68 return {
69 password => {
70 $self->config->{auth}{password_type} => 1,
71 },
72 session => 1,
73 session_data => $self->{config}{auth}{session_data_field} ? 1 : 0,
74 roles => { self_check => 1 },
75 };
76 }
77  
78 sub get_session_data {
79 my ( $self ) = @_;
80 my $col = $self->config->{auth}{session_data_field};
81 return $self->obj->$col;
82 }
83  
84 sub store_session_data {
85 my ( $self, $data ) = @_;
86 my $col = $self->config->{auth}{session_data_field};
87 my $obj = $self->obj;
88 $obj->result_source->schema->txn_do(sub {
89 $obj->$col($data);
90 $obj->update;
91 });
92 }
93  
94 sub check_roles {
95 my ( $self, @wanted_roles ) = @_;
96  
97 my $have = Set::Object->new( $self->roles( @wanted_roles ) );
98 my $need = Set::Object->new( @wanted_roles );
99  
100 $have->superset( $need );
101 }
102  
103 sub roles {
104 my ( $self, @wanted_roles ) = @_;
105  
106 unless ( $self->config->{authz} ) {
107 Catalyst::Exception->throw(
108 message => 'No authorization configuration defined'
109 );
110 }
111  
112 return $self->_role_search(@wanted_roles);
113 }
114  
115 # optimized join if using DBIC
116 sub _role_search {
117 my ($self, @wanted_roles) = @_;
118 my $cfg = $self->config->{authz};
119 my $role_field = $cfg->{role_field};
120
121 my $search = {
122 $cfg->{role_rel} . '.' . $cfg->{user_role_user_field}
123 => $self->obj->id
124 };
125 $search->{ "me.$role_field" } = { -in => \@wanted_roles } if @wanted_roles;
126  
127 my $rs = $cfg->{role_class}->search(
128 $search,
129 {
130 join => $cfg->{role_rel},
131 columns => [ "me.$role_field" ],
132 }
133 );
134 return map { $_->$role_field } $rs->all;
135 }
136  
137 sub for_session {
138 shift->id;
139 }
140  
141 sub AUTOLOAD {
142 my $self = shift;
143 (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
144 return if $method eq "DESTROY";
145  
146 $self->obj->$method(@_);
147 }
148  
149 1;
150 __END__