1 package FS::part_export;
5 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::part_export_option;
13 FS::part_export - Object methods for part_export records
19 $record = new FS::part_export \%hash;
20 $record = new FS::part_export { 'column' => 'value' };
22 ($new_record, $options) = $template_recored->clone( $svcpart );
24 $error = $record->insert( { 'option' => 'value' } );
25 $error = $record->insert( \$options );
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
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:
41 =item exportnum - primary key
43 =item svcpart - Service definition (see L<FS::part_svc>) to which this export applies
45 =item machine - Machine name
47 =item exporttype - Export type
49 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
59 Creates a new export. To add the export to the database, see L<"insert">.
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.
66 # the new method can be inherited from FS::Record, if a table method is defined
68 sub table { 'part_export'; }
72 An alternate constructor. Creates a new export by duplicating an existing
73 export. The given svcpart is assigned to the new export.
75 Returns a list consisting of the new export object and a hashref of options.
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 } )
94 Adds this record to the database. If there is an error, returns the error,
95 otherwise returns false.
97 If a hash reference of options is supplied, part_export_option records are
98 created (see L<FS::part_export_option>).
102 #false laziness w/queue.pm
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';
112 my $oldAutoCommit = $FS::UID::AutoCommit;
113 local $FS::UID::AutoCommit = 0;
116 my $error = $self->SUPER::insert;
118 $dbh->rollback if $oldAutoCommit;
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},
129 $error = $part_export_option->insert;
131 $dbh->rollback if $oldAutoCommit;
136 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
144 Delete this record from the database.
148 #foreign keys would make this much less tedious... grr dumb mysql
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';
158 my $oldAutoCommit = $FS::UID::AutoCommit;
159 local $FS::UID::AutoCommit = 0;
162 my $error = $self->SUPER::delete;
164 $dbh->rollback if $oldAutoCommit;
168 foreach my $part_export_option ( $self->part_export_option ) {
169 my $error = $part_export_option->delete;
171 $dbh->rollback if $oldAutoCommit;
176 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
182 =item replace OLD_RECORD HASHREF
184 Replaces the OLD_RECORD with this one in the database. If there is an error,
185 returns the error, otherwise returns false.
187 If a hash reference of options is supplied, part_export_option records are
188 created or modified (see L<FS::part_export_option>).
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';
201 my $oldAutoCommit = $FS::UID::AutoCommit;
202 local $FS::UID::AutoCommit = 0;
205 my $error = $self->SUPER::replace;
207 $dbh->rollback if $oldAutoCommit;
212 foreach my $optionname ( keys %{$options} ) {
213 my $old = qsearchs( 'part_export_option', {
214 'exportnum' => $self->exportnum,
215 'optionname' => $optionname,
217 my $new = new FS::part_export_option ( {
218 'exportnum' => $self->exportnum,
219 'optionname' => $optionname,
220 'optionvalue' => $options->{$optionname},
222 my $error = $old ? $new->replace($old) : $new->insert;
224 $dbh->rollback if $oldAutoCommit;
229 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
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
246 $self->ut_numbern('exportnum')
247 || $self->ut_number('svcpart')
248 || $self->ut_alpha('exporttype')
250 return $error if $error;
252 return "Unknown svcpart: ". $self->svcpart
253 unless qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
255 $self->machine =~ /^([\w\-\.]*)$/
256 or return "Illegal machine: ". $self->machine;
259 $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
269 Returns the service definition (see L<FS::part_svc>) for this export.
275 qsearchs('part_svc', { svcpart => $self->svcpart } );
278 =item part_export_option
282 sub part_export_option {
284 qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
293 map { $_->optionname => $_->optionvalue } $self->part_export_option;
302 my $part_export_option =
303 qsearchs('part_export_option', {
304 exportnum => $self->exportnum,
307 $part_export_option ? $part_export_option->optionvalue : '';
316 my $exporttype = $self->exporttype;
317 my $class = ref($self);
318 bless($self, $class."::$exporttype");
321 =item export_insert SVC_OBJECT
323 Calls the appropriate export_I<exporttype> for this object's exporttype.
330 $self->_export_insert(@_);
336 # my $method = $AUTOLOAD;
337 # #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
338 # $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
339 # $self->$method(@_);
349 $self->_export_replace(@_);
359 $self->_export_delete(@_);
366 package FS::part_export::infostreet;
368 @ISA = qw(FS::part_export);
371 my( $self, $svc_acct ) = (shift, shift);
372 $self->infostreet_queue( $svc_acct->svcnum,
373 'createUser', $svc_acct->username, $svc_acct->password );
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 );
386 my( $self, $svc_acct ) = (shift, shift);
387 $self->infostreet_queue( $svc_acct->svcnum,
388 'purgeAccount,releaseUsername', $svc_acct->username );
391 sub infostreet_queue {
392 my( $self, $svcnum, $method ) = (shift, shift, shift);
393 my $queue = new FS::queue {
395 'job' => 'FS::part_export::infostreet::infostreet_command',
398 $self->option('url'),
399 $self->option('login'),
400 $self->option('password'),
401 $self->option('groupID'),
407 sub infostreet_command { #subroutine, not method
408 my($url, $username, $password, $groupID, $method, @args) = @_;
411 if ( $method =~ /,/ ) {
412 foreach my $part ( split(/,\s*/, $method) ) {
413 infostreet_command($url, $username, $password, $groupID, $part, @args);
418 eval "use Frontier::Client;";
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};
426 my $result = $conn->call($opt{method}, $key, @{$opt{args}});
427 my %result = _infostreet_parse($result);
428 die $result{error} unless $result{success};
432 sub _infostreet_parse { #subroutine, not method
435 my $value = $arg->{$_};
437 $value = $value->value()
438 if ref($value) && $value->isa('Frontier::RPC2::DataType');
445 package FS::part_export::sqlradius;
447 @ISA = qw(FS::part_export);
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 );
457 sub _export_replace {
458 my( $self, $new, $old ) = (shift, shift, shift);
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;
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
476 my $error = $self->sqlradius_queue( $new->svcnum, 'insert'
477 $table, $new->username, %new );
478 return $error if $error;
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;
491 my( $self, $svc_something ) = (shift, shift);
492 $self->sqlradius_queue( $svc_acct->svcnum, 'delete',
493 $svc_something->username );
496 sub sqlradius_queue {
497 my( $self, $svcnum, $method ) = (shift, shift, shift);
498 my $queue = new FS::queue {
500 'job' => "FS::part_export::sqlradius::sqlradius_$method",
503 $self->option('datasrc'),
504 $self->option('username'),
505 $self->option('password'),
510 sub sqlradius_insert { #subroutine, not method
511 my $dbh = sqlradius_connect(shift, shift, shift);
512 my( $replycheck, $username, %attributes ) = @_;
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 ( ?, ?, ?, ? )" )
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;
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 = ?")
534 $sth->execute($new_username, $old_username)
535 or die "can't update $table: ". $sth->errstr;
540 sub sqlradius_attrib_delete { #subroutine, not method
541 my $dbh = sqlradius_connect(shift, shift, shift);
542 my( $replycheck, $username, @attrib ) = @_;
544 foreach my $attribute ( @attrib ) {
545 my $sth = $dbh->prepare(
546 "DELETE FROM $table WHERE UserName = ? AND Attribute = ?" )
548 $sth->execute($username,$attribute)
549 or die "can't delete from $table table: ". $sth->errstr;
554 sub sqlradius_delete { #subroutine, not method
555 my $dbh = sqlradius_connect(shift, shift, shift);
556 my $username = shift;
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;
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;
574 Writing a new export class:
578 package FS::part_export::myexport;
580 @ISA = qw(FS::part_export);
583 my($self, $svc_something) = (shift, shift);
584 $self->myexport_queue( $svc_acct->svcnum, 'insert',
585 $svc_something->username, $svc_something->password );
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 );
598 my( $self, $svc_something ) = (shift, shift);
599 $self->myexport_queue( $svc_acct->svcnum,
600 'delete', $svc_something->username );
603 #a good idea to queue anything that could fail or take any time
605 my( $self, $svcnum, $method ) = (shift, shift, shift);
606 my $queue = new FS::queue {
608 'job' => "FS::part_export::myexport::myexport_$method",
610 $queue->insert( @_ );
613 sub myexport_insert { #subroutine, not method
615 sub myexport_replace { #subroutine, not method
617 sub myexport_delete { #subroutine, not method
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...) ... ?
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.