blib/lib/VUser/Email/Postfix/SQL.pm untested

filter: Statementstmt 0.0% 0 / 250 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 372 CC 24 SCAR 30.1
CC
24
Cov
0%
email_mod
72.0
email_add
30.0
user_exists
12.0
CRAP 20.3
1 package VUser::Email::Postfix::SQL;
2 use warnings;
3 use strict;
4  
5 # Copyright (c) 2006 Randy Smith <perlstalker@vuser.org>
6 # $Id: SQL.pm,v 1.7 2007/09/21 14:24:25 perlstalker Exp $
7  
8 use VUser::Log qw(:levels);
9 use VUser::ExtLib qw(:config);
10 use VUser::ExtLib::SQL;
11 use VUser::Email qw(:utils);
12 use VUser::ResultSet;
13 use VUser::Meta;
14  
15 our $VERSION = '0.1.1';
16  
17 our $log;
18 our %meta;
19 our $extlib;
20 our $c_sec = 'Extension Email::Postfix::SQL';
21  
22 sub c_sec { return $c_sec; }
23 sub depends { qw(Email::Postfix); }
24  
25 sub init {
26 my $eh = shift;
27 my %cfg = @_;
28  
29 %meta = %VUser::Email::meta;
30
31 if (defined $main::log) {
32 $log = $main::log;
33 } else {
34 $log = VUser::Log->new(\%cfg, 'vuser')
35 }
36
37 $extlib = VUser::ExtLib::SQL->new(\%cfg,
38 {'dsn' => $cfg{$c_sec}{'dsn'},
39 'user' => $cfg{$c_sec}{'user'},
40 'password' => $cfg{$c_sec}{'password'},
41 'macros' => { 'a' => 'account',
42 'p' => 'password',
43 'd' => 'domain',
44 'n' => 'name',
45 'q' => 'quota',
46 'h' => 'home',
47 'd' => 'domain'
48 }
49 }
50 );
51
52 $eh->register_task('email', 'add', \&email_add);
53 $eh->register_task('email', 'mod', \&email_mod);
54 $eh->register_task('email', 'del', \&email_del);
55 $eh->register_task('email', 'info', \&email_info);
56 $eh->register_task('email', 'list', \&email_list);
57  
58 $eh->register_task('domain', 'add', \&domain_add);
59 $eh->register_task('domain', 'mod', \&domain_mod);
60 $eh->register_task('domain', 'del', \&domain_del);
61 $eh->register_task('domain', 'info', \&domain_info);
62 $eh->register_task('domain', 'list', \&domain_list);
63 }
64  
65 sub user_exists {
66 my $cfg = shift;
67 my $opts = shift;
68 my $account = shift;
69  
70 my ($user, $domain);
71 VUser::Email::split_address($cfg, $opts->{account}, \$user, \$domain);
72 my $params = { 'u' => $user, 'd', $domain };
73  
74 my $sql = strip_ws($cfg->{$c_sec}{userinfo_query});
75 my $sth;
76 eval { $sth = $extlib->execute($opts, $sql, $params); };
77 if ($@) {
78 $log->log(LOG_ERROR, "Unable to get user info: $@");
79 die "Unable to get user info: $@";
80 }
81  
82 my $found;
83 my @results;
84 if (@results = $sth->fetchrow_array()) {
85 $found = 1;
86 $log->log(LOG_DEBUG, "User $account exists");
87 } else {
88 $found = 0;
89 $log->log(LOG_DEBUG, "User $account does not exist");
90 }
91  
92 $sth->finish;
93 return $found;
94 }
95  
96 sub email_add {
97 my ($cfg, $opts, $action, $eh) = @_;
98  
99 my $account = $opts->{account};
100 if ($cfg->{$VUser::Email::c_sec}{"lc user"}) {
101 $account = lc($account);
102 }
103
104 my ($user, $domain);
105 VUser::Email::split_address($cfg, $account, \$user, \$domain);
106  
107 if (user_exists($cfg, $opts, $account)) {
108 return;
109 }
110  
111 if (not defined $opts->{home} or not $opts->{home}) {
112 $opts->{"home"} = get_home_directory($cfg, $user, $domain);
113 }
114  
115 if (not defined $opts->{quota}) {
116 $opts->{quota} = strip_ws($cfg->{$VUser::Email::c_sec}{'default quota'});
117 }
118
119 my $sql = strip_ws($cfg->{$c_sec}{'useradd_query'});
120 my $sth = $extlib->execute($opts, $sql, {'u' => $user, 'd' => $domain});
121 return undef;
122 }
123  
124 sub email_mod {
125 my ($cfg, $opts, $action, $eh) = @_;
126
127 my @sql = ();
128
129 my ($user, $domain);
130 VUser::Email::split_address($cfg, $opts->{account}, \$user, \$domain);
131
132 my $params = { 'u' => $user, 'd' => $domain };
133
134 if ($opts->{home} and $cfg->{$c_sec}{usermod_home_query}) {
135 push @sql, strip_ws($cfg->{$c_sec}{usermod_home_query});
136 }
137
138 if ($opts->{password} and $cfg->{$c_sec}{usermod_password_query}) {
139 push @sql, strip_ws($cfg->{$c_sec}{usermod_password_query})
140 }
141
142 if ($opts->{name} and $cfg->{$c_sec}{usermod_name_query}) {
143 push @sql, strip_ws($cfg->{$c_sec}{usermod_name_query});
144 }
145
146 if (defined $opts->{quota} and $cfg->{$c_sec}{usermod_quota_query}) {
147 push @sql, strip_ws($cfg->{$c_sec}{usermod_quota_query});
148 }
149
150 if ($opts->{newaccount} and $cfg->{$c_sec}{usermod_account_query}) {
151 # change the home dir to match the username unless --home was passed
152 if (not $opts->{home} and $cfg->{$c_sec}{usermod_quota_query}) {
153 my ($new_user, $new_domain);
154 VUser::Email::split_address ($cfg, $opts->{newaccount},
155 \$new_user, \$new_domain);
156 $opts->{home} = VUser::Email::get_home_directory($cfg,
157 $new_user,
158 $new_domain);
159 push @sql, strip_ws($cfg->{$c_sec}{usermod_home_query});
160 }
161  
162 # Update the user name
163 push @sql, strip_ws($cfg->{$c_sec}{usermod_account_query});
164 }
165
166 #$extlib->begin();
167 foreach my $sql (@sql) {
168 eval { $extlib->execute($opts, $sql, $params); };
169 if ($@) {
170 $log->log(LOG_ERROR, "Update error, rolling back changes.");
171 $log->log(LOG_DEBUG, "Update error: $@");
172 $extlib->rollback();
173 }
174 }
175 #$extlib->commit();
176 }
177  
178 sub email_del {
179 my ($cfg, $opts, $action, $eh) = @_;
180
181 my ($user, $domain);
182 VUser::Email::split_address($cfg, $opts->{account}, \$user, \$domain);
183
184 my $params = { 'u' => $user, 'd' => $domain };
185
186 my $sql = strip_ws($cfg->{$c_sec}{userdel_query});
187 eval { $extlib->execute($opts, $sql, $params); };
188 if ($@) {
189 $log->log(LOG_ERROR, "Unable to delete user: $@");
190 die "Unable to delete user: $@";
191 }
192 }
193  
194 sub email_info {
195 my ($cfg, $opts, $action, $eh) = @_;
196
197 my ($user, $domain);
198 VUser::Email::split_address($cfg, $opts->{account}, \$user, \$domain);
199 my $params = { 'u' => $user, 'd', $domain };
200
201 my $sql = strip_ws($cfg->{$c_sec}{userinfo_query});
202 my $sth;
203 eval { $sth = $extlib->execute($opts, $sql, $params); };
204 if ($@) {
205 $log->log(LOG_ERROR, "Unable to get user info: $@");
206 die "Unable to get user info: $@";
207 }
208
209 my $rs = VUser::ResultSet->new();
210 $rs->add_meta($meta{'account'});
211 $rs->add_meta($meta{'password'});
212 $rs->add_meta($meta{'name'});
213 $rs->add_meta($meta{'home'});
214 $rs->add_meta($meta{'quota'});
215
216 my @results;
217 while (@results = $sth->fetchrow_array) {
218 $rs->add_data( [@results[0 .. 4]] );
219 }
220 $sth->finish;
221 return $rs;
222 }
223  
224 sub email_list {
225 my ($cfg, $opts, $action, $eh) = @_;
226
227 my ($user, $domain);
228 VUser::Email::split_address($cfg, $opts->{account}, \$user, \$domain);
229 my $params = { 'u' => $user, 'd', $domain };
230
231 my $sql = strip_ws($cfg->{$c_sec}{userlist_query});
232 my $sth;
233 eval { $sth = $extlib->execute($opts, $sql, $params); };
234 if ($@) {
235 $log->log(LOG_ERROR, "Unable to get user info: $@");
236 die "Unable to get user info: $@";
237 }
238
239 my $rs = VUser::ResultSet->new();
240 $rs->add_meta($meta{'account'});
241 $rs->add_meta($meta{'password'});
242 $rs->add_meta($meta{'name'});
243 $rs->add_meta($meta{'home'});
244 $rs->add_meta($meta{'quota'});
245
246 my @results;
247 while (@results = $sth->fetchrow_array) {
248 $rs->add_data( [@results[0 .. 4]] );
249 }
250 $sth->finish;
251 return $rs;
252 }
253  
254 sub domain_add {
255 my ($cfg, $opts, $action, $eh) = @_;
256
257 my $params = { 'd', $opts->{domain} };
258
259 my $sql = strip_ws($cfg->{$c_sec}{domainadd_query});
260 my $sth;
261 eval { $sth = $extlib->execute($opts, $sql, $params); };
262 if ($@) {
263 $log->log(LOG_ERROR, "Unable to add domain: $@");
264 die "Unable to add domain info: $@";
265 }
266 }
267  
268 sub domain_mod {
269 my ($cfg, $opts, $action, $eh) = @_;
270
271 my $params = { 'd' => $opts->{domain} };
272
273 my @sql = ();
274 if ($opts->{newdomain} and $cfg->{$c_sec}{domainmod_domain_query}) {
275 push @sql, strip_ws($cfg->{$c_sec}{domainmod_domain_query});
276 }
277
278 $extlib->begin();
279 foreach my $sql (@sql) {
280 eval { $extlib->execute($opts, $sql, $params); };
281 if ($@) {
282 $log->log(LOG_ERROR, "Update error, rolling back changes.");
283 $log->log(LOG_DEBUG, "Update error: $@");
284 $extlib->rollback();
285 }
286 }
287 $extlib->commit();
288 }
289  
290 sub domain_del {
291 my ($cfg, $opts, $action, $eh) = @_;
292
293 my $params = { 'd', $opts->{domain} };
294
295 my $sql = strip_ws($cfg->{$c_sec}{domainadel_query});
296 my $sth;
297 eval { $sth = $extlib->execute($opts, $sql, $params); };
298 if ($@) {
299 $log->log(LOG_ERROR, "Unable to delete domain: $@");
300 die "Unable to delete domain info: $@";
301 }
302 }
303  
304 sub domain_list {
305 my ($cfg, $opts, $action, $eh) = @_;
306
307 my $params = { 'd', $opts->{domain} };
308
309 my $sql = strip_ws($cfg->{$c_sec}{domainlist_query});
310 my $sth;
311 eval { $sth = $extlib->execute($opts, $sql, $params); };
312 if ($@) {
313 $log->log(LOG_ERROR, "Unable to get the domain list: $@");
314 die "Unable to get the domain list: $@";
315 }
316
317 my $rs = VUser::ResultSet->new();
318 $rs->add_meta($meta{'domain'});
319
320 my @result;
321 while (@result = $sth->fetchrow_array) {
322 $rs->add_data([$result[0]]);
323 }
324
325 return $rs;
326 }
327  
328 sub domain_info {
329 my ($cfg, $opts, $action, $eh) = @_;
330
331 my $params = { 'd', $opts->{domain} };
332
333 my $sql = strip_ws($cfg->{$c_sec}{domaininfo_query});
334 my $sth;
335 eval { $sth = $extlib->execute($opts, $sql, $params); };
336 if ($@) {
337 $log->log(LOG_ERROR, "Unable to get domain info: $@");
338 die "Unable to get domain info: $@";
339 }
340
341 my $rs = VUser::ResultSet->new();
342 $rs->add_meta($meta{'domain'});
343
344 my @result;
345 while (@result = $sth->fetchrow_array) {
346 $rs->add_data([$result[0]]);
347 }
348
349 return $rs;
350 }
351  
352 1;
353  
354 __END__