- remove some out of date documentation
[freeside.git] / FS / FS / part_export.pm
1 package FS::part_export;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK %exports );
5 use Exporter;
6 use Tie::IxHash;
7 use FS::Record qw( qsearch qsearchs dbh );
8 use FS::part_svc;
9 use FS::part_export_option;
10 use FS::export_svc;
11
12 @ISA = qw(FS::Record);
13 @EXPORT_OK = qw(export_info);
14
15 =head1 NAME
16
17 FS::part_export - Object methods for part_export records
18
19 =head1 SYNOPSIS
20
21   use FS::part_export;
22
23   $record = new FS::part_export \%hash;
24   $record = new FS::part_export { 'column' => 'value' };
25
26   #($new_record, $options) = $template_recored->clone( $svcpart );
27
28   $error = $record->insert( { 'option' => 'value' } );
29   $error = $record->insert( \%options );
30
31   $error = $new_record->replace($old_record);
32
33   $error = $record->delete;
34
35   $error = $record->check;
36
37 =head1 DESCRIPTION
38
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:
42
43 =over 4
44
45 =item exportnum - primary key
46
47 =item machine - Machine name 
48
49 =item exporttype - Export type
50
51 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new export.  To add the export to the database, see L<"insert">.
62
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.
65
66 =cut
67
68 # the new method can be inherited from FS::Record, if a table method is defined
69
70 sub table { 'part_export'; }
71
72 =cut
73
74 #=item clone SVCPART
75 #
76 #An alternate constructor.  Creates a new export by duplicating an existing
77 #export.  The given svcpart is assigned to the new export.
78 #
79 #Returns a list consisting of the new export object and a hashref of options.
80 #
81 #=cut
82 #
83 #sub clone {
84 #  my $self = shift;
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 } )
92 #    }
93 #  );
94 #}
95
96 =item insert HASHREF
97
98 Adds this record to the database.  If there is an error, returns the error,
99 otherwise returns false.
100
101 If a hash reference of options is supplied, part_export_option records are
102 created (see L<FS::part_export_option>).
103
104 =cut
105
106 #false laziness w/queue.pm
107 sub insert {
108   my $self = shift;
109   my $options = shift;
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';
116
117   my $oldAutoCommit = $FS::UID::AutoCommit;
118   local $FS::UID::AutoCommit = 0;
119   my $dbh = dbh;
120
121   my $error = $self->SUPER::insert;
122   if ( $error ) {
123     $dbh->rollback if $oldAutoCommit;
124     return $error;
125   }
126
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},
132     } );
133     $error = $part_export_option->insert;
134     if ( $error ) {
135       $dbh->rollback if $oldAutoCommit;
136       return $error;
137     }
138   }
139
140   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
141
142   '';
143
144 };
145
146 =item delete
147
148 Delete this record from the database.
149
150 =cut
151
152 #foreign keys would make this much less tedious... grr dumb mysql
153 sub delete {
154   my $self = shift;
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';
161
162   my $oldAutoCommit = $FS::UID::AutoCommit;
163   local $FS::UID::AutoCommit = 0;
164   my $dbh = dbh;
165
166   my $error = $self->SUPER::delete;
167   if ( $error ) {
168     $dbh->rollback if $oldAutoCommit;
169     return $error;
170   }
171
172   foreach my $part_export_option ( $self->part_export_option ) {
173     my $error = $part_export_option->delete;
174     if ( $error ) {
175       $dbh->rollback if $oldAutoCommit;
176       return $error;
177     }
178   }
179
180   foreach my $export_svc ( $self->export_svc ) {
181     my $error = $export_svc->delete;
182     if ( $error ) {
183       $dbh->rollback if $oldAutoCommit;
184       return $error;
185     }
186   }
187
188   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
189
190   '';
191
192 }
193
194 =item replace OLD_RECORD HASHREF
195
196 Replaces the OLD_RECORD with this one in the database.  If there is an error,
197 returns the error, otherwise returns false.
198
199 If a hash reference of options is supplied, part_export_option records are
200 created or modified (see L<FS::part_export_option>).
201
202 =cut
203
204 sub replace {
205   my $self = shift;
206   my $old = shift;
207   my $options = shift;
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';
214
215   my $oldAutoCommit = $FS::UID::AutoCommit;
216   local $FS::UID::AutoCommit = 0;
217   my $dbh = dbh;
218
219   my $error = $self->SUPER::replace($old);
220   if ( $error ) {
221     $dbh->rollback if $oldAutoCommit;
222     return $error;
223   }
224
225   foreach my $optionname ( keys %{$options} ) {
226     my $old = qsearchs( 'part_export_option', {
227         'exportnum'   => $self->exportnum,
228         'optionname'  => $optionname,
229     } );
230     my $new = new FS::part_export_option ( {
231         'exportnum'   => $self->exportnum,
232         'optionname'  => $optionname,
233         'optionvalue' => $options->{$optionname},
234     } );
235     $new->optionnum($old->optionnum) if $old;
236     my $error = $old ? $new->replace($old) : $new->insert;
237     if ( $error ) {
238       $dbh->rollback if $oldAutoCommit;
239       return $error;
240     }
241   }
242
243   #remove extraneous old options
244   foreach my $opt (
245     grep { !exists $options->{$_->optionname} } $old->part_export_option
246   ) {
247     my $error = $opt->delete;
248     if ( $error ) {
249       $dbh->rollback if $oldAutoCommit;
250       return $error;
251     }
252   }
253
254   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
255
256   '';
257
258 };
259
260 =item check
261
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
264 and replace methods.
265
266 =cut
267
268 sub check {
269   my $self = shift;
270   my $error = 
271     $self->ut_numbern('exportnum')
272     || $self->ut_domain('machine')
273     || $self->ut_alpha('exporttype')
274   ;
275   return $error if $error;
276
277   warn $self->machine. "!!!\n";
278
279   $self->machine =~ /^([\w\-\.]*)$/
280     or return "Illegal machine: ". $self->machine;
281   $self->machine($1);
282
283   $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
284   $self->nodomain($1);
285
286   $self->deprecated(1); #BLAH
287
288   #check exporttype?
289
290   ''; #no error
291 }
292
293 #=item part_svc
294 #
295 #Returns the service definition (see L<FS::part_svc>) for this export.
296 #
297 #=cut
298 #
299 #sub part_svc {
300 #  my $self = shift;
301 #  qsearchs('part_svc', { svcpart => $self->svcpart } );
302 #}
303
304 sub part_svc {
305   use Carp;
306   croak "FS::part_export::part_svc deprecated";
307   #confess "FS::part_export::part_svc deprecated";
308 }
309
310 =item export_svc
311
312 Returns a list of associated FS::export_svc records.
313
314 =cut
315
316 sub export_svc {
317   my $self = shift;
318   qsearch('export_svc', { 'exportnum' => $self->exportnum } );
319 }
320
321 =item part_export_option
322
323 Returns all options as FS::part_export_option objects (see
324 L<FS::part_export_option>).
325
326 =cut
327
328 sub part_export_option {
329   my $self = shift;
330   qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
331 }
332
333 =item options 
334
335 Returns a list of option names and values suitable for assigning to a hash.
336
337 =cut
338
339 sub options {
340   my $self = shift;
341   map { $_->optionname => $_->optionvalue } $self->part_export_option;
342 }
343
344 =item option OPTIONNAME
345
346 Returns the option value for the given name, or the empty string.
347
348 =cut
349
350 sub option {
351   my $self = shift;
352   my $part_export_option =
353     qsearchs('part_export_option', {
354       exportnum  => $self->exportnum,
355       optionname => shift,
356   } );
357   $part_export_option ? $part_export_option->optionvalue : '';
358 }
359
360 =item rebless
361
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, but until then, see L</NEW EXPORT CLASSES>.
365
366 =cut
367
368 sub rebless {
369   my $self = shift;
370   my $exporttype = $self->exporttype;
371   my $class = ref($self). "::$exporttype";
372   eval "use $class;" or die $@;
373   bless($self, $class);
374 }
375
376 =item export_insert SVC_OBJECT
377
378 =cut
379
380 sub export_insert {
381   my $self = shift;
382   $self->rebless;
383   $self->_export_insert(@_);
384 }
385
386 #sub AUTOLOAD {
387 #  my $self = shift;
388 #  $self->rebless;
389 #  my $method = $AUTOLOAD;
390 #  #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
391 #  $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
392 #  $self->$method(@_);
393 #}
394
395 =item export_replace NEW OLD
396
397 =cut
398
399 sub export_replace {
400   my $self = shift;
401   $self->rebless;
402   $self->_export_replace(@_);
403 }
404
405 =item export_delete
406
407 =cut
408
409 sub export_delete {
410   my $self = shift;
411   $self->rebless;
412   $self->_export_delete(@_);
413 }
414
415 #fallbacks providing useful error messages intead of infinite loops
416 sub _export_insert {
417   my $self = shift;
418   return "_export_insert: unknown export type ". $self->exporttype;
419 }
420
421 sub _export_replace {
422   my $self = shift;
423   return "_export_replace: unknown export type ". $self->exporttype;
424 }
425
426 sub _export_delete {
427   my $self = shift;
428   return "_export_delete: unknown export type ". $self->exporttype;
429 }
430
431 =back
432
433 =head1 SUBROUTINES
434
435 =over 4
436
437 =item export_info [ SVCDB ]
438
439 Returns a hash reference of the exports for the given I<svcdb>, or if no
440 I<svcdb> is specified, for all exports.  The keys of the hash are
441 I<exporttype>s and the values are again hash references containing information
442 on the export:
443
444   'desc'     => 'Description',
445   'options'  => {
446                   'option'  => { label=>'Option Label' },
447                   'option2' => { label=>'Another label' },
448                 },
449   'nodomain' => 'Y', #or ''
450   'notes'    => 'Additional notes',
451
452 =cut
453
454 sub export_info {
455   #warn $_[0];
456   return $exports{$_[0]} if @_;
457   #{ map { %{$exports{$_}} } keys %exports };
458   my $r = { map { %{$exports{$_}} } keys %exports };
459 }
460
461 =item exporttype2svcdb EXPORTTYPE
462
463 Returns the applicable I<svcdb> for an I<exporttype>.
464
465 =cut
466
467 sub exporttype2svcdb {
468   my $exporttype = $_[0];
469   foreach my $svcdb ( keys %exports ) {
470     return $svcdb if grep { $exporttype eq $_ } keys %{$exports{$svcdb}};
471   }
472   '';
473 }
474
475 tie my %shellcommands_options, 'Tie::IxHash',
476   #'machine' => { label=>'Remote machine' },
477   'user' => { label=>'Remote username', default=>'root' },
478   'useradd' => { label=>'Insert command',
479                  default=>'useradd -d $dir -m -s $shell -u $uid $username'
480                 #default=>'cp -pr /etc/skel $dir; chown -R $uid.$gid $dir'
481                },
482   'userdel' => { label=>'Delete command',
483                  default=>'userdel $username',
484                  #default=>'rm -rf $dir',
485                },
486   'usermod' => { label=>'Modify command',
487                  default=>'usermod -d $new_dir -l $new_username -s $new_shell -u $new_uid $old_username',
488                 #default=>'[ -d $old_dir ] && mv $old_dir $new_dir || ( '.
489                  #  'chmod u+t $old_dir; mkdir $new_dir; cd $old_dir; '.
490                  #  'find . -depth -print | cpio -pdm $new_dir; '.
491                  #  'chmod u-t $new_dir; chown -R $uid.$gid $new_dir; '.
492                  #  'rm -rf $old_dir'.
493                  #')'
494                },
495 ;
496
497 tie my %sqlradius_options, 'Tie::IxHash',
498   'datasrc'  => { label=>'DBI data source' },
499   'username' => { label=>'Database username' },
500   'password' => { label=>'Database password' },
501 ;
502
503 tie my %cyrus_options, 'Tie::IxHash',
504   'server' => { label=>'IMAP server' },
505   'username' => { label=>'Admin username' },
506   'password' => { label=>'Admin password' },
507 ;
508
509 tie my %cp_options, 'Tie::IxHash',
510   'host'      => { label=>'Hostname' },
511   'port'      => { label=>'Port number' },
512   'username'  => { label=>'Username' },
513   'password'  => { label=>'Password' },
514   'domain'    => { label=>'Domain' },
515   'workgroup' => { label=>'Default Workgroup' },
516 ;
517
518 tie my %infostreet_options, 'Tie::IxHash',
519   'url'      => { label=>'XML-RPC Access URL', },
520   'login'    => { label=>'InfoStreet login', },
521   'password' => { label=>'InfoStreet password', },
522   'groupID'  => { label=>'InfoStreet groupID', },
523 ;
524
525 tie my %vpopmail_options, 'Tie::IxHash',
526   'machine' => { label=>'vpopmail machine', },
527   'dir'     => { label=>'directory', }, # ?more info? default?
528   'uid'     => { label=>'vpopmail uid' },
529   'gid'     => { label=>'vpopmail gid' },
530 ;
531
532 tie my %bind_options, 'Tie::IxHash',
533   #'machine'    => { label=>'named machine' },
534   'named_conf' => { label  => 'named.conf location',
535                     default=> '/etc/bind/named.conf' },
536   'zonepath'   => { label => 'path to zone files',
537                     default=> '/etc/bind/', },
538 ;
539
540 tie my %bind_slave_options, 'Tie::IxHash',
541   #'machine'    => { label=> 'Slave machine' },
542   'master'      => { label=> 'Master IP address(s) (semicolon-separated)' },
543   'named_conf'  => { label   => 'named.conf location',
544                      default => '/etc/bind/named.conf' },
545 ;
546
547
548
549 #export names cannot have dashes...
550 %exports = (
551   'svc_acct' => {
552     'sysvshell' => {
553       'desc' =>
554         'Batch export of /etc/passwd and /etc/shadow files (Linux/SysV)',
555       'options' => {},
556     },
557     'bsdshell' => {
558       'desc' =>
559         'Batch export of /etc/passwd and /etc/master.passwd files (BSD)',
560       'options' => {},
561     },
562 #    'nis' => {
563 #      'desc' =>
564 #        'Batch export of /etc/global/passwd and /etc/global/shadow for NIS ',
565 #      'options' => {},
566 #    },
567     'textradius' => {
568       'desc' => 'Batch export of a text /etc/raddb/users file (Livingston, Cistron)',
569       'options' => {},
570     },
571
572     'shellcommands' => {
573       'desc' => 'Real-time export via remote SSH (i.e. useradd, userdel, etc.)',
574       'options' => \%shellcommands_options,
575       'nodomain' => 'Y',
576       'notes' => 'shellcommandsnotes... (this one is the nodomain one)',
577     },
578
579     'sqlradius' => {
580       'desc' => 'Real-time export to SQL-backed RADIUS (ICRADIUS, FreeRADIUS)',
581       'options' => \%sqlradius_options,
582       'nodomain' => 'Y',
583       '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.',
584     },
585
586     'cyrus' => {
587       'desc' => 'Real-time export to Cyrus IMAP server',
588       'options' => \%cyrus_options,
589       'nodomain' => 'Y',
590       '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. '
591     },
592
593     'cp' => {
594       'desc' => 'Real-time export to Critical Path Account Provisioning Protocol',
595       'options' => \%cp_options,
596       '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.',
597     },
598     
599     'infostreet' => {
600       'desc' => 'Real-time export to InfoStreet streetSmartAPI',
601       'options' => \%infostreet_options,
602       'nodomain' => 'Y',
603       '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.',
604     },
605
606     'vpopmail' => {
607       'desc' => 'Real-time export to vpopmail text files',
608       'options' => \%vpopmail_options,
609
610       'notes' => 'Real time export to <a href="http://inter7.com/vpopmail/">vpopmail</a> text files (...extended description from jeff?...)',
611     },
612
613   },
614
615   'svc_domain' => {
616
617     'bind' => {
618       'desc' =>'Batch export to BIND named',
619       'options' => \%bind_options,
620       'notes' => 'bind export notes',
621     },
622
623     'bind_slave' => {
624       'desc' =>'Batch export to slave BIND named',
625       'options' => \%bind_slave_options,
626       'notes' => 'bind export notes (secondary munge)',
627     },
628
629
630   },
631
632   'svc_acct_sm' => {},
633
634   'svc_forward' => {},
635
636   'svc_www' => {},
637
638 );
639
640 =back
641
642 =head1 NEW EXPORT CLASSES
643
644 Should be added to the %export hash here, and a module should be added in
645 FS/FS/part_export/ (an example may be found in eg/export_template.pm)
646
647 =head1 BUGS
648
649 Probably.
650
651 Hmm... cust_export class (not necessarily a database table...) ... ?
652
653 deprecated column...
654
655 =head1 SEE ALSO
656
657 L<FS::part_export_option>, L<FS::export_svc>, L<FS::svc_acct>,
658 L<FS::svc_domain>,
659 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.
660
661 =cut
662
663 1;
664