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 #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;
233 # $dbh->rollback if $oldAutoCommit;
238 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
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
255 $self->ut_numbern('exportnum')
256 || $self->ut_number('svcpart')
257 || $self->ut_alpha('exporttype')
259 return $error if $error;
261 return "Unknown svcpart: ". $self->svcpart
262 unless qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
264 $self->machine =~ /^([\w\-\.]*)$/
265 or return "Illegal machine: ". $self->machine;
268 $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
278 Returns the service definition (see L<FS::part_svc>) for this export.
284 qsearchs('part_svc', { svcpart => $self->svcpart } );
287 =item part_export_option
291 sub part_export_option {
293 qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
302 map { $_->optionname => $_->optionvalue } $self->part_export_option;
311 my $part_export_option =
312 qsearchs('part_export_option', {
313 exportnum => $self->exportnum,
316 $part_export_option ? $part_export_option->optionvalue : '';
325 my $exporttype = $self->exporttype;
326 my $class = ref($self);
327 bless($self, $class."::$exporttype");
330 =item export_insert SVC_OBJECT
337 $self->_export_insert(@_);
343 # my $method = $AUTOLOAD;
344 # #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
345 # $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
346 # $self->$method(@_);
356 $self->_export_replace(@_);
366 $self->_export_delete(@_);
375 package FS::part_export::infostreet;
377 @ISA = qw(FS::part_export);
380 my( $self, $svc_acct ) = (shift, shift);
381 $self->infostreet_queue( $svc_acct->svcnum,
382 'createUser', $svc_acct->username, $svc_acct->password );
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 );
395 my( $self, $svc_acct ) = (shift, shift);
396 $self->infostreet_queue( $svc_acct->svcnum,
397 'purgeAccount,releaseUsername', $svc_acct->username );
400 sub infostreet_queue {
401 my( $self, $svcnum, $method ) = (shift, shift, shift);
402 my $queue = new FS::queue {
404 'job' => 'FS::part_export::infostreet::infostreet_command',
407 $self->option('url'),
408 $self->option('login'),
409 $self->option('password'),
410 $self->option('groupID'),
416 sub infostreet_command { #subroutine, not method
417 my($url, $username, $password, $groupID, $method, @args) = @_;
420 if ( $method =~ /,/ ) {
421 foreach my $part ( split(/,\s*/, $method) ) {
422 infostreet_command($url, $username, $password, $groupID, $part, @args);
427 eval "use Frontier::Client;";
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};
435 my $result = $conn->call($method, $key, @args);
436 my %result = _infostreet_parse($result);
437 die $result{error} unless $result{success};
441 sub _infostreet_parse { #subroutine, not method
444 my $value = $arg->{$_};
446 $value = $value->value()
447 if ref($value) && $value->isa('Frontier::RPC2::DataType');
454 package FS::part_export::sqlradius;
456 @ISA = qw(FS::part_export);
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 );
466 sub _export_replace {
467 my( $self, $new, $old ) = (shift, shift, shift);
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;
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
485 my $error = $self->sqlradius_queue( $new->svcnum, 'insert',
486 $table, $new->username, %new );
487 return $error if $error;
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;
500 my( $self, $svc_acct ) = (shift, shift);
501 $self->sqlradius_queue( $svc_acct->svcnum, 'delete',
502 $svc_acct->username );
505 sub sqlradius_queue {
506 my( $self, $svcnum, $method ) = (shift, shift, shift);
507 my $queue = new FS::queue {
509 'job' => "FS::part_export::sqlradius::sqlradius_$method",
512 $self->option('datasrc'),
513 $self->option('username'),
514 $self->option('password'),
519 sub sqlradius_insert { #subroutine, not method
520 my $dbh = sqlradius_connect(shift, shift, shift);
521 my( $replycheck, $username, %attributes ) = @_;
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 ( ?, ?, ?, ? )" )
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;
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 = ?")
543 $sth->execute($new_username, $old_username)
544 or die "can't update $table: ". $sth->errstr;
549 sub sqlradius_attrib_delete { #subroutine, not method
550 my $dbh = sqlradius_connect(shift, shift, shift);
551 my( $replycheck, $username, @attrib ) = @_;
553 foreach my $attribute ( @attrib ) {
554 my $sth = $dbh->prepare(
555 "DELETE FROM rad$replycheck WHERE UserName = ? AND Attribute = ?" )
557 $sth->execute($username,$attribute)
558 or die "can't delete from rad$replycheck table: ". $sth->errstr;
563 sub sqlradius_delete { #subroutine, not method
564 my $dbh = sqlradius_connect(shift, shift, shift);
565 my $username = shift;
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;
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;
581 =head1 NEW EXPORT CLASSES
585 package FS::part_export::myexport;
587 @ISA = qw(FS::part_export);
590 my($self, $svc_something) = (shift, shift);
591 $self->myexport_queue( $svc_acct->svcnum, 'insert',
592 $svc_something->username, $svc_something->password );
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 );
605 my( $self, $svc_something ) = (shift, shift);
606 $self->myexport_queue( $svc_acct->svcnum,
607 'delete', $svc_something->username );
610 #a good idea to queue anything that could fail or take any time
612 my( $self, $svcnum, $method ) = (shift, shift, shift);
613 my $queue = new FS::queue {
615 'job' => "FS::part_export::myexport::myexport_$method",
617 $queue->insert( @_ );
620 sub myexport_insert { #subroutine, not method
622 sub myexport_replace { #subroutine, not method
624 sub myexport_delete { #subroutine, not method
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...) ... ?
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.