41dfe77a1ea2947585018991a399d5ae665e6ad4
[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   my $options = shift;
106   local $SIG{HUP} = 'IGNORE';
107   local $SIG{INT} = 'IGNORE';
108   local $SIG{QUIT} = 'IGNORE';
109   local $SIG{TERM} = 'IGNORE';
110   local $SIG{TSTP} = 'IGNORE';
111   local $SIG{PIPE} = 'IGNORE';
112
113   my $oldAutoCommit = $FS::UID::AutoCommit;
114   local $FS::UID::AutoCommit = 0;
115   my $dbh = dbh;
116
117   my $error = $self->SUPER::insert;
118   if ( $error ) {
119     $dbh->rollback if $oldAutoCommit;
120     return $error;
121   }
122
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   my $old = shift;
195   my $options = shift;
196   local $SIG{HUP} = 'IGNORE';
197   local $SIG{INT} = 'IGNORE';
198   local $SIG{QUIT} = 'IGNORE';
199   local $SIG{TERM} = 'IGNORE';
200   local $SIG{TSTP} = 'IGNORE';
201   local $SIG{PIPE} = 'IGNORE';
202
203   my $oldAutoCommit = $FS::UID::AutoCommit;
204   local $FS::UID::AutoCommit = 0;
205   my $dbh = dbh;
206
207   my $error = $self->SUPER::replace($old);
208   if ( $error ) {
209     $dbh->rollback if $oldAutoCommit;
210     return $error;
211   }
212
213   foreach my $optionname ( keys %{$options} ) {
214     my $old = qsearchs( 'part_export_option', {
215         'exportnum'   => $self->exportnum,
216         'optionname'  => $optionname,
217     } );
218     my $new = new FS::part_export_option ( {
219         'exportnum'   => $self->exportnum,
220         'optionname'  => $optionname,
221         'optionvalue' => $options->{$optionname},
222     } );
223     $new->optionnum($old->optionnum) if $old;
224     my $error = $old ? $new->replace($old) : $new->insert;
225     if ( $error ) {
226       $dbh->rollback if $oldAutoCommit;
227       return $error;
228     }
229   }
230
231   #remove extraneous old options
232   foreach my $opt (
233     grep { !exists $options->{$_->optionname} } $old->part_export_option
234   ) {
235     my $error = $opt->delete;
236     if ( $error ) {
237       $dbh->rollback if $oldAutoCommit;
238       return $error;
239     }
240   }
241
242   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
243
244   '';
245
246 };
247
248 =item check
249
250 Checks all fields to make sure this is a valid export.  If there is
251 an error, returns the error, otherwise returns false.  Called by the insert
252 and replace methods.
253
254 =cut
255
256 sub check {
257   my $self = shift;
258   my $error = 
259     $self->ut_numbern('exportnum')
260     || $self->ut_domain('machine')
261     || $self->ut_number('svcpart')
262     || $self->ut_alpha('exporttype')
263   ;
264   return $error if $error;
265
266   return "Unknown svcpart: ". $self->svcpart
267     unless qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
268
269   $self->machine =~ /^([\w\-\.]*)$/
270     or return "Illegal machine: ". $self->machine;
271   $self->machine($1);
272
273   $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
274   $self->nodomain($1);
275
276   #check exporttype?
277
278   ''; #no error
279 }
280
281 =item part_svc
282
283 Returns the service definition (see L<FS::part_svc>) for this export.
284
285 =cut
286
287 sub part_svc {
288   my $self = shift;
289   qsearchs('part_svc', { svcpart => $self->svcpart } );
290 }
291
292 =item part_export_option
293
294 Returns all options as FS::part_export_option objects (see
295 L<FS::part_export_option>).
296
297 =cut
298
299 sub part_export_option {
300   my $self = shift;
301   qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
302 }
303
304 =item options 
305
306 Returns a list of option names and values suitable for assigning to a hash.
307
308 =cut
309
310 sub options {
311   my $self = shift;
312   map { $_->optionname => $_->optionvalue } $self->part_export_option;
313 }
314
315 =item option OPTIONNAME
316
317 Returns the option value for the given name, or the empty string.
318
319 =cut
320
321 sub option {
322   my $self = shift;
323   my $part_export_option =
324     qsearchs('part_export_option', {
325       exportnum  => $self->exportnum,
326       optionname => shift,
327   } );
328   $part_export_option ? $part_export_option->optionvalue : '';
329 }
330
331 =item rebless
332
333 Reblesses the object into the FS::part_export::EXPORTTYPE class, where
334 EXPORTTYPE is the object's I<exporttype> field.  There should be better docs
335 on how to create new exports (and they should live in their own files and be
336 autoloaded-on-demand), but until then, see L</NEW EXPORT CLASSES>.
337
338 =cut
339
340 sub rebless {
341   my $self = shift;
342   my $exporttype = $self->exporttype;
343   my $class = ref($self);
344   bless($self, $class."::$exporttype");
345 }
346
347 =item export_insert SVC_OBJECT
348
349 =cut
350
351 sub export_insert {
352   my $self = shift;
353   $self->rebless;
354   $self->_export_insert(@_);
355 }
356
357 #sub AUTOLOAD {
358 #  my $self = shift;
359 #  $self->rebless;
360 #  my $method = $AUTOLOAD;
361 #  #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
362 #  $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
363 #  $self->$method(@_);
364 #}
365
366 =item export_replace NEW OLD
367
368 =cut
369
370 sub export_replace {
371   my $self = shift;
372   $self->rebless;
373   $self->_export_replace(@_);
374 }
375
376 =item export_delete
377
378 =cut
379
380 sub export_delete {
381   my $self = shift;
382   $self->rebless;
383   $self->_export_delete(@_);
384 }
385
386 #fallbacks providing useful error messages intead of infinite loops
387 sub _export_insert {
388   my $self = shift;
389   return "_export_insert: unknown export type ". $self->exporttype;
390 }
391
392 sub _export_replace {
393   my $self = shift;
394   return "_export_replace: unknown export type ". $self->exporttype;
395 }
396
397 sub _export_delete {
398   my $self = shift;
399   return "_export_delete: unknown export type ". $self->exporttype;
400 }
401
402 =back
403
404 =cut
405
406 #infostreet
407
408 package FS::part_export::infostreet;
409 use vars qw(@ISA);
410 @ISA = qw(FS::part_export);
411
412 sub rebless { shift; }
413
414 sub _export_insert {
415   my( $self, $svc_acct ) = (shift, shift);
416   $self->infostreet_queue( $svc_acct->svcnum,
417     'createUser', $svc_acct->username, $svc_acct->password );
418 }
419
420 sub _export_replace {
421   my( $self, $new, $old ) = (shift, shift, shift);
422   return "can't change username with InfoStreet"
423     if $old->username ne $new->username;
424   return '' unless $old->_password ne $new->_password;
425   $self->infostreet_queue( $new->svcnum,
426     'passwd', $new->username, $new->password );
427 }
428
429 sub _export_delete {
430   my( $self, $svc_acct ) = (shift, shift);
431   $self->infostreet_queue( $svc_acct->svcnum,
432     'purgeAccount,releaseUsername', $svc_acct->username );
433 }
434
435 sub infostreet_queue {
436   my( $self, $svcnum, $method ) = (shift, shift, shift);
437   my $queue = new FS::queue {
438     'svcnum' => $svcnum,
439     'job'    => 'FS::part_export::infostreet::infostreet_command',
440   };
441   $queue->insert(
442     $self->option('url'),
443     $self->option('login'),
444     $self->option('password'),
445     $self->option('groupID'),
446     $method,
447     @_,
448   );
449 }
450
451 sub infostreet_command { #subroutine, not method
452   my($url, $username, $password, $groupID, $method, @args) = @_;
453
454   #quelle hack
455   if ( $method =~ /,/ ) {
456     foreach my $part ( split(/,\s*/, $method) ) {
457       infostreet_command($url, $username, $password, $groupID, $part, @args);
458     }
459     return;
460   }
461
462   eval "use Frontier::Client;";
463
464   my $conn = Frontier::Client->new( url => $url );
465   my $key_result = $conn->call( 'authenticate', $username, $password, $groupID);
466   my %key_result = _infostreet_parse($key_result);
467   die $key_result{error} unless $key_result{success};
468   my $key = $key_result{data};
469
470   my $result = $conn->call($method, $key, @args);
471   my %result = _infostreet_parse($result);
472   die $result{error} unless $result{success};
473
474 }
475
476 sub _infostreet_parse { #subroutine, not method
477   my $arg = shift;
478   map {
479     my $value = $arg->{$_};
480     #warn ref($value);
481     $value = $value->value()
482       if ref($value) && $value->isa('Frontier::RPC2::DataType');
483     $_=>$value;
484   } keys %$arg;
485 }
486
487 #sqlradius
488
489 package FS::part_export::sqlradius;
490 use vars qw(@ISA);
491 @ISA = qw(FS::part_export);
492
493 sub rebless { shift; }
494
495 sub _export_insert {
496   my($self, $svc_acct) = (shift, shift);
497
498   foreach my $table (qw(reply check)) {
499     my $method = "radius_$table";
500     my %attrib = $svc_acct->$method;
501     next unless keys %attrib;
502     my $error = $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
503       $table, $svc_acct->username, %attrib );
504     return $error if $error;
505   }
506   my @groups = $svc_acct->radius_groups;
507   if ( @groups ) {
508     my $error = $self->sqlradius_queue( $svc_acct->svcnum, 'usergroup_insert',
509       $svc_acct->username, @groups );
510     return $error if $error;
511   }
512   '';
513 }
514
515 sub _export_replace {
516   my( $self, $new, $old ) = (shift, shift, shift);
517
518   #return "can't (yet) change username with sqlradius"
519   #  if $old->username ne $new->username;
520   if ( $old->username ne $new->username ) {
521     my $error = $self->sqlradius_queue( $new->svcnum, 'rename',
522       $new->username, $old->username );
523     return $error if $error;
524   }
525
526   foreach my $table (qw(reply check)) {
527     my $method = "radius_$table";
528     my %new = $new->$method;
529     my %old = $old->$method;
530     if ( grep { !exists $old{$_} #new attributes
531                 || $new{$_} ne $old{$_} #changed
532               } keys %new
533     ) {
534       my $error = $self->sqlradius_queue( $new->svcnum, 'insert',
535         $table, $new->username, %new );
536       return $error if $error;
537     }
538
539     my @del = grep { !exists $new{$_} } keys %old;
540     if ( @del ) {
541       my $error = $self->sqlradius_queue( $new->svcnum, 'attrib_delete',
542         $table, $new->username, @del );
543       return $error if $error;
544     }
545   }
546
547   # (sorta) false laziness with FS::svc_acct::replace
548   my @oldgroups = @{$old->usergroup}; #uuuh
549   my @newgroups = $new->radius_groups;
550   my @delgroups = ();
551   foreach my $oldgroup ( @oldgroups ) {
552     if ( grep { $oldgroup eq $_ } @newgroups ) {
553       @newgroups = grep { $oldgroup ne $_ } @newgroups;
554       next;
555     }
556     push @delgroups, $oldgroup;
557   }
558
559   if ( @delgroups ) {
560     my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_delete',
561       $new->username, @delgroups );
562     return $error if $error;
563   }
564
565   if ( @newgroups ) {
566     my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_insert',
567       $new->username, @newgroups );
568     return $error if $error;
569   }
570
571   '';
572 }
573
574 sub _export_delete {
575   my( $self, $svc_acct ) = (shift, shift);
576   $self->sqlradius_queue( $svc_acct->svcnum, 'delete',
577     $svc_acct->username );
578 }
579
580 sub sqlradius_queue {
581   my( $self, $svcnum, $method ) = (shift, shift, shift);
582   my $queue = new FS::queue {
583     'svcnum' => $svcnum,
584     'job'    => "FS::part_export::sqlradius::sqlradius_$method",
585   };
586   $queue->insert(
587     $self->option('datasrc'),
588     $self->option('username'),
589     $self->option('password'),
590     @_,
591   );
592 }
593
594 sub sqlradius_insert { #subroutine, not method
595   my $dbh = sqlradius_connect(shift, shift, shift);
596   my( $replycheck, $username, %attributes ) = @_;
597
598   foreach my $attribute ( keys %attributes ) {
599     my $u_sth = $dbh->prepare(
600       "UPDATE rad$replycheck SET Value = ? WHERE UserName = ? AND Attribute = ?"    ) or die $dbh->errstr;
601     my $i_sth = $dbh->prepare(
602       "INSERT INTO rad$replycheck ( id, UserName, Attribute, Value ) ".
603         "VALUES ( ?, ?, ?, ? )"
604     ) or die $dbh->errstr;
605     $u_sth->execute($attributes{$attribute}, $username, $attribute) > 0
606       or $i_sth->execute( '', $username, $attribute, $attributes{$attribute} )
607         or die "can't insert into rad$replycheck table: ". $i_sth->errstr;
608   }
609   $dbh->disconnect;
610 }
611
612 sub sqlradius_usergroup_insert { #subroutine, not method
613   my $dbh = sqlradius_connect(shift, shift, shift);
614   my( $username, @groups ) = @_;
615
616   my $sth = $dbh->prepare( 
617     "INSERT INTO usergroup ( id, UserName, GroupName ) VALUES ( ?, ?, ? )"
618   ) or die $dbh->errstr;
619   foreach my $group ( @groups ) {
620     $sth->execute( '', $username, $group )
621       or die "can't insert into groupname table: ". $sth->errstr;
622   }
623   $dbh->disconnect;
624 }
625
626 sub sqlradius_usergroup_delete { #subroutine, not method
627   my $dbh = sqlradius_connect(shift, shift, shift);
628   my( $username, @groups ) = @_;
629
630   my $sth = $dbh->prepare( 
631     "DELETE FROM usergroup ( id, UserName, GroupName ) VALUES ( ?, ?, ? )"
632   ) or die $dbh->errstr;
633   foreach my $group ( @groups ) {
634     $sth->execute( '', $username, $group )
635       or die "can't delete from groupname table: ". $sth->errstr;
636   }
637   $dbh->disconnect;
638 }
639
640 sub sqlradius_rename { #subroutine, not method
641   my $dbh = sqlradius_connect(shift, shift, shift);
642   my($new_username, $old_username) = @_;
643   foreach my $table (qw(radreply radcheck usergroup )) {
644     my $sth = $dbh->prepare("UPDATE $table SET Username = ? WHERE UserName = ?")
645       or die $dbh->errstr;
646     $sth->execute($new_username, $old_username)
647       or die "can't update $table: ". $sth->errstr;
648   }
649   $dbh->disconnect;
650 }
651
652 sub sqlradius_attrib_delete { #subroutine, not method
653   my $dbh = sqlradius_connect(shift, shift, shift);
654   my( $replycheck, $username, @attrib ) = @_;
655
656   foreach my $attribute ( @attrib ) {
657     my $sth = $dbh->prepare(
658         "DELETE FROM rad$replycheck WHERE UserName = ? AND Attribute = ?" )
659       or die $dbh->errstr;
660     $sth->execute($username,$attribute)
661       or die "can't delete from rad$replycheck table: ". $sth->errstr;
662   }
663   $dbh->disconnect;
664 }
665
666 sub sqlradius_delete { #subroutine, not method
667   my $dbh = sqlradius_connect(shift, shift, shift);
668   my $username = shift;
669
670   foreach my $table (qw( radcheck radreply usergroup )) {
671     my $sth = $dbh->prepare( "DELETE FROM $table WHERE UserName = ?" );
672     $sth->execute($username)
673       or die "can't delete from $table table: ". $sth->errstr;
674   }
675   $dbh->disconnect;
676 }
677
678 sub sqlradius_connect {
679   #my($datasrc, $username, $password) = @_;
680   #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
681   DBI->connect(@_) or die $DBI::errstr;
682 }
683
684 =head1 NEW EXPORT CLASSES
685
686   #myexport
687   
688   package FS::part_export::myexport;
689   use vars qw(@ISA);
690   @ISA = qw(FS::part_export);
691
692   sub rebless { shift; }
693
694   sub _export_insert {
695     my($self, $svc_something) = (shift, shift);
696     $self->myexport_queue( $svc_acct->svcnum, 'insert',
697       $svc_something->username, $svc_something->password );
698   }
699   
700   sub _export_replace {
701     my( $self, $new, $old ) = (shift, shift, shift);
702     #return "can't change username with myexport"
703     #  if $old->username ne $new->username;
704     #return '' unless $old->_password ne $new->_password;
705     $self->myexport_queue( $new->svcnum,
706       'replace', $new->username, $new->password );
707   }
708   
709   sub _export_delete {
710     my( $self, $svc_something ) = (shift, shift);
711     $self->myexport_queue( $svc_acct->svcnum,
712       'delete', $svc_something->username );
713   }
714   
715   #a good idea to queue anything that could fail or take any time
716   sub myexport_queue {
717     my( $self, $svcnum, $method ) = (shift, shift, shift);
718     my $queue = new FS::queue {
719       'svcnum' => $svcnum,
720       'job'    => "FS::part_export::myexport::myexport_$method",
721     };
722     $queue->insert( @_ );
723   }
724   
725   sub myexport_insert { #subroutine, not method
726   }
727   sub myexport_replace { #subroutine, not method
728   }
729   sub myexport_delete { #subroutine, not method
730   }
731
732 =head1 BUGS
733
734 Probably.
735
736 Hmm, export code has wound up in here.  Move those sub-classes out into their
737 own files, at least.  Also hmm... cust_export class (not necessarily a
738 database table...) ... ?
739
740 =head1 SEE ALSO
741
742 L<FS::part_export_option>, L<FS::part_svc>, L<FS::svc_acct>, L<FS::svc_domain>,
743 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.
744
745 =cut
746
747 1;
748