Wombat/Realm/DBIRealm.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 202 CC 13 SCAR 23.5
CC
13
Cov
0%
authenticate
42.0
open
12.0
start
12.0
CRAP 10.5
1 # -*- Mode: Perl; indent-tabs-mode: nil; -*-
2  
3 package Wombat::Realm::DBIRealm;
4  
5 =pod
6  
7 =head1 NAME
8  
9 Wombat::Realm::DBIRealm - internal realm base clas
10  
11 =head1 SYNOPSIS
12  
13 =head1 DESCRIPTION
14  
15 Implementation of B<Wombat::Realm> that works with any DBI supported
16 database.
17  
18 =cut
19  
20 use base qw(Wombat::Realm::RealmBase);
21 use fields qw(connectionName connectionPassword connectionURL dbConnection);
22 use fields qw(driverName preparedCredentials preparedRoles roleNameCol);
23 use fields qw(userCredCol userNameCol userRoleTable userTable);
24 use strict;
25 use warnings;
26  
27 use DBI ();
28 use Wombat::Exception ();
29 use Wombat::Realm::GenericPrincipal ();
30  
31 use constant name => 'DBIRealm';
32  
33 =pod
34  
35 =head1 CONSTRUCTOR
36  
37 =over
38  
39 =item new()
40  
41 Construct and return a B<Wombat::Realm::RealmBase> instance,
42 initializing fields appropriately. If subclasses override the
43 constructor, they must be sure to call
44  
45 $self->SUPER::new();
46  
47 =back
48  
49 =cut
50  
51 sub new {
52 my $self = shift;
53  
54 $self = fields::new($self) unless ref $self;
55 $self->SUPER::new();
56  
57 $self->{connectionName} = undef;
58 $self->{connectionPassword} = undef;
59 $self->{connectionURL} = undef;
60 $self->{dbConnection} = undef;
61 $self->{driverName} = undef;
62 $self->{roleNameCol} = undef;
63 $self->{userCredCol} = undef;
64 $self->{userNameCol} = undef;
65 $self->{userRoleTable} = undef;
66 $self->{userTable} = undef;
67  
68 return $self;
69 }
70  
71 =pod
72  
73 =head1 ACCESSOR METHODS
74  
75 =over
76  
77 =item setConnectionName($connectionName)
78  
79 Set the username to use to connect to the database.
80  
81 B<Parameters:>
82  
83 =over
84  
85 =item $connectionName
86  
87 the database username
88  
89 =back
90  
91 =cut
92  
93 sub setConnectionName {
94 my $self = shift;
95 my $connectionName = shift;
96  
97 $self->{connectionName} = $connectionName;
98  
99 return 1;
100 }
101  
102 =pod
103  
104 =item setConnectionPassword($connectionPassword)
105  
106 Set the password to use to connect to the database.
107  
108 B<Parameters:>
109  
110 =over
111  
112 =item $connectionPassword
113  
114 the database password
115  
116 =back
117  
118 =cut
119  
120 sub setConnectionPassword {
121 my $self = shift;
122 my $connectionPassword = shift;
123  
124 $self->{connectionPassword} = $connectionPassword;
125  
126 return 1;
127 }
128  
129 =pod
130  
131 =item setConnectionURL($connectionURL)
132  
133 Set the URL to use to connect to the database. The URL is the part of
134 the DBI data source after the driver name. In this example
135  
136 DBI:mysql:hostname=localhost;port=12345;database=hi
137  
138 the URL is
139  
140 hostname=localhost;port=12345;database=hi
141  
142 B<Parameters:>
143  
144 =over
145  
146 =item $connectionURL
147  
148 the database URL
149  
150 =back
151  
152 =cut
153  
154 sub setConnectionURL {
155 my $self = shift;
156 my $connectionURL = shift;
157  
158 $self->{connectionURL} = $connectionURL;
159  
160 return 1;
161 }
162  
163 =pod
164  
165 =item setDriverName($driverName)
166  
167 Set the DBI driver to use.
168  
169 B<Parameters:>
170  
171 =over
172  
173 =item $driverName
174  
175 the DBI driver name
176  
177 =back
178  
179 =cut
180  
181 sub setDriverName {
182 my $self = shift;
183 my $driverName = shift;
184  
185 $self->{driverName} = $driverName;
186  
187 return 1;
188 }
189  
190 =pod
191  
192 =item setRoleNameCol($roleNameCol)
193  
194 Set the column in the user role table that names a role.
195  
196 B<Parameters:>
197  
198 =over
199  
200 =item $roleNameCol
201  
202 the column name
203  
204 =back
205  
206 =cut
207  
208 sub setRoleNameCol {
209 my $self = shift;
210 my $roleNameCol = shift;
211  
212 $self->{roleNameCol} = $roleNameCol;
213  
214 return 1;
215 }
216  
217 =pod
218  
219 =item setUserCredCol($userCredCol)
220  
221 Set the column in the user table that holds the user's credentials.
222  
223 B<Parameters:>
224  
225 =over
226  
227 =item $userCredCol
228  
229 the column name
230  
231 =back
232  
233 =cut
234  
235 sub setUserCredCol {
236 my $self = shift;
237 my $userCredCol = shift;
238  
239 $self->{userCredCol} = $userCredCol;
240  
241 return 1;
242 }
243  
244 =pod
245  
246 =item setUserNameCol($userNameCol)
247  
248 Set the column in the user table that holds the user's name.
249  
250 B<Parameters:>
251  
252 =over
253  
254 =item $userNameCol
255  
256 the column name
257  
258 =back
259  
260 =cut
261  
262 sub setUserNameCol {
263 my $self = shift;
264 my $userNameCol = shift;
265  
266 $self->{userNameCol} = $userNameCol;
267  
268 return 1;
269 }
270  
271 =pod
272  
273 =item setUserRoleTable($userRoleTable)
274  
275 Set the table that holds the relation between users and roles.
276  
277 B<Parameters:>
278  
279 =over
280  
281 =item $userRoleTable
282  
283 the table name
284  
285 =back
286  
287 =cut
288  
289 sub setUserRoleTable {
290 my $self = shift;
291 my $userRoleTable = shift;
292  
293 $self->{userRoleTable} = $userRoleTable;
294  
295 return 1;
296 }
297  
298 =item setUserTable($userTable)
299  
300 Set the table that holds user data.
301  
302 B<Parameters:>
303  
304 =over
305  
306 =item $userTable
307  
308 the table name
309  
310 =back
311  
312 =cut
313  
314 sub setUserTable {
315 my $self = shift;
316 my $userTable = shift;
317  
318 $self->{userTable} = $userTable;
319  
320 return 1;
321 }
322  
323 =pod
324  
325 =back
326  
327 =head1 PUBLIC METHODS
328  
329 =over
330  
331 =item authenticate ($username, $credentials)
332  
333 Return the Principal associated with the specified username and
334 credentials, if there is one, or C<undef> otherwise.
335  
336 If there are any errors with the DBI connection, executing the query
337 or anything else, do not authenticate and return C<undef>. This event
338 is also logged, and the connection will be closed so that a subsequent
339 request will automatically re-open it.
340  
341 B<Parameters>
342  
343 =over
344  
345 =item $username
346  
347 username of the principal to be looked up
348  
349 =item $credentials
350  
351 password or other credentials to use in authenticating this username
352  
353 =back
354  
355 =cut
356  
357 sub authenticate {
358 my $self = shift;
359 my $username = shift;
360 my $credentials = shift;
361  
362 my $principal;
363 eval {
364 $self->open();
365  
366 unless ($self->{preparedCredentials}) {
367 my $str = sprintf("SELECT %s FROM %s WHERE %s = ?",
368 $self->{userCredCol},
369 $self->{userTable},
370 $self->{userNameCol});
371 $self->{preparedCredentials} =
372 $self->{dbConnection}->prepare($str);
373 }
374  
375 my $sth = $self->{preparedCredentials};
376 my $rs = $self->{dbConnection}->selectcol_arrayref($sth, {},
377 $username);
378  
379 my $password = $rs->[0];
380 return undef unless defined $password;
381  
382 return undef unless $self->digest($credentials) eq $password;
383  
384 unless ($self->{preparedRoles}) {
385 my $str = sprintf("SELECT %s FROM %s WHERE %s = ?",
386 $self->{roleNameCol},
387 $self->{userRoleTable},
388 $self->{userNameCol});
389 $self->{preparedRoles} = $self->{dbConnection}->prepare($str);
390 }
391  
392 $sth = $self->{preparedRoles};
393 $rs = $self->{dbConnection}->selectcol_arrayref($sth, {}, $username);
394  
395 $principal = Wombat::Realm::GenericPrincipal->new($self,
396 $username,
397 $credentials,
398 $rs);
399 };
400 if ($@) {
401 $self->log("DBIRealm database exception", $@, 'ERROR');
402  
403 # close the connection so that it gets reopened next time
404 $self->close();
405  
406 return undef;
407 }
408  
409 return $principal;
410 }
411  
412 =pod
413  
414 =back
415  
416 =head1 PACKAGE METHODS
417  
418 =over
419  
420 =item close()
421  
422 Close the database connection.
423  
424 =cut
425  
426 sub close {
427 my $self = shift;
428  
429 return 1 unless $self->{dbConnection};
430  
431 $self->{dbConnection}->disconnect();
432 delete $self->{dbConnection};
433  
434 return 1;
435 }
436  
437 =pod
438  
439 =item getName()
440  
441 Return a short name for this Realm implementation.
442  
443 =cut
444  
445 sub getName {
446 my $self = shift;
447  
448 return name;
449 }
450  
451 =pod
452  
453 =item open()
454  
455 Open the database connection.
456  
457 =cut
458  
459 sub open {
460 my $self = shift;
461  
462 return 1 if $self->{dbConnection};
463  
464 my $dsn = join ':', 'DBI', $self->{driverName}, $self->{connectionURL};
465 $self->{dbConnection} = DBI->connect($dsn,
466 $self->{connectionName},
467 $self->{connectionPassword},
468 {RaiseError => 1,
469 PrintError => 0});
470 die $DBI::errstr unless $self->{dbConnection};
471  
472 return 1;
473 }
474  
475 =pod
476  
477 =back
478  
479 =head1 LIFECYCLE METHODS
480  
481 =over
482  
483 =item start()
484  
485 Prepare for active use of this Realm, opening the database
486 connection. This method should be called before any of the public
487 methods of the Realm are utilized.
488  
489 B<Throws:>
490  
491 =over
492  
493 =item B<Servlet::Util::Exception>
494  
495 if the Realm has already been started
496  
497 =back
498  
499 =cut
500  
501 sub start {
502 my $self = shift;
503  
504 eval {
505 $self->open();
506 };
507 if ($@) {
508 $self->{dbConnection}->disconnect() if $self->{dbConnection};
509  
510 my $msg = "DBIRealm: database connection error: $@";
511 Wombat::LifecycleException->throw($msg);
512 }
513  
514 $self->SUPER::start();
515  
516 return 1;
517 }
518  
519 =pod
520  
521 =item stop()
522  
523 Gracefully terminate active use of this Realm, closing the database
524 connection. Once this method has been called, no public methods of the
525 Realm should be utilized.
526  
527 B<Throws:>
528  
529 =over
530  
531 =item B<Servlet::Util::Exception>
532  
533 if the Realm is not started
534  
535 =back
536  
537 =cut
538  
539 sub stop {
540 my $self = shift;
541  
542 $self->SUPER::stop();
543  
544 eval {
545 $self->close();
546 };
547 if ($@) {
548 my $msg = "DBIRealm: database disconnection error: $@";
549 Wombat::LifecycleException->throw($msg);
550 }
551  
552 return 1;
553 }
554  
555 1;
556 __END__