1 package FS::part_export;
4 use vars qw( @ISA @EXPORT_OK %exports );
7 use FS::Record qw( qsearch qsearchs dbh );
9 use FS::part_export_option;
12 @ISA = qw(FS::Record);
13 @EXPORT_OK = qw(export_info);
17 FS::part_export - Object methods for part_export records
23 $record = new FS::part_export \%hash;
24 $record = new FS::part_export { 'column' => 'value' };
26 #($new_record, $options) = $template_recored->clone( $svcpart );
28 $error = $record->insert( { 'option' => 'value' } );
29 $error = $record->insert( \%options );
31 $error = $new_record->replace($old_record);
33 $error = $record->delete;
35 $error = $record->check;
39 An FS::part_export object represents an export of Freeside data to an external
40 provisioning system. FS::part_export inherits from FS::Record. The following
41 fields are currently supported:
45 =item exportnum - primary key
47 =item machine - Machine name
49 =item exporttype - Export type
51 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
61 Creates a new export. To add the export to the database, see L<"insert">.
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to. You can ask the object for a copy with the I<hash> method.
68 # the new method can be inherited from FS::Record, if a table method is defined
70 sub table { 'part_export'; }
76 #An alternate constructor. Creates a new export by duplicating an existing
77 #export. The given svcpart is assigned to the new export.
79 #Returns a list consisting of the new export object and a hashref of options.
85 # my $class = ref($self);
86 # my %hash = $self->hash;
87 # $hash{'exportnum'} = '';
88 # $hash{'svcpart'} = shift;
89 # ( $class->new( \%hash ),
90 # { map { $_->optionname => $_->optionvalue }
91 # qsearch('part_export_option', { 'exportnum' => $self->exportnum } )
98 Adds this record to the database. If there is an error, returns the error,
99 otherwise returns false.
101 If a hash reference of options is supplied, part_export_option records are
102 created (see L<FS::part_export_option>).
106 #false laziness w/queue.pm
110 local $SIG{HUP} = 'IGNORE';
111 local $SIG{INT} = 'IGNORE';
112 local $SIG{QUIT} = 'IGNORE';
113 local $SIG{TERM} = 'IGNORE';
114 local $SIG{TSTP} = 'IGNORE';
115 local $SIG{PIPE} = 'IGNORE';
117 my $oldAutoCommit = $FS::UID::AutoCommit;
118 local $FS::UID::AutoCommit = 0;
121 my $error = $self->SUPER::insert;
123 $dbh->rollback if $oldAutoCommit;
127 foreach my $optionname ( keys %{$options} ) {
128 my $part_export_option = new FS::part_export_option ( {
129 'exportnum' => $self->exportnum,
130 'optionname' => $optionname,
131 'optionvalue' => $options->{$optionname},
133 $error = $part_export_option->insert;
135 $dbh->rollback if $oldAutoCommit;
140 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
148 Delete this record from the database.
152 #foreign keys would make this much less tedious... grr dumb mysql
155 local $SIG{HUP} = 'IGNORE';
156 local $SIG{INT} = 'IGNORE';
157 local $SIG{QUIT} = 'IGNORE';
158 local $SIG{TERM} = 'IGNORE';
159 local $SIG{TSTP} = 'IGNORE';
160 local $SIG{PIPE} = 'IGNORE';
162 my $oldAutoCommit = $FS::UID::AutoCommit;
163 local $FS::UID::AutoCommit = 0;
166 my $error = $self->SUPER::delete;
168 $dbh->rollback if $oldAutoCommit;
172 foreach my $part_export_option ( $self->part_export_option ) {
173 my $error = $part_export_option->delete;
175 $dbh->rollback if $oldAutoCommit;
180 foreach my $export_svc ( $self->export_svc ) {
181 my $error = $export_svc->delete;
183 $dbh->rollback if $oldAutoCommit;
188 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
194 =item replace OLD_RECORD HASHREF
196 Replaces the OLD_RECORD with this one in the database. If there is an error,
197 returns the error, otherwise returns false.
199 If a hash reference of options is supplied, part_export_option records are
200 created or modified (see L<FS::part_export_option>).
208 local $SIG{HUP} = 'IGNORE';
209 local $SIG{INT} = 'IGNORE';
210 local $SIG{QUIT} = 'IGNORE';
211 local $SIG{TERM} = 'IGNORE';
212 local $SIG{TSTP} = 'IGNORE';
213 local $SIG{PIPE} = 'IGNORE';
215 my $oldAutoCommit = $FS::UID::AutoCommit;
216 local $FS::UID::AutoCommit = 0;
219 my $error = $self->SUPER::replace($old);
221 $dbh->rollback if $oldAutoCommit;
225 foreach my $optionname ( keys %{$options} ) {
226 my $old = qsearchs( 'part_export_option', {
227 'exportnum' => $self->exportnum,
228 'optionname' => $optionname,
230 my $new = new FS::part_export_option ( {
231 'exportnum' => $self->exportnum,
232 'optionname' => $optionname,
233 'optionvalue' => $options->{$optionname},
235 $new->optionnum($old->optionnum) if $old;
236 my $error = $old ? $new->replace($old) : $new->insert;
238 $dbh->rollback if $oldAutoCommit;
243 #remove extraneous old options
245 grep { !exists $options->{$_->optionname} } $old->part_export_option
247 my $error = $opt->delete;
249 $dbh->rollback if $oldAutoCommit;
254 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
262 Checks all fields to make sure this is a valid export. If there is
263 an error, returns the error, otherwise returns false. Called by the insert
271 $self->ut_numbern('exportnum')
272 || $self->ut_domain('machine')
273 || $self->ut_alpha('exporttype')
275 return $error if $error;
277 warn $self->machine. "!!!\n";
279 $self->machine =~ /^([\w\-\.]*)$/
280 or return "Illegal machine: ". $self->machine;
283 $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
286 $self->deprecated(1); #BLAH
295 #Returns the service definition (see L<FS::part_svc>) for this export.
301 # qsearchs('part_svc', { svcpart => $self->svcpart } );
306 croak "FS::part_export::part_svc deprecated";
307 #confess "FS::part_export::part_svc deprecated";
312 Returns a list of associated FS::export_svc records.
318 qsearch('export_svc', { 'exportnum' => $self->exportnum } );
321 =item part_export_option
323 Returns all options as FS::part_export_option objects (see
324 L<FS::part_export_option>).
328 sub part_export_option {
330 qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
335 Returns a list of option names and values suitable for assigning to a hash.
341 map { $_->optionname => $_->optionvalue } $self->part_export_option;
344 =item option OPTIONNAME
346 Returns the option value for the given name, or the empty string.
352 my $part_export_option =
353 qsearchs('part_export_option', {
354 exportnum => $self->exportnum,
357 $part_export_option ? $part_export_option->optionvalue : '';
362 Reblesses the object into the FS::part_export::EXPORTTYPE class, where
363 EXPORTTYPE is the object's I<exporttype> field. There should be better docs
364 on how to create new exports (and they should live in their own files and be
365 autoloaded-on-demand), but until then, see L</NEW EXPORT CLASSES>.
371 my $exporttype = $self->exporttype;
372 my $class = ref($self). "::$exporttype";
373 # eval "use $class;" or die $@;
375 bless($self, $class);
378 =item export_insert SVC_OBJECT
385 $self->_export_insert(@_);
391 # my $method = $AUTOLOAD;
392 # #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
393 # $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
394 # $self->$method(@_);
397 =item export_replace NEW OLD
404 $self->_export_replace(@_);
414 $self->_export_delete(@_);
417 #fallbacks providing useful error messages intead of infinite loops
420 return "_export_insert: unknown export type ". $self->exporttype;
423 sub _export_replace {
425 return "_export_replace: unknown export type ". $self->exporttype;
430 return "_export_delete: unknown export type ". $self->exporttype;
439 =item export_info [ SVCDB ]
441 Returns a hash reference of the exports for the given I<svcdb>, or if no
442 I<svcdb> is specified, for all exports. The keys of the hash are
443 I<exporttype>s and the values are again hash references containing information
446 'desc' => 'Description',
448 'option' => { label=>'Option Label' },
449 'option2' => { label=>'Another label' },
451 'nodomain' => 'Y', #or ''
452 'notes' => 'Additional notes',
458 return $exports{$_[0]} if @_;
459 #{ map { %{$exports{$_}} } keys %exports };
460 my $r = { map { %{$exports{$_}} } keys %exports };
463 =item exporttype2svcdb EXPORTTYPE
465 Returns the applicable I<svcdb> for an I<exporttype>.
469 # This subroutine should be modified or removed. In its present form, it
470 # imposes the arbitrary restriction that no export type can be associated
471 # with more than one svcdb. The only place it's used is in edit/part_svc.cgi
472 # to generate the list of allowed exports, which can be done more cleanly by
473 # export_info anyway.
475 sub exporttype2svcdb {
476 my $exporttype = $_[0];
477 foreach my $svcdb ( keys %exports ) {
478 return $svcdb if grep { $exporttype eq $_ } keys %{$exports{$svcdb}};
483 tie my %shellcommands_options, 'Tie::IxHash',
484 #'machine' => { label=>'Remote machine' },
485 'user' => { label=>'Remote username', default=>'root' },
486 'useradd' => { label=>'Insert command',
487 default=>'useradd -d $dir -m -s $shell -u $uid $username'
488 #default=>'cp -pr /etc/skel $dir; chown -R $uid.$gid $dir'
490 'userdel' => { label=>'Delete command',
491 default=>'userdel $username',
492 #default=>'rm -rf $dir',
494 'usermod' => { label=>'Modify command',
495 default=>'usermod -d $new_dir -l $new_username -s $new_shell -u $new_uid $old_username',
496 #default=>'[ -d $old_dir ] && mv $old_dir $new_dir || ( '.
497 # 'chmod u+t $old_dir; mkdir $new_dir; cd $old_dir; '.
498 # 'find . -depth -print | cpio -pdm $new_dir; '.
499 # 'chmod u-t $new_dir; chown -R $uid.$gid $new_dir; '.
505 tie my %sqlradius_options, 'Tie::IxHash',
506 'datasrc' => { label=>'DBI data source ' },
507 'username' => { label=>'Database username' },
508 'password' => { label=>'Database password' },
511 tie my %cyrus_options, 'Tie::IxHash',
512 'server' => { label=>'IMAP server' },
513 'username' => { label=>'Admin username' },
514 'password' => { label=>'Admin password' },
517 tie my %cp_options, 'Tie::IxHash',
518 'host' => { label=>'Hostname' },
519 'port' => { label=>'Port number' },
520 'username' => { label=>'Username' },
521 'password' => { label=>'Password' },
522 'domain' => { label=>'Domain' },
523 'workgroup' => { label=>'Default Workgroup' },
526 tie my %infostreet_options, 'Tie::IxHash',
527 'url' => { label=>'XML-RPC Access URL', },
528 'login' => { label=>'InfoStreet login', },
529 'password' => { label=>'InfoStreet password', },
530 'groupID' => { label=>'InfoStreet groupID', },
533 tie my %vpopmail_options, 'Tie::IxHash',
534 'machine' => { label=>'vpopmail machine', },
535 'dir' => { label=>'directory', }, # ?more info? default?
536 'uid' => { label=>'vpopmail uid' },
537 'gid' => { label=>'vpopmail gid' },
540 tie my %bind_options, 'Tie::IxHash',
541 #'machine' => { label=>'named machine' },
542 'named_conf' => { label => 'named.conf location',
543 default=> '/etc/bind/named.conf' },
544 'zonepath' => { label => 'path to zone files',
545 default=> '/etc/bind/', },
548 tie my %bind_slave_options, 'Tie::IxHash',
549 #'machine' => { label=> 'Slave machine' },
550 'master' => { label=> 'Master IP address(s) (semicolon-separated)' },
551 'named_conf' => { label => 'named.conf location',
552 default => '/etc/bind/named.conf' },
555 tie my %sqlmail_options, 'Tie::IxHash',
556 'datasrc' => { label=>'DBI data source' },
557 'username' => { label=>'Database username' },
558 'password' => { label=>'Database password' },
562 #export names cannot have dashes...
567 'Batch export of /etc/passwd and /etc/shadow files (Linux/SysV)',
572 'Batch export of /etc/passwd and /etc/master.passwd files (BSD)',
577 # 'Batch export of /etc/global/passwd and /etc/global/shadow for NIS ',
581 'desc' => 'Batch export of a text /etc/raddb/users file (Livingston, Cistron)',
586 'desc' => 'Real-time export via remote SSH (i.e. useradd, userdel, etc.)',
587 'options' => \%shellcommands_options,
589 'notes' => 'shellcommandsnotes... (this one is the nodomain one)',
593 'desc' => 'Real-time export to SQL-backed RADIUS (ICRADIUS, FreeRADIUS)',
594 'options' => \%sqlradius_options,
596 'notes' => 'Real-time export of radcheck, radreply and usergroup tables to any SQL database for <a href="http://www.freeradius.org/">FreeRADIUS</a> or <a href="http://radius.innercite.com/">ICRADIUS</a>. Use <a href="../docs/man/bin/freeside-sqlradius-reset">freeside-sqlradius-reset</a> to delete and repopulate the tables from the Freeside database. See the <a href="http://search.cpan.org/doc/TIMB/DBI-1.23/DBI.pm">DBI documentation</a> and the <a href="http://search.cpan.org/search?mode=module&query=DBD%3A%3A">documentation for your DBD</a> for the exact syntax of a DBI data source. If using <a href="http://www.freeradius.org/">FreeRADIUS</a> 0.5 or above, make sure your <b>op</b> fields are set to allow NULL values.',
600 'desc' => 'Real-time export to SQL-backed mail server',
601 'options' => \%sqlmail_options,
603 'notes' => 'Database schema can be made to work with Courier IMAP and
604 Exim. Others could work but are untested.',
608 'desc' => 'Real-time export to Cyrus IMAP server',
609 'options' => \%cyrus_options,
611 'notes' => 'Integration with <a href="http://asg.web.cmu.edu/cyrus/imapd/">Cyrus IMAP Server</a>. Cyrus::IMAP::Admin should be installed locally and the connection to the server secured. <B>svc_acct.quota</B>, if available, is used to set the Cyrus quota. '
615 'desc' => 'Real-time export to Critical Path Account Provisioning Protocol',
616 'options' => \%cp_options,
617 'notes' => 'Real-time export to <a href="http://www.cp.net/">Critial Path Account Provisioning Protocol</a>. Requires installation of <a href="http://search.cpan.org/search?dist=Net-APP">Net::APP</a> from CPAN.',
621 'desc' => 'Real-time export to InfoStreet streetSmartAPI',
622 'options' => \%infostreet_options,
624 'notes' => 'Real-time export to <a href="http://www.infostreet.com/">InfoStreet</a> streetSmartAPI. Requires installation of <a href="http://search.cpan.org/search?dist=Frontier-Client">Frontier::Client</a> from CPAN.',
628 'desc' => 'Real-time export to vpopmail text files',
629 'options' => \%vpopmail_options,
631 'notes' => 'Real time export to <a href="http://inter7.com/vpopmail/">vpopmail</a> text files (...extended description from jeff?...)',
639 'desc' =>'Batch export to BIND named',
640 'options' => \%bind_options,
641 'notes' => 'bind export notes File::Rsync dependancy, run bind.export',
645 'desc' =>'Batch export to slave BIND named',
646 'options' => \%bind_slave_options,
647 'notes' => 'bind export notes (secondary munge) File::Rsync dependancy, run bind.export',
651 'desc' => 'Real-time export to SQL-backed mail server',
652 'options' => \%sqlmail_options,
654 'notes' => 'Database schema can be made to work with Courier IMAP and
655 Exim. Others could work but are untested.',
665 'desc' => 'Real-time export to SQL-backed mail server',
666 'options' => \%sqlmail_options,
668 'notes' => 'Database schema can be made to work with Courier IMAP and
669 Exim. Others could work but are untested.',
679 =head1 NEW EXPORT CLASSES
681 Should be added to the %export hash here, and a module should be added in
682 FS/FS/part_export/ (an example may be found in eg/export_template.pm)
688 Hmm... cust_export class (not necessarily a database table...) ... ?
694 L<FS::part_export_option>, L<FS::export_svc>, L<FS::svc_acct>,
696 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.