File Coverage

blib/lib/CAS/Config.pm
Criterion Covered Total %
statement 41 61 67.2
branch 8 24 33.3
condition 9 19 47.3
subroutine 8 8 100.0
pod 0 2 0.0
total 66 114 57.8


line stmt bran cond sub pod time code
1             package CAS::Config;
2              
3 6     6   27253 use strict;
  6         14  
  6         330  
4              
5             =head1 NAME
6              
7             CAS::Config - Load the core configuration data and conect to the database.
8              
9             =head1 VERSION
10              
11             Version 0.40
12              
13             =cut
14              
15             our $VERSION = '0.40';
16              
17             =head1 SYNOPSIS
18              
19              
20             use CAS::Config;
21             my $HR_config = CAS::Config->load({CLIENT_ID => n});
22              
23             This module isn't intended for direct use. It returns a hashref which is used
24             as the common core data structure for all other CAS modules.
25              
26             =cut
27              
28 6     6   5280 use YAML;
  6         88556  
  6         440  
29 6     6   9395 use CAS::DB;
  6         19  
  6         375  
30 6     6   50 use Carp qw(cluck confess croak carp);
  6         14  
  6         4996  
31              
32              
33             =head1 load
34              
35             Load a CAS client. This function returns a hashref containing all the basic
36             data from the config file and the indicated client, except the admin password.
37             This function is not intended to be called directly, but rather is the core
38             data structure blessed as a CAS client object.
39              
40             PARAMETERS:
41              
42             One of the following three arguments must be provided. If more than one is
43             available, they are selected in the order listed here.
44              
45             CLIENT_ID: The database ID of the client which is seeking to connect to
46             CAS.
47              
48             CLIENT_NAME: The name of the client which is seeking to connect to
49             CAS.
50              
51             CLIENT_DOMAIN: The domain of the client which is seeking to connect to
52             CAS.
53             listed.
54              
55              
56             OPTIONS:
57              
58             CONFIG: Alternate configuration file. Defaults to '/etc/CAS.yaml'.
59              
60             =cut
61             sub load {
62             # though we don't use $class
63 8     8 0 44 my $proto = shift;
64 8   33     59 my $class = ref($proto) || $proto;
65            
66            
67 8         18 my $HR_params = shift;
68 8 50       37 croak("Parameters not passed as a hashref")
69             unless ref($HR_params) eq 'HASH';
70            
71 8 50 100     57 croak("No client key provided")
      66        
72             unless defined $HR_params->{CLIENT_ID} || $HR_params->{CLIENT_NAME}
73             || $HR_params->{CLIENT_DOMAIN};
74            
75             # see if conf specified in case we're in make test
76 8   50     46 my $conf_file = $HR_params->{CONFIG} || '/etc/CAS.yaml';
77 8         40 my $HR_config = _load_config($conf_file);
78            
79            
80             # allow caller to overide default
81 8         37 $HR_config->{debug} = $HR_config->{DEBUG}; # user-fetchable from config
82 8 50       59 $HR_config->{debug} = $HR_params->{debug} if defined $HR_params->{debug};
83 8 50       39 $^W++ if $HR_config->{debug};
84 8 50 0     49 require diagnostics && import diagnostics
      33        
85             if $HR_config->{debug} && $HR_config->{debug} > 2;
86            
87            
88             # connect to db
89 8         41 $HR_config->{cas_db_connect} = cas_db_connect($HR_config);
90 8         19 my $dbh = &{$HR_config->{cas_db_connect}};
  8         26  
91 0 0       0 warn "dbh = $dbh" if $HR_config->{debug} >= 2;
92 0         0 $HR_config->{dbh} = $dbh;
93            
94             # We don't need anyone interogating the config for the password,
95             # and we're done with it now
96 0         0 delete $HR_config->{DB_PASSWD};
97            
98            
99 0         0 my $HR_client = $dbh->client_info($HR_params);
100 0 0       0 die 'Problem getting Client data: ' . $dbh->errstr
101             unless ref $HR_client eq 'HASH';
102             # $HR_config->{client} is now deprecated and will be removed soon
103 0         0 $HR_config->{client} = $HR_client;
104 0         0 foreach my $field (keys %{$HR_client}) {
  0         0  
105 0         0 $HR_config->{lc($field)} = $HR_client->{$field};
106             } # for each bit'o'client data
107            
108            
109             # get user info table fields - will it get used a lot? Should we get the
110             # client tables as well then? Should the field type be a value?
111 0         0 foreach my $field (@{$dbh->selectcol_arrayref("DESC UserInfo")}) {
  0         0  
112 0         0 $HR_config->{user_info_fields}{$field} = 1;
113             } # foreach field in the UserInfo table
114            
115             # if this client has a suplimental user table load that too
116 0 0       0 if (defined $HR_config->{client}{Supplemental_User_Table}) {
117 0         0 foreach my $field (@{$dbh->selectcol_arrayref("DESC
  0         0  
118             $HR_config->{client}{Supplemental_User_Table}")}) {
119 0         0 $HR_config->{supl_user_info_fields}{$field} = 1;
120             } # foreach feild
121             } # if Supplemental_User_Table
122            
123             # This assumes CAS will always be Client 1 - that should be in the docs
124 0         0 $HR_config->{admin_email} = $dbh->selectrow_array("SELECT Email
125             FROM Clients, UserInfo WHERE Clients.ID = 1 AND UserInfo.ID = Admin");
126 0 0       0 die('Problem getting Admin email: ' . $dbh->errstr)
127             if $dbh->err;
128            
129 0         0 return $HR_config;
130             } # load
131              
132              
133             sub cas_db_connect {
134             # should also work as $self if called in OOP mode, since this is the core
135             # hashref blessed
136 8     8 0 22 my $HR_config = shift;
137 8         26 my $password = $HR_config->{DB_PASSWD};
138            
139             return sub {
140 8     8   22 my $dbh = '';
141 8         21 eval { $dbh = CAS::DB->connectDB({user => $HR_config->{DB_USER},
  8         138  
142             password => $password, host => $HR_config->{DB_HOST},
143             debug => $HR_config->{debug},
144             database => $HR_config->{DB_DATABASE}}) };
145 8 50       13891 die "Problem connecting to database: $@" if $@;
146 0         0 return $dbh;
147             } # annonymous connection sub
148 8         73 } # cas_db_connect
149              
150              
151             sub _load_config {
152 8   50 8   42 my $conf_file = shift || die "conf_file not passed";
153            
154 8         35 local $/ = undef; # slurpy
155 8 50       619 open(YAML,$conf_file) or die "Couldn't open CAS config file $conf_file: $!";
156 8         232 my $yaml_in = ;
157 8 50       125 close YAML or warn("YAML didn't close preoperly: $!");
158            
159 8         50 my $HR_config = Load($yaml_in);
160 8         185974 $HR_config->{conf_file} = $conf_file;
161            
162 8         78 return $HR_config;
163             } # _load_config
164              
165             =head1 AUTHOR
166              
167             Sean P. Quinlan, C<< >>
168              
169             =head1 BUGS
170              
171             Please report any bugs or feature requests to
172             C, or through the web interface at
173             L.
174             I will be notified, and then you'll automatically be notified of progress on
175             your bug as I make changes.
176              
177             =head1 SUPPORT
178              
179             You can find documentation for this module with the perldoc command.
180              
181             perldoc CAS
182              
183             You can also look for information at:
184              
185             =over 4
186              
187             =item * AnnoCPAN: Annotated CPAN documentation
188              
189             L
190              
191             =item * CPAN Ratings
192              
193             L
194              
195             =item * RT: CPAN's request tracker
196              
197             L
198              
199             =item * Search CPAN
200              
201             L
202              
203             =back
204              
205             =head1 ACKNOWLEDGEMENTS
206              
207             =head1 COPYRIGHT & LICENSE
208              
209             Copyright 2006 Sean P. Quinlan, all rights reserved.
210              
211             This program is free software; you can redistribute it and/or modify it
212             under the same terms as Perl itself.
213              
214             =cut
215              
216             1; # End of CAS::Config