blib/lib/Catalyst/Plugin/Authentication/CDBI.pm

filter: Statementstmt 9.5% 8 / 84 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 18.1% 2 / 11 Podpod 100% 9 / 9 totaltotal 11.4% 19 / 166 CC 30 SCAR 40.3
CC
30
Cov
6%
process_roles
90.0
login
72.0
roles
72.0
CRAP 56.1
↑ Summary
1 package Catalyst::Plugin::Authentication::CDBI;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use NEXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.10';
7  
8 =head1 NAME
9  
10 Catalyst::Plugin::Authentication::CDBI - CDBI Authentication for Catalyst
11  
12 =head1 SYNOPSIS
13  
14 use Catalyst 'Authentication::CDBI';
15 __PACKAGE__->config->{authentication} = {
16 user_class => 'PetStore::Model::CDBI::Customer',
17 user_field => 'email',
18 role_class => 'PetStore::Model::CDBI::Role',
19 user_role_class => 'PetStore::Model::CDBI::CustomerRole',
20 user_role_user_field => 'customer'
21 };
22 $c->login( $user, $password );
23 $c->logout;
24 $c->session_login( $user, $password );
25 $c->session_logout;
26 $c->roles(qw/customer admin/);
27  
28 CREATE TABLE customer (
29 id INTEGER PRIMARY KEY,
30 email TEXT,
31 password TEXT
32 );
33  
34 CREATE TABLE role (
35 id INTEGER PRIMARY KEY,
36 name TEXT
37 );
38  
39 CREATE TABLE customer_role (
40 id INTEGER PRIMARY KEY,
41 customer INTEGER REFERENCES customer,
42 role INTEGER REFERENCES role
43 );
44  
45 =head1 DESCRIPTION
46  
47 This plugin allows you to authenticate your web users using database
48 tables accessed through C<Class::DBI> classes.
49  
50 Note that this plugin requires a session plugin such as
51 C<Catalyst::Plugin::Session::FastMmap>.
52  
53 This module is now well past the teatime of it's lifespan, and
54 no new features will be added. For new applications, you probably
55 want to look at L<Catalyst::Plugin::Authentication> and friends
56 instead
57  
58 =head1 CONFIGURATION
59  
60 This plugin is configured by passing an "authentication" hash
61 reference to your application's config method. The following keys are
62 supported:
63  
64 =over 4
65  
66 =item user_class
67  
68 the name of the class that represents a user object (no default)
69  
70 =item user_field
71  
72 the name of the column holding the user identifier (defaults to "C<user>")
73  
74 =item password_field
75  
76 the name of the column holding the user's password (defaults to "C<password>")
77  
78 =item password_hash
79  
80 specifies the hashing method for password values; one of: C<SHA> or
81 C<MD5> (the values are not case-sensitive and the default is empty,
82 i.e. no hashing).
83  
84 =item role_class
85  
86 the name of the role class
87  
88 =item role_field
89  
90 name of the role field
91  
92  
93 =item user_role_class
94  
95  
96 =item user_role_user_field
97  
98 (defaults to "C<uer>")
99  
100 =item user_role_role_field
101  
102 (defaults to "C<role>")
103  
104 =back
105  
106  
107 =head2 METHODS
108  
109 =over 4
110  
111 =item login
112  
113 Attempt to authenticate a user. Takes username/password as arguments,
114  
115 $c->login( $user, $password );
116  
117 The user remains authenticated until end of request. See
118 C<session_login> for persistent login.
119  
120 =cut
121  
122 0 ▶sub login {
Subroutine login: not called CC=8 SCAR=42.8
Pod documented
123 0 my ( $c, $user, $password ) = @_;
124 0 ▶ return 1 if $c->request->{user};
Branch: 1 branch
BranchTrueFalseTotal
if $c->request->{"user"} 0 0 0
125 0 my $user_class = $c->config->{authentication}->{user_class};
126 0 ▶ my $user_field = $c->config->{authentication}->{user_field} || 'user';
Condition0%0/2
$c->config->{'authentication'}{'user_field'} || 'user'
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'user_field'} || 'user'
$c->config->{'authentication'}{'user_field'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'user_field'}
Aresult
11
00
127 my $password_field = $c->config->{authentication}->{password_field}
128 0 ▶ || 'password';
Condition0%0/2
$c->config->{'authentication'}{'password_field'} || 'password'
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'password_field'} || 'password'
$c->config->{'authentication'}{'password_field'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'password_field'}
Aresult
11
00
129 0 ▶ my $password_hash = $c->config->{authentication}->{password_hash} || '';
Condition0%0/2
$c->config->{'authentication'}{'password_hash'} || ''
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'password_hash'} || ''
$c->config->{'authentication'}{'password_hash'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'password_hash'}
Aresult
11
00
130 0 ▶ if ( $password_hash =~ /sha/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($password_hash =~ /sha/i) { } 0 0 0
elsif ($password_hash =~ /md5/i) { } 0 0 0
131 0 require Digest::SHA;
132 0 $password = Digest::SHA::sha1_hex($password);
133 }
134 elsif ( $password_hash =~ /md5/i ) {
135 0 require Digest::MD5;
136 0 $password = Digest::MD5::md5_hex($password);
137 }
138 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my $user_obj = $user_class->search({$user_field, $user, $password_field, $password})->next) 0 0 0
139 my $user_obj=$user_class->search(
140 { $user_field => $user, $password_field => $password }
141 )->next
142 )
143 {
144 0 $c->request->{user} = $user;
145 0 $c->request->{user_id} = $user_obj->id;
146 0 return 1;
147 }
148 0 return 0;
149 }
150  
151 =item logout
152  
153 Log out the user. will not clear the session, so user will still remain
154 logged in at next request unless session_logout is called.
155  
156 =cut
157  
158 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
159 0 my $c = shift;
160 0 $c->request->{user} = undef;
161 0 $c->request->{user_id} = undef;
162 }
163  
164 =item process_permission
165  
166 check for permissions. used by the 'roles' function.
167  
168 =cut
169  
170 0 ▶sub process_permission {
Subroutine process_permission: not called CC=6 SCAR=37.4
Pod documented
171 0 my ( $c, $roles ) = @_;
172 0 ▶ if ($roles) {
Branch: 1 branch
BranchTrueFalseTotal
if ($roles) 0 0 0
173 0 ▶ return 1 if $#$roles < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $roles->$#* < 0 0 0 0
174 0 my $string = join ' ', @$roles;
175 0 ▶ if ( $c->process_roles($roles) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->process_roles($roles)) { } 0 0 0
176 0 ▶ $c->log->debug(qq/Permission granted "$string"/) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
177 }
178 else {
179 0 ▶ $c->log->debug(qq/Permission denied "$string"/) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
180 0 return 0;
181 }
182 }
183 0 return 1;
184 }
185  
186 =item roles
187  
188 Check permissions for roles and return true or false.
189  
190 $c->roles(qw/foo bar/);
191  
192 Returns an arrayref containing the verified roles.
193  
194 my @roles = @{ $c->roles };
195  
196 =cut
197  
198 0 ▶sub roles {
Subroutine roles: not called CC=8 SCAR=42.8
Pod documented
199 0 my $c = shift;
200 0 ▶ $c->{roles} ||= [];
Condition0%0/2
$c->{"roles"} ||= []
l!l
00
MC/DC0%0/1
$c->{"roles"} ||= []
$c->{"roles"}
Truth table0%0/2
A
A= $c->{"roles"}
Aresult
11
00
201 0 ▶ my $roles = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
202 0 ▶ if ( $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 0 0
203 0 my @roles;
204 0 foreach my $role (@$roles) {
205 0 ▶ push @roles, $role unless grep $_ eq $role, @{ $c->{roles} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $role), @{$$c{"roles"};}) 0 0 0
206 }
207 0 ▶ return 1 unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 0 0 0
208 0 ▶ if ( $c->process_permission( \@roles ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->process_permission(\@roles)) { } 0 0 0
209 0 $c->{roles} = [ @{ $c->{roles} }, @roles ];
210 0 return 1;
211 }
212 0 else { return 0 }
213 }
214 0 return $c->{roles};
215 }
216  
217 =item session_login
218  
219 Persistently login the user. The user will remain logged in
220 until he clears the session himself, or session_logout is
221 called.
222  
223 $c->session_login( $user, $password );
224  
225 =cut
226  
227 0 ▶sub session_login {
Subroutine session_login: not called CC=2 SCAR=17.9
Pod documented
228 0 my ( $c, $user, $password ) = @_;
229 0 ▶ return 0 unless $c->login( $user, $password );
Branch: 1 branch
BranchTrueFalseTotal
unless $c->login($user, $password) 0 0 0
230 0 $c->session->{user} = $c->req->{user};
231 0 $c->session->{user_id} = $c->req->{user_id};
232 0 return 1;
233 }
234  
235 =item session_logout
236  
237 Session logout. will delete the user object from the session.
238  
239 =cut
240  
241 0 ▶sub session_logout {
Subroutine session_logout: not called CC=1 SCAR=6.9
Pod documented
242 0 my $c = shift;
243 0 $c->logout;
244 0 $c->session->{user} = undef;
245 0 $c->session->{user_id} = undef;
246 }
247  
248 =back
249  
250 =head2 EXTENDED METHODS
251  
252 =over 4
253  
254 =item prepare_action
255  
256 sets $c->request->{user} from session.
257  
258 =cut
259  
260 0 ▶sub prepare_action {
Subroutine prepare_action: not called CC=1 SCAR=6.9
Pod documented
261 0 my $c = shift;
262 0 $c->NEXT::prepare_action(@_);
263 0 $c->request->{user} = $c->session->{user};
264 0 $c->request->{user_id} = $c->session->{user_id};
265 }
266  
267 =item setup
268  
269 sets up $c->config->{authentication}.
270  
271 =cut
272  
273 0 ▶sub setup {
Subroutine setup: not called CC=2 SCAR=17.9
Pod documented
274 0 my $c = shift;
275 0 my $conf = $c->config->{authentication};
276 0 ▶ $conf = ref $conf eq 'ARRAY' ? {@$conf} : $conf;
Branch: 1 branch
BranchTrueFalseTotal
ref $conf eq 'ARRAY' ? : 0 0 0
277 0 $c->config->{authentication} = $conf;
278 0 return $c->NEXT::setup(@_);
279 }
280  
281 =back
282  
283 =head2 OVERLOADED METHODS
284  
285 =over 4
286  
287 =item process_roles
288  
289 Takes an arrayref of roles and checks if user has the supplied roles.
290 Returns 1/0.
291  
292 =cut
293  
294 0 ▶sub process_roles {
Subroutine process_roles: not called CC=9 SCAR=45.0
Pod documented
295 0 my ( $c, $roles ) = @_;
296 0 my $user_class = $c->config->{authentication}->{user_class};
297 0 ▶ my $user_field = $c->config->{authentication}->{user_field} || 'user';
Condition0%0/2
$c->config->{'authentication'}{'user_field'} || 'user'
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'user_field'} || 'user'
$c->config->{'authentication'}{'user_field'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'user_field'}
Aresult
11
00
298 0 my $role_class = $c->config->{authentication}->{role_class};
299 0 ▶ my $role_field = $c->config->{authentication}->{role_field} || 'name';
Condition0%0/2
$c->config->{'authentication'}{'role_field'} || 'name'
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'role_field'} || 'name'
$c->config->{'authentication'}{'role_field'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'role_field'}
Aresult
11
00
300 0 my $user_role_class = $c->config->{authentication}->{user_role_class};
301 my $user_role_user_field =
302 0 ▶ $c->config->{authentication}->{user_role_user_field} || 'user';
Condition0%0/2
$c->config->{'authentication'}{'user_role_user_field'} || 'user'
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'user_role_user_field'} || 'user'
$c->config->{'authentication'}{'user_role_user_field'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'user_role_user_field'}
Aresult
11
00
303 my $user_role_role_field =
304 0 ▶ $c->config->{authentication}->{user_role_role_field} || 'role';
Condition0%0/2
$c->config->{'authentication'}{'user_role_role_field'} || 'role'
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'user_role_role_field'} || 'role'
$c->config->{'authentication'}{'user_role_role_field'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'user_role_role_field'}
Aresult
11
00
305  
306 0 ▶ if ( my $user =
Branch: 1 branch
BranchTrueFalseTotal
if (my $user = $user_class->search({$user_field, $c->request->{'user'}})->first) { } 0 0 0
307 $user_class->search( { $user_field => $c->request->{user} } )->first )
308 {
309 0 for my $role (@$roles) {
310 0 ▶ if ( my $role =
Branch: 1 branch
BranchTrueFalseTotal
if (my $role = $role_class->search({$role_field, $role})->first) { } 0 0 0
311 $role_class->search( { $role_field => $role } )->first )
312 {
313 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $user_role_class->search({$user_role_user_field, $user->id, $user_role_role_field, $role->id}) 0 0 0
314 unless $user_role_class->search(
315 {
316 $user_role_user_field => $user->id,
317 $user_role_role_field => $role->id
318 }
319 );
320 }
321 0 else { return 0 }
322 }
323 }
324 0 else { return 0 }
325 0 return 1;
326 }
327  
328 =back
329  
330  
331 =head1 SEE ALSO
332  
333 L<Catalyst>, L<Catalyst::Plugin::Session::FastMmap>
334  
335 =head1 AUTHOR
336  
337 Sebastian Riedel <sri@cpan.org>,
338 Marcus Ramberg <mramberg@cpan.org>,
339 Andrew Ford <a.ford@ford-mason.co.uk>
340  
341 =head1 COPYRIGHT
342  
343 This program is free software, you can redistribute it and/or modify it under
344 the same terms as Perl itself.
345  
346 =cut
347  
348 1 1;
↑ Summary