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
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';
113 my $oldAutoCommit = $FS::UID::AutoCommit;
114 local $FS::UID::AutoCommit = 0;
117 my $error = $self->SUPER::insert;
119 $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>).
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';
203 my $oldAutoCommit = $FS::UID::AutoCommit;
204 local $FS::UID::AutoCommit = 0;
207 my $error = $self->SUPER::replace($old);
209 $dbh->rollback if $oldAutoCommit;
213 foreach my $optionname ( keys %{$options} ) {
214 my $old = qsearchs( 'part_export_option', {
215 'exportnum' => $self->exportnum,
216 'optionname' => $optionname,
218 my $new = new FS::part_export_option ( {
219 'exportnum' => $self->exportnum,
220 'optionname' => $optionname,
221 'optionvalue' => $options->{$optionname},
223 $new->optionnum($old->optionnum) if $old;
224 my $error = $old ? $new->replace($old) : $new->insert;
226 $dbh->rollback if $oldAutoCommit;
231 #remove extraneous old options
233 grep { !exists $options->{$_->optionname} } $old->part_export_option
235 my $error = $opt->delete;
237 $dbh->rollback if $oldAutoCommit;
242 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
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
259 $self->ut_numbern('exportnum')
260 || $self->ut_domain('machine')
261 || $self->ut_number('svcpart')
262 || $self->ut_alpha('exporttype')
264 return $error if $error;
266 return "Unknown svcpart: ". $self->svcpart
267 unless qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
269 $self->machine =~ /^([\w\-\.]*)$/
270 or return "Illegal machine: ". $self->machine;
273 $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
283 Returns the service definition (see L<FS::part_svc>) for this export.
289 qsearchs('part_svc', { svcpart => $self->svcpart } );
292 =item part_export_option
294 Returns all options as FS::part_export_option objects (see
295 L<FS::part_export_option>).
299 sub part_export_option {
301 qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
306 Returns a list of option names and values suitable for assigning to a hash.
312 map { $_->optionname => $_->optionvalue } $self->part_export_option;
315 =item option OPTIONNAME
317 Returns the option value for the given name, or the empty string.
323 my $part_export_option =
324 qsearchs('part_export_option', {
325 exportnum => $self->exportnum,
328 $part_export_option ? $part_export_option->optionvalue : '';
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>.
342 my $exporttype = $self->exporttype;
343 my $class = ref($self);
344 bless($self, $class."::$exporttype");
347 =item export_insert SVC_OBJECT
354 $self->_export_insert(@_);
360 # my $method = $AUTOLOAD;
361 # #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
362 # $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
363 # $self->$method(@_);
366 =item export_replace NEW OLD
373 $self->_export_replace(@_);
383 $self->_export_delete(@_);
386 #fallbacks providing useful error messages intead of infinite loops
389 return "_export_insert: unknown export type ". $self->exporttype;
392 sub _export_replace {
394 return "_export_replace: unknown export type ". $self->exporttype;
399 return "_export_delete: unknown export type ". $self->exporttype;
408 package FS::part_export::infostreet;
410 @ISA = qw(FS::part_export);
413 my( $self, $svc_acct ) = (shift, shift);
414 $self->infostreet_queue( $svc_acct->svcnum,
415 'createUser', $svc_acct->username, $svc_acct->password );
418 sub _export_replace {
419 my( $self, $new, $old ) = (shift, shift, shift);
420 return "can't change username with InfoStreet"
421 if $old->username ne $new->username;
422 return '' unless $old->_password ne $new->_password;
423 $self->infostreet_queue( $new->svcnum,
424 'passwd', $new->username, $new->password );
428 my( $self, $svc_acct ) = (shift, shift);
429 $self->infostreet_queue( $svc_acct->svcnum,
430 'purgeAccount,releaseUsername', $svc_acct->username );
433 sub infostreet_queue {
434 my( $self, $svcnum, $method ) = (shift, shift, shift);
435 my $queue = new FS::queue {
437 'job' => 'FS::part_export::infostreet::infostreet_command',
440 $self->option('url'),
441 $self->option('login'),
442 $self->option('password'),
443 $self->option('groupID'),
449 sub infostreet_command { #subroutine, not method
450 my($url, $username, $password, $groupID, $method, @args) = @_;
453 if ( $method =~ /,/ ) {
454 foreach my $part ( split(/,\s*/, $method) ) {
455 infostreet_command($url, $username, $password, $groupID, $part, @args);
460 eval "use Frontier::Client;";
462 my $conn = Frontier::Client->new( url => $url );
463 my $key_result = $conn->call( 'authenticate', $username, $password, $groupID);
464 my %key_result = _infostreet_parse($key_result);
465 die $key_result{error} unless $key_result{success};
466 my $key = $key_result{data};
468 my $result = $conn->call($method, $key, @args);
469 my %result = _infostreet_parse($result);
470 die $result{error} unless $result{success};
474 sub _infostreet_parse { #subroutine, not method
477 my $value = $arg->{$_};
479 $value = $value->value()
480 if ref($value) && $value->isa('Frontier::RPC2::DataType');
487 package FS::part_export::sqlradius;
489 @ISA = qw(FS::part_export);
492 my($self, $svc_acct) = (shift, shift);
494 foreach my $table (qw(reply check)) {
495 my $method = "radius_$table";
496 my %attrib = $svc_acct->$method;
497 next unless keys %attrib;
498 my $error = $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
499 $table, $svc_acct->username, %attrib );
500 return $error if $error;
502 my @groups = $svc_acct->radius_groups;
504 my $error = $self->sqlradius_queue( $svc_acct->svcnum, 'usergroup_insert',
505 $svc_acct->username, @groups );
506 return $error if $error;
511 sub _export_replace {
512 my( $self, $new, $old ) = (shift, shift, shift);
514 #return "can't (yet) change username with sqlradius"
515 # if $old->username ne $new->username;
516 if ( $old->username ne $new->username ) {
517 my $error = $self->sqlradius_queue( $new->svcnum, 'rename',
518 $new->username, $old->username );
519 return $error if $error;
522 foreach my $table (qw(reply check)) {
523 my $method = "radius_$table";
524 my %new = $new->$method;
525 my %old = $old->$method;
526 if ( grep { !exists $old{$_} #new attributes
527 || $new{$_} ne $old{$_} #changed
530 my $error = $self->sqlradius_queue( $new->svcnum, 'insert',
531 $table, $new->username, %new );
532 return $error if $error;
535 my @del = grep { !exists $new{$_} } keys %old;
537 my $error = $self->sqlradius_queue( $new->svcnum, 'attrib_delete',
538 $table, $new->username, @del );
539 return $error if $error;
543 # (sorta) false laziness with FS::svc_acct::replace
544 my @oldgroups = @{$old->usergroup}; #uuuh
545 my @newgroups = $new->radius_groups;
547 foreach my $oldgroup ( @oldgroups ) {
548 if ( grep { $oldgroup eq $_ } @newgroups ) {
549 @newgroups = grep { $oldgroup ne $_ } @newgroups;
552 push @delgroups, $oldgroup;
556 my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_delete',
557 $new->username, @delgroups );
558 return $error if $error;
562 my $error = $self->sqlradius_queue( $new->svcnum, 'usergroup_insert',
563 $new->username, @newgroups );
564 return $error if $error;
571 my( $self, $svc_acct ) = (shift, shift);
572 $self->sqlradius_queue( $svc_acct->svcnum, 'delete',
573 $svc_acct->username );
576 sub sqlradius_queue {
577 my( $self, $svcnum, $method ) = (shift, shift, shift);
578 my $queue = new FS::queue {
580 'job' => "FS::part_export::sqlradius::sqlradius_$method",
583 $self->option('datasrc'),
584 $self->option('username'),
585 $self->option('password'),
590 sub sqlradius_insert { #subroutine, not method
591 my $dbh = sqlradius_connect(shift, shift, shift);
592 my( $replycheck, $username, %attributes ) = @_;
594 foreach my $attribute ( keys %attributes ) {
595 my $u_sth = $dbh->prepare(
596 "UPDATE rad$replycheck SET Value = ? WHERE UserName = ? AND Attribute = ?" ) or die $dbh->errstr;
597 my $i_sth = $dbh->prepare(
598 "INSERT INTO rad$replycheck ( id, UserName, Attribute, Value ) ".
599 "VALUES ( ?, ?, ?, ? )"
600 ) or die $dbh->errstr;
601 $u_sth->execute($attributes{$attribute}, $username, $attribute) > 0
602 or $i_sth->execute( '', $username, $attribute, $attributes{$attribute} )
603 or die "can't insert into rad$replycheck table: ". $i_sth->errstr;
608 sub sqlradius_usergroup_insert { #subroutine, not method
609 my $dbh = sqlradius_connect(shift, shift, shift);
610 my( $username, @groups ) = @_;
612 my $sth = $dbh->prepare(
613 "INSERT INTO usergroup ( id, UserName, GroupName ) VALUES ( ?, ?, ? )"
614 ) or die $dbh->errstr;
615 foreach my $group ( @groups ) {
616 $sth->execute( '', $username, $group )
617 or die "can't insert into groupname table: ". $sth->errstr;
622 sub sqlradius_usergroup_delete { #subroutine, not method
623 my $dbh = sqlradius_connect(shift, shift, shift);
624 my( $username, @groups ) = @_;
626 my $sth = $dbh->prepare(
627 "DELETE FROM usergroup ( id, UserName, GroupName ) VALUES ( ?, ?, ? )"
628 ) or die $dbh->errstr;
629 foreach my $group ( @groups ) {
630 $sth->execute( '', $username, $group )
631 or die "can't delete from groupname table: ". $sth->errstr;
636 sub sqlradius_rename { #subroutine, not method
637 my $dbh = sqlradius_connect(shift, shift, shift);
638 my($new_username, $old_username) = @_;
639 foreach my $table (qw(radreply radcheck usergroup )) {
640 my $sth = $dbh->prepare("UPDATE $table SET Username = ? WHERE UserName = ?")
642 $sth->execute($new_username, $old_username)
643 or die "can't update $table: ". $sth->errstr;
648 sub sqlradius_attrib_delete { #subroutine, not method
649 my $dbh = sqlradius_connect(shift, shift, shift);
650 my( $replycheck, $username, @attrib ) = @_;
652 foreach my $attribute ( @attrib ) {
653 my $sth = $dbh->prepare(
654 "DELETE FROM rad$replycheck WHERE UserName = ? AND Attribute = ?" )
656 $sth->execute($username,$attribute)
657 or die "can't delete from rad$replycheck table: ". $sth->errstr;
662 sub sqlradius_delete { #subroutine, not method
663 my $dbh = sqlradius_connect(shift, shift, shift);
664 my $username = shift;
666 foreach my $table (qw( radcheck radreply usergroup )) {
667 my $sth = $dbh->prepare( "DELETE FROM $table WHERE UserName = ?" );
668 $sth->execute($username)
669 or die "can't delete from $table table: ". $sth->errstr;
674 sub sqlradius_connect {
675 #my($datasrc, $username, $password) = @_;
676 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
677 DBI->connect(@_) or die $DBI::errstr;
680 =head1 NEW EXPORT CLASSES
684 package FS::part_export::myexport;
686 @ISA = qw(FS::part_export);
689 my($self, $svc_something) = (shift, shift);
690 $self->myexport_queue( $svc_acct->svcnum, 'insert',
691 $svc_something->username, $svc_something->password );
694 sub _export_replace {
695 my( $self, $new, $old ) = (shift, shift, shift);
696 #return "can't change username with myexport"
697 # if $old->username ne $new->username;
698 #return '' unless $old->_password ne $new->_password;
699 $self->myexport_queue( $new->svcnum,
700 'replace', $new->username, $new->password );
704 my( $self, $svc_something ) = (shift, shift);
705 $self->myexport_queue( $svc_acct->svcnum,
706 'delete', $svc_something->username );
709 #a good idea to queue anything that could fail or take any time
711 my( $self, $svcnum, $method ) = (shift, shift, shift);
712 my $queue = new FS::queue {
714 'job' => "FS::part_export::myexport::myexport_$method",
716 $queue->insert( @_ );
719 sub myexport_insert { #subroutine, not method
721 sub myexport_replace { #subroutine, not method
723 sub myexport_delete { #subroutine, not method
730 Hmm, export code has wound up in here. Move those sub-classes out into their
731 own files, at least. Also hmm... cust_export class (not necessarily a
732 database table...) ... ?
736 L<FS::part_export_option>, L<FS::part_svc>, L<FS::svc_acct>, L<FS::svc_domain>,
737 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.