*** empty log message ***
[freeside.git] / site_perl / svc_acct.pm
index a43af6b..f066ebd 100644 (file)
@@ -1,21 +1,24 @@
 package FS::svc_acct;
 
 use strict;
-use vars qw(@ISA @EXPORT_OK $nossh_hack $conf $dir_prefix @shells
+use vars qw(@ISA $nossh_hack $conf $dir_prefix @shells
             $shellmachine @saltset @pw_set);
-use Exporter;
 use FS::Conf;
-use FS::Record qw(fields qsearchs);
+use FS::Record qw( qsearchs fields );
+use FS::svc_Common;
 use FS::SSH qw(ssh);
-use FS::cust_svc;
+use FS::part_svc;
+use FS::svc_acct_pop;
 
-@ISA = qw(FS::Record Exporter);
-@EXPORT_OK = qw(fields);
+@ISA = qw( FS::svc_Common );
 
-$conf = new FS::Conf;
-$dir_prefix = $conf->config('home');
-@shells = $conf->config('shells');
-$shellmachine = $conf->config('shellmachine');
+#ask FS::UID to run this stuff for us later
+$FS::UID::callback{'FS::svc_acct'} = sub { 
+  $conf = new FS::Conf;
+  $dir_prefix = $conf->config('home');
+  @shells = $conf->config('shells');
+  $shellmachine = $conf->config('shellmachine');
+};
 
 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
@@ -30,8 +33,8 @@ FS::svc_acct - Object methods for svc_acct records
 
   use FS::svc_acct;
 
-  $record = create FS::svc_acct \%hash;
-  $record = create FS::svc_acct { 'column' => 'value' };
+  $record = new FS::svc_acct \%hash;
+  $record = new FS::svc_acct { 'column' => 'value' };
 
   $error = $record->insert;
 
@@ -50,7 +53,7 @@ FS::svc_acct - Object methods for svc_acct records
 =head1 DESCRIPTION
 
 An FS::svc_acct object represents an account.  FS::svc_acct inherits from
-FS::Record.  The following fields are currently supported:
+FS::svc_Common.  The following fields are currently supported:
 
 =over 4
 
@@ -84,24 +87,13 @@ FS::Record.  The following fields are currently supported:
 
 =over 4
 
-=item create HASHREF
+=item new HASHREF
 
 Creates a new account.  To add the account to the database, see L<"insert">.
 
 =cut
 
-sub create {
-  my($proto,$hashref)=@_;
-
-  #now in FS::Record::new
-  #my($field);
-  #foreach $field (fields('svc_acct')) {
-  #  $hashref->{$field}='' unless defined $hashref->{$field};
-  #}
-
-  $proto->new('svc_acct',$hashref);
-
-}
+sub table { 'svc_acct'; }
 
 =item insert
 
@@ -122,50 +114,34 @@ setting $FS::svc_acct::nossh_hack true.
 =cut
 
 sub insert {
-  my($self)=@_;
-  my($error);
+  my $self = shift;
+  my $error;
 
   local $SIG{HUP} = 'IGNORE';
   local $SIG{INT} = 'IGNORE';
   local $SIG{QUIT} = 'IGNORE';
   local $SIG{TERM} = 'IGNORE';
   local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
 
-  $error=$self->check;
+  $error = $self->check;
   return $error if $error;
 
   return "Username ". $self->username. " in use"
-    if qsearchs('svc_acct',{'username'=> $self->username } );
+    if qsearchs( 'svc_acct', { 'username' => $self->username } );
 
-  my($part_svc) = qsearchs('part_svc',{ 'svcpart' => $self->svcpart });
+  my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
   return "Unkonwn svcpart" unless $part_svc;
   return "uid in use"
     if $part_svc->svc_acct__uid_flag ne 'F'
-      && qsearchs('svc_acct',{'uid'=> $self->uid } )
+      && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
       && $self->username !~ /^(hyla)?fax$/
     ;
 
-  my($svcnum)=$self->svcnum;
-  my($cust_svc);
-  unless ( $svcnum ) {
-    $cust_svc=create FS::cust_svc ( {
-      'svcnum'  => $svcnum,
-      'pkgnum'  => $self->pkgnum,
-      'svcpart' => $self->svcpart,
-    } );
-    my($error) = $cust_svc->insert;
-    return $error if $error;
-    $svcnum = $self->svcnum($cust_svc->svcnum);
-  }
-
-  $error = $self->add;
-  if ($error) {
-    #$cust_svc->del if $cust_svc;
-    $cust_svc->delete if $cust_svc;
-    return $error;
-  }
+  $error = $self->SUPER::insert;
+  return $error if $error;
 
-  my($username,$uid,$dir,$shell) = (
+  my ( $username, $uid, $dir, $shell ) = (
     $self->username,
     $self->uid,
     $self->dir,
@@ -207,25 +183,20 @@ setting $FS::svc_acct::nossh_hack true.
 =cut
 
 sub delete {
-  my($self)=@_;
-  my($error);
+  my $self = shift;
+  my $error;
 
   local $SIG{HUP} = 'IGNORE';
   local $SIG{INT} = 'IGNORE';
   local $SIG{QUIT} = 'IGNORE';
   local $SIG{TERM} = 'IGNORE';
   local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
 
-  my($svcnum)=$self->getfield('svcnum');
-
-  $error = $self->del;
+  $error = $self->SUPER::delete;
   return $error if $error;
 
-  my($cust_svc)=qsearchs('cust_svc',{'svcnum'=>$svcnum});  
-  $error = $cust_svc->del;
-  return $error if $error;
-
-  my($username) = $self->getfield('username');
+  my $username = $self->username;
   if ( $username && $shellmachine && ! $nossh_hack ) {
     ssh("root\@$shellmachine","userdel $username");
   }
@@ -258,39 +229,30 @@ setting $FS::svc_acct::nossh_hack true.
 =cut
 
 sub replace {
-  my($new,$old)=@_;
-  my($error);
-
-  return "(Old) Not a svc_acct record!" unless $old->table eq "svc_acct";
-  return "Can't change svcnum!"
-    unless $old->getfield('svcnum') eq $new->getfield('svcnum');
+  my ( $new, $old ) = ( shift, shift );
+  my $error;
 
   return "Username in use"
-    if $old->getfield('username') ne $new->getfield('username') &&
-      qsearchs('svc_acct',{'username'=> $new->getfield('username') } );
+    if $old->username ne $new->username &&
+      qsearchs( 'svc_acct', { 'username' => $new->username } );
 
-  return "Can't change uid!"
-    if $old->getfield('uid') ne $new->getfield('uid');
+  return "Can't change uid!" if $old->uid != $new->uid;
 
   #change homdir when we change username
-  if ( $old->getfield('username') ne $new->getfield('username') ) {
-    $new->setfield('dir','');
-  }
-
-  $error=$new->check;
-  return $error if $error;
+  $new->setfield('dir', '') if $old->username ne $new->username;
 
   local $SIG{HUP} = 'IGNORE';
   local $SIG{INT} = 'IGNORE';
   local $SIG{QUIT} = 'IGNORE';
   local $SIG{TERM} = 'IGNORE';
   local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
 
-  $error = $new->rep($old);
+  $error = $new->SUPER::replace($old);
   return $error if $error;
 
-  my($old_dir,$new_dir)=( $old->getfield('dir'),$new->getfield('dir') );
-  my($uid,$gid)=( $new->getfield('uid'), $new->getfield('gid') );
+  my ( $old_dir, $new_dir ) = ( $old->getfield('dir'), $new->getfield('dir') );
+  my ( $uid, $gid) = ( $new->getfield('uid'), $new->getfield('gid') );
   if ( $old_dir
        && $new_dir
        && $old_dir ne $new_dir
@@ -319,17 +281,15 @@ Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
 =cut
 
 sub suspend {
-  my($old) = @_;
-  my(%hash) = $old->hash;
+  my $self = shift;
+  my %hash = $self->hash;
   unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
     $hash{_password} = '*SUSPENDED* '.$hash{_password};
-    my($new) = create FS::svc_acct ( \%hash );
-#    $new->replace($old);
-    $new->rep($old); #to avoid password checking :)
+    my $new = new FS::svc_acct ( \%hash );
+    $new->replace($self);
   } else {
     ''; #no error (already suspended)
   }
-
 }
 
 =item unsuspend
@@ -342,13 +302,12 @@ Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
 =cut
 
 sub unsuspend {
-  my($old) = @_;
-  my(%hash) = $old->hash;
+  my $self = shift;
+  my %hash = $self->hash;
   if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
     $hash{_password} = $1;
-    my($new) = create FS::svc_acct ( \%hash );
-#    $new->replace($old);
-    $new->rep($old); #to avoid password checking :)
+    my $new = new FS::svc_acct ( \%hash );
+    $new->replace($self);
   } else {
     ''; #no error (already unsuspended)
   }
@@ -360,13 +319,6 @@ Just returns false (no error) for now.
 
 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
 
-=cut
-
-# Usage: $error = $record -> cancel;
-sub cancel {
-  ''; #stub (no error) - taken care of in delete
-}
-
 =item check
 
 Checks all fields to make sure this is a valid service.  If there is an error,
@@ -378,35 +330,15 @@ Sets any fixed values; see L<FS::part_svc>.
 =cut
 
 sub check {
-  my($self)=@_;
-  return "Not a svc_acct record!" unless $self->table eq "svc_acct";
-  my($recref) = $self->hashref;
-
-  $recref->{svcnum} =~ /^(\d*)$/ or return "Illegal svcnum";
-  $recref->{svcnum} = $1;
+  my $self = shift;
 
-  #get part_svc
-  my($svcpart);
-  my($svcnum)=$self->getfield('svcnum');
-  if ($svcnum) {
-    my($cust_svc)=qsearchs('cust_svc',{'svcnum'=>$svcnum});
-    return "Unknown svcnum" unless $cust_svc; 
-    $svcpart=$cust_svc->svcpart;
-  } else {
-    $svcpart=$self->getfield('svcpart');
-  }
-  my($part_svc)=qsearchs('part_svc',{'svcpart'=>$svcpart});
-  return "Unkonwn svcpart" unless $part_svc;
+  my($recref) = $self->hashref;
 
-  #set fixed fields from part_svc
-  my($field);
-  foreach $field ( fields('svc_acct') ) {
-    if ( $part_svc->getfield('svc_acct__'. $field. '_flag') eq 'F' ) {
-      $self->setfield($field,$part_svc->getfield('svc_acct__'. $field) );
-    }
-  }
+  my $x = $self->setfixed;
+  return $x unless ref($x);
+  my $part_svc = $x;
 
-  my($ulen)=$self->dbdef_table->column('username')->length;
+  my $ulen =$self->dbdef_table->column('username')->length;
   $recref->{username} =~ /^([a-z0-9_\-]{2,$ulen})$/
     or return "Illegal username";
   $recref->{username} = $1;
@@ -511,20 +443,23 @@ sub check {
 
 =back
 
-=head1 BUGS
+=head1 VERSION
 
-It doesn't properly override FS::Record yet.
+$Id: svc_acct.pm,v 1.7 1999-04-07 14:37:37 ivan Exp $
+
+=head1 BUGS
 
 The remote commands should be configurable.
 
-The create method should set defaults from part_svc (like the check method
-sets fixed values).
+The bits which ssh should fork before doing so.
+
+The $recref stuff in sub check should be cleaned up.
 
 =head1 SEE ALSO
 
-L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>,
-L<FS::SSH>, L<ssh>, L<FS::svc_acct_pop>, schema.html from the base
-documentation.
+L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
+L<FS::part_svc>, L<FS::cust_pkg>, L<FS::SSH>, L<ssh>, L<FS::svc_acct_pop>,
+schema.html from the base documentation.
 
 =head1 HISTORY
 
@@ -551,6 +486,24 @@ arbitrary radius attributes ivan@sisd.com 98-aug-13
 
 pod and FS::conf ivan@sisd.com 98-sep-22
 
+$Log: svc_acct.pm,v $
+Revision 1.7  1999-04-07 14:37:37  ivan
+use FS::part_svc and FS::svc_acct_pop to avoid warnings
+
+Revision 1.6  1999/01/25 12:26:15  ivan
+yet more mod_perl stuff
+
+Revision 1.5  1999/01/18 21:58:09  ivan
+esthetic: eq and ne were used in a few places instead of == and !=
+
+Revision 1.4  1998/12/30 00:30:45  ivan
+svc_ stuff is more properly OO - has a common superclass FS::svc_Common
+
+Revision 1.2  1998/11/13 09:56:55  ivan
+change configuration file layout to support multiple distinct databases (with
+own set of config files, export, etc.)
+
+
 =cut
 
 1;