more for the new world of export...
[freeside.git] / FS / FS / part_export.pm
1 package FS::part_export;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::part_svc;
7 use FS::part_export_option;
8
9 @ISA = qw(FS::Record);
10
11 =head1 NAME
12
13 FS::part_export - Object methods for part_export records
14
15 =head1 SYNOPSIS
16
17   use FS::part_export;
18
19   $record = new FS::part_export \%hash;
20   $record = new FS::part_export { 'column' => 'value' };
21
22   ($new_record, $options) = $template_recored->clone( $svcpart );
23
24   $error = $record->insert( { 'option' => 'value' } );
25   $error = $record->insert( \%options );
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33 =head1 DESCRIPTION
34
35 An FS::part_export object represents an export of Freeside data to an external
36 provisioning system.  FS::part_export inherits from FS::Record.  The following
37 fields are currently supported:
38
39 =over 4
40
41 =item exportnum - primary key
42
43 =item svcpart - Service definition (see L<FS::part_svc>) to which this export applies
44
45 =item machine - Machine name 
46
47 =item exporttype - Export type
48
49 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item new HASHREF
58
59 Creates a new export.  To add the export to the database, see L<"insert">.
60
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to.  You can ask the object for a copy with the I<hash> method.
63
64 =cut
65
66 # the new method can be inherited from FS::Record, if a table method is defined
67
68 sub table { 'part_export'; }
69
70 =item clone SVCPART
71
72 An alternate constructor.  Creates a new export by duplicating an existing
73 export.  The given svcpart is assigned to the new export.
74
75 Returns a list consisting of the new export object and a hashref of options.
76
77 =cut
78
79 sub clone {
80   my $self = shift;
81   my $class = ref($self);
82   my %hash = $self->hash;
83   $hash{'exportnum'} = '';
84   $hash{'svcpart'} = shift;
85   ( $class->new( \%hash ),
86     { map { $_->optionname => $_->optionvalue }
87         qsearch('part_export_option', { 'exportnum' => $self->exportnum } )
88     }
89   );
90 }
91
92 =item insert HASHREF
93
94 Adds this record to the database.  If there is an error, returns the error,
95 otherwise returns false.
96
97 If a hash reference of options is supplied, part_export_option records are
98 created (see L<FS::part_export_option>).
99
100 =cut
101
102 #false laziness w/queue.pm
103 sub insert {
104   my $self = shift;
105   local $SIG{HUP} = 'IGNORE';
106   local $SIG{INT} = 'IGNORE';
107   local $SIG{QUIT} = 'IGNORE';
108   local $SIG{TERM} = 'IGNORE';
109   local $SIG{TSTP} = 'IGNORE';
110   local $SIG{PIPE} = 'IGNORE';
111
112   my $oldAutoCommit = $FS::UID::AutoCommit;
113   local $FS::UID::AutoCommit = 0;
114   my $dbh = dbh;
115
116   my $error = $self->SUPER::insert;
117   if ( $error ) {
118     $dbh->rollback if $oldAutoCommit;
119     return $error;
120   }
121
122   my $options = shift;
123   foreach my $optionname ( keys %{$options} ) {
124     my $part_export_option = new FS::part_export_option ( {
125       'exportnum'   => $self->exportnum,
126       'optionname'  => $optionname,
127       'optionvalue' => $options->{$optionname},
128     } );
129     $error = $part_export_option->insert;
130     if ( $error ) {
131       $dbh->rollback if $oldAutoCommit;
132       return $error;
133     }
134   }
135
136   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
137
138   '';
139
140 };
141
142 =item delete
143
144 Delete this record from the database.
145
146 =cut
147
148 #foreign keys would make this much less tedious... grr dumb mysql
149 sub delete {
150   my $self = shift;
151   local $SIG{HUP} = 'IGNORE';
152   local $SIG{INT} = 'IGNORE';
153   local $SIG{QUIT} = 'IGNORE';
154   local $SIG{TERM} = 'IGNORE';
155   local $SIG{TSTP} = 'IGNORE';
156   local $SIG{PIPE} = 'IGNORE';
157
158   my $oldAutoCommit = $FS::UID::AutoCommit;
159   local $FS::UID::AutoCommit = 0;
160   my $dbh = dbh;
161
162   my $error = $self->SUPER::delete;
163   if ( $error ) {
164     $dbh->rollback if $oldAutoCommit;
165     return $error;
166   }
167
168   foreach my $part_export_option ( $self->part_export_option ) {
169     my $error = $part_export_option->delete;
170     if ( $error ) {
171       $dbh->rollback if $oldAutoCommit;
172       return $error;
173     }
174   }
175
176   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
177
178   '';
179
180 }
181
182 =item replace OLD_RECORD HASHREF
183
184 Replaces the OLD_RECORD with this one in the database.  If there is an error,
185 returns the error, otherwise returns false.
186
187 If a hash reference of options is supplied, part_export_option records are
188 created or modified (see L<FS::part_export_option>).
189
190 =cut
191
192 sub replace {
193   my $self = shift;
194   local $SIG{HUP} = 'IGNORE';
195   local $SIG{INT} = 'IGNORE';
196   local $SIG{QUIT} = 'IGNORE';
197   local $SIG{TERM} = 'IGNORE';
198   local $SIG{TSTP} = 'IGNORE';
199   local $SIG{PIPE} = 'IGNORE';
200
201   my $oldAutoCommit = $FS::UID::AutoCommit;
202   local $FS::UID::AutoCommit = 0;
203   my $dbh = dbh;
204
205   my $error = $self->SUPER::replace;
206   if ( $error ) {
207     $dbh->rollback if $oldAutoCommit;
208     return $error;
209   }
210
211   my $options = shift;
212   foreach my $optionname ( keys %{$options} ) {
213     my $old = qsearchs( 'part_export_option', {
214         'exportnum'   => $self->exportnum,
215         'optionname'  => $optionname,
216     } );
217     my $new = new FS::part_export_option ( {
218         'exportnum'   => $self->exportnum,
219         'optionname'  => $optionname,
220         'optionvalue' => $options->{$optionname},
221     } );
222     my $error = $old ? $new->replace($old) : $new->insert;
223     if ( $error ) {
224       $dbh->rollback if $oldAutoCommit;
225       return $error;
226     }
227   }
228
229   #remove extraneous old options?  not necessary now, but...
230   #foreach my $opt ( grep { !exist $options->{$_->optionname} } $old->part_export_option ) {
231   #  my $error = $opt->delete;
232   #  if ( $error ) {
233   #    $dbh->rollback if $oldAutoCommit;
234   #    return $error;
235   #  }
236   #}
237
238   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
239
240   '';
241
242 };
243
244 =item check
245
246 Checks all fields to make sure this is a valid export.  If there is
247 an error, returns the error, otherwise returns false.  Called by the insert
248 and replace methods.
249
250 =cut
251
252 sub check {
253   my $self = shift;
254   my $error = 
255     $self->ut_numbern('exportnum')
256     || $self->ut_number('svcpart')
257     || $self->ut_alpha('exporttype')
258   ;
259   return $error if $error;
260
261   return "Unknown svcpart: ". $self->svcpart
262     unless qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
263
264   $self->machine =~ /^([\w\-\.]*)$/
265     or return "Illegal machine: ". $self->machine;
266   $self->machine($1);
267
268   $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
269   $self->nodomain($1);
270
271   #check exporttype?
272
273   ''; #no error
274 }
275
276 =item part_svc
277
278 Returns the service definition (see L<FS::part_svc>) for this export.
279
280 =cut
281
282 sub part_svc {
283   my $self = shift;
284   qsearchs('part_svc', { svcpart => $self->svcpart } );
285 }
286
287 =item part_export_option
288
289 =cut
290
291 sub part_export_option {
292   my $self = shift;
293   qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
294 }
295
296 =item options 
297
298 =cut
299
300 sub options {
301   my $self = shift;
302   map { $_->optionname => $_->optionvalue } $self->part_export_option;
303 }
304
305 =item option
306
307 =cut
308
309 sub option {
310   my $self = shift;
311   my $part_export_option =
312     qsearchs('part_export_option', {
313       exportnum  => $self->exportnum,
314       optionname => shift,
315   } );
316   $part_export_option ? $part_export_option->optionvalue : '';
317 }
318
319 =item rebless
320
321 =cut
322
323 sub rebless {
324   my $self = shift;
325   my $exporttype = $self->exporttype;
326   my $class = ref($self);
327   bless($self, $class."::$exporttype");
328 }
329
330 =item export_insert SVC_OBJECT
331
332 =cut
333
334 sub export_insert {
335   my $self = shift;
336   $self->rebless;
337   $self->_export_insert(@_);
338 }
339
340 #sub AUTOLOAD {
341 #  my $self = shift;
342 #  $self->rebless;
343 #  my $method = $AUTOLOAD;
344 #  #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
345 #  $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
346 #  $self->$method(@_);
347 #}
348
349 =item export_replace
350
351 =cut
352
353 sub export_replace {
354   my $self = shift;
355   $self->rebless;
356   $self->_export_replace(@_);
357 }
358
359 =item export_delete
360
361 =cut
362
363 sub export_delete {
364   my $self = shift;
365   $self->rebless;
366   $self->_export_delete(@_);
367 }
368
369 =back
370
371 =cut
372
373 #infostreet
374
375 package FS::part_export::infostreet;
376 use vars qw(@ISA);
377 @ISA = qw(FS::part_export);
378
379 sub _export_insert {
380   my( $self, $svc_acct ) = (shift, shift);
381   $self->infostreet_queue( $svc_acct->svcnum,
382     'createUser', $svc_acct->username, $svc_acct->password );
383 }
384
385 sub _export_replace {
386   my( $self, $new, $old ) = (shift, shift, shift);
387   return "can't change username with InfoStreet"
388     if $old->username ne $new->username;
389   return '' unless $old->_password ne $new->_password;
390   $self->infostreet_queue( $new->svcnum,
391     'passwd', $new->username, $new->password );
392 }
393
394 sub _export_delete {
395   my( $self, $svc_acct ) = (shift, shift);
396   $self->infostreet_queue( $svc_acct->svcnum,
397     'purgeAccount,releaseUsername', $svc_acct->username );
398 }
399
400 sub infostreet_queue {
401   my( $self, $svcnum, $method ) = (shift, shift, shift);
402   my $queue = new FS::queue {
403     'svcnum' => $svcnum,
404     'job'    => 'FS::part_export::infostreet::infostreet_command',
405   };
406   $queue->insert(
407     $self->option('url'),
408     $self->option('login'),
409     $self->option('password'),
410     $self->option('groupID'),
411     $method,
412     @_,
413   );
414 }
415
416 sub infostreet_command { #subroutine, not method
417   my($url, $username, $password, $groupID, $method, @args) = @_;
418
419   #quelle hack
420   if ( $method =~ /,/ ) {
421     foreach my $part ( split(/,\s*/, $method) ) {
422       infostreet_command($url, $username, $password, $groupID, $part, @args);
423     }
424     return;
425   }
426
427   eval "use Frontier::Client;";
428
429   my $conn = Frontier::Client->new( url => $url );
430   my $key_result = $conn->call( 'authenticate', $username, $password, $groupID);
431   my %key_result = _infostreet_parse($key_result);
432   die $key_result{error} unless $key_result{success};
433   my $key = $key_result{data};
434
435   my $result = $conn->call($method, $key, @args);
436   my %result = _infostreet_parse($result);
437   die $result{error} unless $result{success};
438
439 }
440
441 sub _infostreet_parse { #subroutine, not method
442   my $arg = shift;
443   map {
444     my $value = $arg->{$_};
445     #warn ref($value);
446     $value = $value->value()
447       if ref($value) && $value->isa('Frontier::RPC2::DataType');
448     $_=>$value;
449   } keys %$arg;
450 }
451
452 #sqlradius
453
454 package FS::part_export::sqlradius;
455 use vars qw(@ISA);
456 @ISA = qw(FS::part_export);
457
458 sub _export_insert {
459   my($self, $svc_acct) = (shift, shift);
460   $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
461     'reply', $svc_acct->username, $svc_acct->radius_reply );
462   $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
463     'check', $svc_acct->username, $svc_acct->radius_check );
464 }
465
466 sub _export_replace {
467   my( $self, $new, $old ) = (shift, shift, shift);
468
469   #return "can't (yet) change username with sqlradius"
470   #  if $old->username ne $new->username;
471   if ( $old->username ne $new->username ) {
472     my $error = $self->sqlradius_queue( $new->svcnum, 'rename',
473       $new->username, $old->username );
474     return $error if $error;
475   }
476
477   foreach my $table (qw(reply check)) {
478     my $method = "radius_$table";
479     my %new = $new->$method;
480     my %old = $old->$method;
481     if ( grep { !exists $old{$_} #new attributes
482                 || $new{$_} ne $old{$_} #changed
483               } keys %new
484     ) {
485       my $error = $self->sqlradius_queue( $new->svcnum, 'insert',
486         $table, $new->username, %new );
487       return $error if $error;
488     }
489
490     my @del = grep { !exists $new{$_} } keys %old;
491     my $error = $self->sqlradius_queue( $new->svcnum, 'sqlradius_attrib_delete',
492       $table, $new->username, @del );
493     return $error if $error;
494   }
495
496   '';
497 }
498
499 sub _export_delete {
500   my( $self, $svc_acct ) = (shift, shift);
501   $self->sqlradius_queue( $svc_acct->svcnum, 'delete',
502     $svc_acct->username );
503 }
504
505 sub sqlradius_queue {
506   my( $self, $svcnum, $method ) = (shift, shift, shift);
507   my $queue = new FS::queue {
508     'svcnum' => $svcnum,
509     'job'    => "FS::part_export::sqlradius::sqlradius_$method",
510   };
511   $queue->insert(
512     $self->option('datasrc'),
513     $self->option('username'),
514     $self->option('password'),
515     @_,
516   );
517 }
518
519 sub sqlradius_insert { #subroutine, not method
520   my $dbh = sqlradius_connect(shift, shift, shift);
521   my( $replycheck, $username, %attributes ) = @_;
522
523   foreach my $attribute ( keys %attributes ) {
524     my $u_sth = $dbh->prepare(
525       "UPDATE rad$replycheck SET Value = ? WHERE UserName = ? AND Attribute = ?"    ) or die $dbh->errstr;
526     my $i_sth = $dbh->prepare(
527       "INSERT INTO rad$replycheck ( id, UserName, Attribute, Value ) ".
528         "VALUES ( ?, ?, ?, ? )" )
529       or die $dbh->errstr;
530     $u_sth->execute($attributes{$attribute}, $username, $attribute) > 0
531       or $i_sth->execute( '', $username, $attribute, $attributes{$attribute} )
532         or die "can't insert into rad$replycheck table: ". $i_sth->errstr;
533   }
534   $dbh->disconnect;
535 }
536
537 sub sqlradius_rename { #subroutine, not method
538   my $dbh = sqlradius_connect(shift, shift, shift);
539   my($new_username, $old_username) = @_;
540   foreach my $table (qw(radreply radcheck)) {
541     my $sth = $dbh->prepare("UPDATE $table SET Username = ? WHERE UserName = ?")
542       or die $dbh->errstr;
543     $sth->execute($new_username, $old_username)
544       or die "can't update $table: ". $sth->errstr;
545   }
546   $dbh->disconnect;
547 }
548
549 sub sqlradius_attrib_delete { #subroutine, not method
550   my $dbh = sqlradius_connect(shift, shift, shift);
551   my( $replycheck, $username, @attrib ) = @_;
552
553   foreach my $attribute ( @attrib ) {
554     my $sth = $dbh->prepare(
555         "DELETE FROM rad$replycheck WHERE UserName = ? AND Attribute = ?" )
556       or die $dbh->errstr;
557     $sth->execute($username,$attribute)
558       or die "can't delete from rad$replycheck table: ". $sth->errstr;
559   }
560   $dbh->disconnect;
561 }
562
563 sub sqlradius_delete { #subroutine, not method
564   my $dbh = sqlradius_connect(shift, shift, shift);
565   my $username = shift;
566
567   foreach my $table (qw( radcheck radreply )) {
568     my $sth = $dbh->prepare( "DELETE FROM $table WHERE UserName = ?" );
569     $sth->execute($username)
570       or die "can't delete from $table table: ". $sth->errstr;
571   }
572   $dbh->disconnect;
573 }
574
575 sub sqlradius_connect {
576   #my($datasrc, $username, $password) = @_;
577   #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
578   DBI->connect(@_) or die $DBI::errstr;
579 }
580
581 =head1 NEW EXPORT CLASSES
582
583   #myexport
584   
585   package FS::part_export::myexport;
586   use vars qw(@ISA);
587   @ISA = qw(FS::part_export);
588   
589   sub _export_insert {
590     my($self, $svc_something) = (shift, shift);
591     $self->myexport_queue( $svc_acct->svcnum, 'insert',
592       $svc_something->username, $svc_something->password );
593   }
594   
595   sub _export_replace {
596     my( $self, $new, $old ) = (shift, shift, shift);
597     #return "can't change username with myexport"
598     #  if $old->username ne $new->username;
599     #return '' unless $old->_password ne $new->_password;
600     $self->myexport_queue( $new->svcnum,
601       'replace', $new->username, $new->password );
602   }
603   
604   sub _export_delete {
605     my( $self, $svc_something ) = (shift, shift);
606     $self->myexport_queue( $svc_acct->svcnum,
607       'delete', $svc_something->username );
608   }
609   
610   #a good idea to queue anything that could fail or take any time
611   sub myexport_queue {
612     my( $self, $svcnum, $method ) = (shift, shift, shift);
613     my $queue = new FS::queue {
614       'svcnum' => $svcnum,
615       'job'    => "FS::part_export::myexport::myexport_$method",
616     };
617     $queue->insert( @_ );
618   }
619   
620   sub myexport_insert { #subroutine, not method
621   }
622   sub myexport_replace { #subroutine, not method
623   }
624   sub myexport_delete { #subroutine, not method
625   }
626
627 =head1 BUGS
628
629 Probably.
630
631 Hmm, export code has wound up in here.  Move those sub-classes out into their
632 own files, at least.  Also hmm... cust_export class (not necessarily a
633 database table...) ... ?
634
635 =head1 SEE ALSO
636
637 L<FS::part_export_option>, L<FS::part_svc>, L<FS::svc_acct>, L<FS::svc_domain>,
638 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.
639
640 =cut
641
642 1;
643