444e86aa091d6fb423be25e4791d446b24fa6848
[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   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
230
231   '';
232
233 };
234
235 =item check
236
237 Checks all fields to make sure this is a valid export.  If there is
238 an error, returns the error, otherwise returns false.  Called by the insert
239 and replace methods.
240
241 =cut
242
243 sub check {
244   my $self = shift;
245   my $error = 
246     $self->ut_numbern('exportnum')
247     || $self->ut_number('svcpart')
248     || $self->ut_alpha('exporttype')
249   ;
250   return $error if $error;
251
252   return "Unknown svcpart: ". $self->svcpart
253     unless qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
254
255   $self->machine =~ /^([\w\-\.]*)$/
256     or return "Illegal machine: ". $self->machine;
257   $self->machine($1);
258
259   $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
260   $self->nodomain($1);
261
262   #check exporttype?
263
264   ''; #no error
265 }
266
267 =item part_svc
268
269 Returns the service definition (see L<FS::part_svc>) for this export.
270
271 =cut
272
273 sub part_svc {
274   my $self = shift;
275   qsearchs('part_svc', { svcpart => $self->svcpart } );
276 }
277
278 =item part_export_option
279
280 =cut
281
282 sub part_export_option {
283   my $self = shift;
284   qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
285 }
286
287 =item options 
288
289 =cut
290
291 sub options {
292   my $self = shift;
293   map { $_->optionname => $_->optionvalue } $self->part_export_option;
294 }
295
296 =item option
297
298 =cut
299
300 sub option {
301   my $self = shift;
302   my $part_export_option =
303     qsearchs('part_export_option', {
304       exportnum  => $self->exportnum,
305       optionname => shift,
306   } );
307   $part_export_option ? $part_export_option->optionvalue : '';
308 }
309
310 =item rebless
311
312 =cut
313
314 sub rebless {
315   my $self = shift;
316   my $exporttype = $self->exporttype;
317   my $class = ref($self);
318   bless($self, $class."::$exporttype");
319 }
320
321 =item export_insert SVC_OBJECT
322
323 Calls the appropriate export_I<exporttype> for this object's exporttype.
324
325 =cut
326
327 sub export_insert {
328   my $self = shift;
329   $self->rebless;
330   $self->_export_insert(@_);
331 }
332
333 #sub AUTOLOAD {
334 #  my $self = shift;
335 #  $self->rebless;
336 #  my $method = $AUTOLOAD;
337 #  #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
338 #  $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
339 #  $self->$method(@_);
340 #}
341
342 =item export_replace
343
344 =cut
345
346 sub export_replace {
347   my $self = shift;
348   $self->rebless;
349   $self->_export_replace(@_);
350 }
351
352 =item export_delete
353
354 =cut
355
356 sub export_delete {
357   my $self = shift;
358   $self->rebless;
359   $self->_export_delete(@_);
360 }
361
362 =back
363
364 #infostreet
365
366 package FS::part_export::infostreet;
367 use vars qw(@ISA);
368 @ISA = qw(FS::part_export);
369
370 sub _export_insert {
371   my( $self, $svc_acct ) = (shift, shift);
372   $self->infostreet_queue( $svc_acct->svcnum,
373     'createUser', $svc_acct->username, $svc_acct->password );
374 }
375
376 sub _export_replace {
377   my( $self, $new, $old ) = (shift, shift, shift);
378   return "can't change username with InfoStreet"
379     if $old->username ne $new->username;
380   return '' unless $old->_password ne $new->_password;
381   $self->infostreet_queue( $new->svcnum,
382     'passwd', $new->username, $new->password );
383 }
384
385 sub _export_delete {
386   my( $self, $svc_acct ) = (shift, shift);
387   $self->infostreet_queue( $svc_acct->svcnum,
388     'purgeAccount,releaseUsername', $svc_acct->username );
389 }
390
391 sub infostreet_queue {
392   my( $self, $svcnum, $method ) = (shift, shift, shift);
393   my $queue = new FS::queue {
394     'svcnum' => $svcnum,
395     'job'    => 'FS::part_export::infostreet::infostreet_command',
396   };
397   $queue->insert(
398     $self->option('url'),
399     $self->option('login'),
400     $self->option('password'),
401     $self->option('groupID'),
402     $method,
403     @_,
404   );
405 }
406
407 sub infostreet_command { #subroutine, not method
408   my($url, $username, $password, $groupID, $method, @args) = @_;
409
410   #quelle hack
411   if ( $method =~ /,/ ) {
412     foreach my $part ( split(/,\s*/, $method) ) {
413       infostreet_command($url, $username, $password, $groupID, $part, @args);
414     }
415     return;
416   }
417
418   eval "use Frontier::Client;";
419
420   my $conn = Frontier::Client->new( url => $url );
421   my $key_result = $conn->call( 'authenticate', $username, $password, $groupID);
422   my %key_result = _infostreet_parse($key_result);
423   die $key_result{error} unless $key_result{success};
424   my $key = $key_result{data};
425
426   my $result = $conn->call($opt{method}, $key, @{$opt{args}});
427   my %result = _infostreet_parse($result);
428   die $result{error} unless $result{success};
429
430 }
431
432 sub _infostreet_parse { #subroutine, not method
433   my $arg = shift;
434   map {
435     my $value = $arg->{$_};
436     #warn ref($value);
437     $value = $value->value()
438       if ref($value) && $value->isa('Frontier::RPC2::DataType');
439     $_=>$value;
440   } keys %$arg;
441 }
442
443 #sqlradius
444
445 package FS::part_export::sqlradius;
446 use vars qw(@ISA);
447 @ISA = qw(FS::part_export);
448
449 sub _export_insert {
450   my($self, $svc_acct) = (shift, shift);
451   $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
452     'reply', $svc_acct->username, $svc_acct->radius_reply );
453   $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
454     'check', $svc_acct->username, $svc_acct->radius_check );
455 }
456
457 sub _export_replace {
458   my( $self, $new, $old ) = (shift, shift, shift);
459
460   #return "can't (yet) change username with sqlradius"
461   #  if $old->username ne $new->username;
462   if ( $old->username ne $new->username ) {
463     my $error = $self->sqlradius_queue( $new->svcnum, 'rename',
464       $new->username, $old->username );
465     return $error if $error;
466   }
467
468   foreach my $table (qw(reply check)) {
469     my $method = "radius_$table";
470     my %new = $new->$method;
471     my %old = $old->$method;
472     if ( grep { !exists $old{$_} #new attributes
473                 || $new{$n} ne $old{$n} #changed
474               } keys %new
475     ) {
476       my $error = $self->sqlradius_queue( $new->svcnum, 'insert'
477         $table, $new->username, %new );
478       return $error if $error;
479     }
480
481     my @del = grep { !exists $new{$_} } keys %old;
482     my $error = $self->sqlradius_queue( $new->svcnum, 'sqlradius_attrib_delete',
483       $table, $new->username, @del );
484     return $error if $error;
485   }
486
487   '';
488 }
489
490 sub _export_delete {
491   my( $self, $svc_something ) = (shift, shift);
492   $self->sqlradius_queue( $svc_acct->svcnum, 'delete',
493     $svc_something->username );
494 }
495
496 sub sqlradius_queue {
497   my( $self, $svcnum, $method ) = (shift, shift, shift);
498   my $queue = new FS::queue {
499     'svcnum' => $svcnum,
500     'job'    => "FS::part_export::sqlradius::sqlradius_$method",
501   };
502   $queue->insert(
503     $self->option('datasrc'),
504     $self->option('username'),
505     $self->option('password'),
506     @_,
507   );
508 }
509
510 sub sqlradius_insert { #subroutine, not method
511   my $dbh = sqlradius_connect(shift, shift, shift);
512   my( $replycheck, $username, %attributes ) = @_;
513
514   foreach my $attribute ( keys %attributes ) {
515     my $u_sth = $dbh->prepare(
516       "UPDATE rad$replycheck SET Value = ? WHERE UserName = ? AND Attribute = ?"    ) or die $dbh->errstr;
517     my $i_sth = $dbh->prepare(
518       "INSERT INTO rad$replycheck ( id, UserName, Attribute, Value ) ".
519         "VALUES ( ?, ?, ?, ? )" )
520       or die $dbh->errstr;
521     $u_sth->execute($attributes{$attribute}, $username, $attribute) > 0
522       or $i_sth->execute( '', $username, $attribute, $attributes{$attribute} )
523         or die "can't insert into rad$replycheck table: ". $i_sth->errstr;
524   }
525   $dbh->disconnect;
526 }
527
528 sub sqlradius_rename { #subroutine, not method
529   my $dbh = sqlradius_connect(shift, shift, shift);
530   my($new_username, $old_username) = @_;
531   foreach my $table (qw(radreply radcheck)) {
532     my $sth = $dbh->prepare("UPDATE $table SET Username = ? WHERE UserName = ?")
533       or die $dbh->errstr;
534     $sth->execute($new_username, $old_username)
535       or die "can't update $table: ". $sth->errstr;
536   }
537   $dbh->disconnect;
538 }
539
540 sub sqlradius_attrib_delete { #subroutine, not method
541   my $dbh = sqlradius_connect(shift, shift, shift);
542   my( $replycheck, $username, @attrib ) = @_;
543
544   foreach my $attribute ( @attrib ) {
545     my $sth = $dbh->prepare(
546         "DELETE FROM $table WHERE UserName = ? AND Attribute = ?" )
547       or die $dbh->errstr;
548     $sth->execute($username,$attribute)
549       or die "can't delete from $table table: ". $sth->errstr;
550   }
551   $dbh->disconnect;
552 }
553
554 sub sqlradius_delete { #subroutine, not method
555   my $dbh = sqlradius_connect(shift, shift, shift);
556   my $username = shift;
557
558   foreach my $table (qw( radcheck radreply )) {
559     my $sth = $dbh->prepare( "DELETE FROM $table WHERE UserName = ?" );
560     $sth->execute($username)
561       or die "can't delete from $table table: ". $sth->errstr;
562   }
563   $dbh->disconnect;
564 }
565
566 sub sqlradius_connect {
567   #my($datasrc, $username, $password) = @_;
568   #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
569   DBI->connect(@_) or die $DBI::errstr;
570 }
571
572 =head1 NOTES
573
574 Writing a new export class:
575
576 #myexport
577
578 package FS::part_export::myexport;
579 use vars qw(@ISA);
580 @ISA = qw(FS::part_export);
581
582 sub _export_insert {
583   my($self, $svc_something) = (shift, shift);
584   $self->myexport_queue( $svc_acct->svcnum, 'insert',
585     $svc_something->username, $svc_something->password );
586 }
587
588 sub _export_replace {
589   my( $self, $new, $old ) = (shift, shift, shift);
590   #return "can't change username with myexport"
591   #  if $old->username ne $new->username;
592   #return '' unless $old->_password ne $new->_password;
593   $self->myexport_queue( $new->svcnum,
594     'replace', $new->username, $new->password );
595 }
596
597 sub _export_delete {
598   my( $self, $svc_something ) = (shift, shift);
599   $self->myexport_queue( $svc_acct->svcnum,
600     'delete', $svc_something->username );
601 }
602
603 #a good idea to queue anything that could fail or take any time
604 sub myexport_queue {
605   my( $self, $svcnum, $method ) = (shift, shift, shift);
606   my $queue = new FS::queue {
607     'svcnum' => $svcnum,
608     'job'    => "FS::part_export::myexport::myexport_$method",
609   };
610   $queue->insert( @_ );
611 }
612
613 sub myexport_insert { #subroutine, not method
614 }
615 sub myexport_replace { #subroutine, not method
616 }
617 sub myexport_delete { #subroutine, not method
618 }
619
620 =head1 BUGS
621
622 Probably.
623
624 Hmm, export code has wound up in here.  Move those sub-classes out into their
625 own files, at least.  Also hmm... cust_export class (not necessarily a
626 database table...) ... ?
627
628 =head1 SEE ALSO
629
630 L<FS::part_export_option>, L<FS::part_svc>, L<FS::svc_acct>, L<FS::svc_domain>,
631 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.
632
633 =cut
634
635 1;
636