- documentation updates
[freeside.git] / FS / FS / svc_acct.pm
index 2305aeb..1e1cbb0 100644 (file)
@@ -8,7 +8,6 @@ use vars qw( @ISA $nossh_hack $noexport_hack $conf
              $username_noperiod $username_uppercase
              $shellmachine $useradd $usermod $userdel $mydomain
              $cyrus_server $cyrus_admin_user $cyrus_admin_pass
-             $cp_server $cp_user $cp_pass $cp_workgroup
              $dirhash
              @saltset @pw_set
              $rsync $ssh $exportdir $vpopdir);
@@ -27,6 +26,8 @@ use FS::cust_main_invoice;
 use FS::svc_domain;
 use FS::raddb;
 use FS::queue;
+use FS::radius_usergroup;
+use FS::Msgcat qw(gettext);
 
 @ISA = qw( FS::svc_Common );
 
@@ -78,16 +79,6 @@ $FS::UID::callback{'FS::svc_acct'} = sub {
     $cyrus_admin_user = '';
     $cyrus_admin_pass = '';
   }
-  if ( $conf->exists('cp_app') ) {
-    ($cp_server, $cp_user, $cp_pass, $cp_workgroup) =
-      $conf->config('cp_app');
-    eval "use Net::APP;"
-  } else {
-    $cp_server = '';
-    $cp_user = '';
-    $cp_pass = '';
-    $cp_workgroup = '';
-  }
 
   $dirhash = $conf->config('dirhash') || 0;
   $exportdir = "/usr/local/etc/freeside/export." . datasrc;
@@ -169,6 +160,8 @@ FS::svc_Common.  The following fields are currently supported:
 
 =item _password - generated if blank
 
+=item sec_phrase - security phrase
+
 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
 
 =item uid
@@ -191,8 +184,6 @@ FS::svc_Common.  The following fields are currently supported:
 
 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
 
-=item domsvc - service number of svc_domain with which to associate
-
 =back
 
 =head1 METHODS
@@ -215,6 +206,10 @@ otherwise returns false.
 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be 
 defined.  An FS::cust_svc record will be created and inserted.
 
+The additional field I<usergroup> can optionally be defined; if so it should
+contain an arrayref of group names.  See L<FS::radius_usergroup>.  (used in
+sqlradius export only)
+
 If the configuration value (see L<FS::Conf>) shellmachine exists, and the 
 username, uid, and dir fields are defined, the command(s) specified in
 the shellmachine-useradd configuration are added to the job queue (see
@@ -254,12 +249,10 @@ sub insert {
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
-  my $amount = 0;
-
   $error = $self->check;
   return $error if $error;
 
-  return "Username ". $self->username. " in use"
+  return gettext('username_in_use'). ": ". $self->username
     if qsearchs( 'svc_acct', { 'username' => $self->username,
                                'domsvc'   => $self->domsvc,
                              } );
@@ -288,6 +281,20 @@ sub insert {
     return $error;
   }
 
+  if ( $self->usergroup ) {
+    foreach my $groupname ( @{$self->usergroup} ) {
+      my $radius_usergroup = new FS::radius_usergroup ( {
+        svcnum    => $self->svcnum,
+        groupname => $groupname,
+      } );
+      my $error = $radius_usergroup->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
+    }
+  }
+
   #new-style exports!
   unless ( $noexport_hack ) {
     foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
@@ -333,18 +340,6 @@ sub insert {
     }
   }
 
-  if ( $cp_server ) {
-    my $queue = new FS::queue {
-      'svcnum' => $self->svcnum,
-      'job'    => 'FS::svc_acct::cp_insert'
-    };
-    $error = $queue->insert($self->username, $self->_password);
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return "queueing job (transaction rolled back): $error";
-    }
-  }
-  
   if ( $vpopdir ) {
 
     my $vpopmail_queue =
@@ -405,27 +400,6 @@ sub cyrus_insert {
   1;
 }
 
-sub cp_insert {
-  my( $username, $password ) = @_;
-
-  my $app = new Net::APP ( $cp_server,
-                        User     => $cp_user,
-                        Password => $cp_pass,
-                        Domain   => $mydomain,
-                        Timeout  => 60,
-                        #Debug    => 1,
-                      ) or die "$@\n";
-
-  $app->create_mailbox(
-                        Mailbox   => $username,
-                        Password  => $password,
-                        Workgroup => $cp_workgroup,
-                        Domain    => $mydomain,
-                      );
-
-  die $app->message."\n" unless $app->ok;
-}
-
 sub vpopmail_insert {
   my( $username, $password, $domain, $vpopdir ) = @_;
   
@@ -560,6 +534,18 @@ sub delete {
     }
   }
 
+  foreach my $radius_usergroup (
+    qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
+  ) {
+    my $error = $radius_usergroup->delete;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
+  my $part_svc = $self->cust_svc->part_svc;
+
   my $error = $self->SUPER::delete;
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
@@ -568,7 +554,7 @@ sub delete {
 
   #new-style exports!
   unless ( $noexport_hack ) {
-    foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
+    foreach my $part_export ( $part_svc->part_export ) {
       my $error = $part_export->export_delete($self);
       if ( $error ) {
         $dbh->rollback if $oldAutoCommit;
@@ -603,15 +589,6 @@ sub delete {
     }
   }
   
-  if ( $cp_server ) {
-    my $queue = new FS::queue { 'job' => 'FS::svc_acct::cp_delete' };
-    $error = $queue->insert($self->username);
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return "queueing job (transaction rolled back): $error";
-    }
-  }
-
   if ( $vpopdir ) {
     my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
     $error = $queue->insert( $self->username, $self->domain );
@@ -649,24 +626,6 @@ sub cyrus_delete {
   1;
 }
 
-sub cp_delete {
-  my( $username ) = @_;
-  my $app = new Net::APP ( $cp_server,
-                        User     => $cp_user,
-                        Password => $cp_pass,
-                        Domain   => $mydomain,
-                        Timeout  => 60,
-                        #Debug    => 1,
-                      ) or die "$@\n";
-
-  $app->delete_mailbox(
-                        Mailbox   => $username,
-                        Domain    => $mydomain,
-                      );
-
-  die $app->message."\n" unless $app->ok;
-}
-
 sub vpopmail_delete {
   my( $username, $domain ) = @_;
   
@@ -699,6 +658,10 @@ sub vpopmail_delete {
 Replaces OLD_RECORD with this one in the database.  If there is an error,
 returns the error, otherwise returns false.
 
+The additional field I<usergroup> can optionally be defined; if so it should
+contain an arrayref of group names.  See L<FS::radius_usergroup>.  (used in
+sqlradius export only)
+
 If the configuration value (see L<FS::Conf>) shellmachine exists, and the 
 dir field has changed, the command(s) specified in the shellmachine-usermod
 configuraiton file are added to the job queue (see L<FS::queue> and
@@ -759,6 +722,40 @@ sub replace {
     return $error if $error;
   }
 
+  $old->usergroup( [ $old->radius_groups ] );
+  if ( $new->usergroup ) {
+    #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
+    my @newgroups = @{$new->usergroup};
+    foreach my $oldgroup ( @{$old->usergroup} ) {
+      if ( grep { $oldgroup eq $_ } @newgroups ) {
+        @newgroups = grep { $oldgroup ne $_ } @newgroups;
+        next;
+      }
+      my $radius_usergroup = qsearchs('radius_usergroup', {
+        svcnum    => $old->svcnum,
+        groupname => $oldgroup,
+      } );
+      my $error = $radius_usergroup->delete;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return "error deleting radius_usergroup $oldgroup: $error";
+      }
+    }
+
+    foreach my $newgroup ( @newgroups ) {
+      my $radius_usergroup = new FS::radius_usergroup ( {
+        svcnum    => $new->svcnum,
+        groupname => $newgroup,
+      } );
+      my $error = $radius_usergroup->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return "error adding radius_usergroup $newgroup: $error";
+      }
+    }
+
+  }
+
   #new-style exports!
   unless ( $noexport_hack ) {
     foreach my $part_export ( $new->cust_svc->part_svc->part_export ) {
@@ -803,18 +800,6 @@ sub replace {
     }
   }
 
-  if ( $cp_server && $old->_password ne $new->_password ) {
-    my $queue = new FS::queue {  
-      'svcnum' => $new->svcnum,
-      'job' => 'FS::svc_acct::cp_change'
-    };
-    $error = $queue->insert( $new->username, $new->_password );
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return "queueing job (transaction rolled back): $error";
-    }
-  }
-
   if ( $vpopdir ) {
     my $cpassword = crypt(
       $new->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]
@@ -846,65 +831,6 @@ sub replace {
   ''; #no error
 }
 
-sub cp_rename {
-  my ( $old_username, $new_username ) = @_;
-
-  my $app = new Net::APP ( $cp_server,
-                        User     => $cp_user,
-                        Password => $cp_pass,
-                        Domain   => $mydomain,
-                        Timeout  => 60,
-                        #Debug    => 1,
-                      ) or die "$@\n";
-
-  $app->rename_mailbox(
-                        Domain        => $mydomain,
-                        Old_Mailbox   => $old_username,
-                        New_Mailbox   => $new_username,
-                      );
-
-  die $app->message."\n" unless $app->ok;
-
-}
-
-sub cp_change {
-  my ( $username, $password ) = @_;
-
-  my $app = new Net::APP ( $cp_server,
-                        User     => $cp_user,
-                        Password => $cp_pass,
-                        Domain   => $mydomain,
-                        Timeout  => 60,
-                        #Debug    => 1,
-                      ) or die "$@\n";
-
-  if ( $password =~ /^\*SUSPENDED\* (.*)$/ ) {
-    $password = $1;
-    $app->set_mailbox_status(
-                              Domain       => $mydomain,
-                              Mailbox      => $username,
-                              Other        => 'T',
-                              Other_Bounce => 'T',
-                            );
-  } else {
-    $app->set_mailbox_status(
-                              Domain       => $mydomain,
-                              Mailbox      => $username,
-                              Other        => 'F',
-                              Other_Bounce => 'F',
-                            );
-  }
-  die $app->message."\n" unless $app->ok;
-
-  $app->change_mailbox(
-                        Domain    => $mydomain,
-                        Mailbox   => $username,
-                        Password  => $password,
-                      );
-  die $app->message."\n" unless $app->ok;
-
-}
-
 sub vpopmail_replace_password {
   my( $username, $password, $domain ) = @_;
   
@@ -1007,32 +933,38 @@ sub check {
   return $x unless ref($x);
   my $part_svc = $x;
 
+  if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
+    $self->usergroup(
+      [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
+  }
+
   my $error = $self->ut_numbern('svcnum')
               || $self->ut_number('domsvc')
+              || $self->ut_textn('sec_phrase')
   ;
   return $error if $error;
 
   my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
   if ( $username_uppercase ) {
     $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/i
-      or return "Illegal username: ". $recref->{username};
+      or return gettext('illegal_username'). ": ". $recref->{username};
     $recref->{username} = $1;
   } else {
     $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/
-      or return "Illegal username: ". $recref->{username};
+      or return gettext('illegal_username'). ": ". $recref->{username};
     $recref->{username} = $1;
   }
 
   if ( $username_letterfirst ) {
-    $recref->{username} =~ /^[a-z]/ or return "Illegal username";
+    $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
   } elsif ( $username_letter ) {
-    $recref->{username} =~ /[a-z]/ or return "Illegal username";
+    $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
   }
   if ( $username_noperiod ) {
-    $recref->{username} =~ /\./ and return "Illegal username";
+    $recref->{username} =~ /\./ and return gettext('illegal_username');
   }
   unless ( $username_ampersand ) {
-    $recref->{username} =~ /\&/ and return "Illegal username";
+    $recref->{username} =~ /\&/ and return gettext('illegal_username');
   }
 
   $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
@@ -1148,7 +1080,7 @@ sub check {
     $recref->{_password} = '!!';
   } else {
     #return "Illegal password";
-    return "Illegal password: ". $recref->{_password};
+    return gettext('illegal_password'). ": ". $recref->{_password};
   }
 
   ''; #no error
@@ -1282,8 +1214,71 @@ sub seconds_since {
   $self->cust_svc->seconds_since(@_);
 }
 
+=item radius_groups
+
+Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
+
+=cut
+
+sub radius_groups {
+  my $self = shift;
+  map { $_->groupname }
+    qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
+}
+
 =back
 
+=head1 SUBROUTINES
+
+=item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
+
+=cut
+
+sub radius_usergroup_selector {
+  my $sel_groups = shift;
+  my %sel_groups = map { $_=>1 } @$sel_groups;
+
+  my $selectname = shift || 'radius_usergroup';
+
+  my $dbh = dbh;
+  my $sth = $dbh->prepare(
+    'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
+  ) or die $dbh->errstr;
+  $sth->execute() or die $sth->errstr;
+  my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
+
+  my $html = <<END;
+    <SCRIPT>
+    function ${selectname}_doadd(object) {
+      var myvalue = object.${selectname}_add.value;
+      var optionName = new Option(myvalue,myvalue,false,true);
+      var length = object.$selectname.length;
+      object.$selectname.options[length] = optionName;
+      object.${selectname}_add.value = "";
+    }
+    </SCRIPT>
+    <SELECT MULTIPLE NAME="$selectname">
+END
+
+  foreach my $group ( @all_groups ) {
+    $html .= '<OPTION';
+    if ( $sel_groups{$group} ) {
+      $html .= ' SELECTED';
+      $sel_groups{$group} = 0;
+    }
+    $html .= ">$group</OPTION>\n";
+  }
+  foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
+    $html .= "<OPTION SELECTED>$group</OPTION>\n";
+  };
+  $html .= '</SELECT>';
+
+  $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
+           qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
+
+  $html;
+}
+
 =head1 BUGS
 
 The $recref stuff in sub check should be cleaned up.
@@ -1292,6 +1287,9 @@ The suspend, unsuspend and cancel methods update the database, but not the
 current object.  This is probably a bug as it's unexpected and
 counterintuitive.
 
+radius_usergroup_selector?  putting web ui components in here?  they should
+probably live somewhere else...
+
 =head1 SEE ALSO
 
 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,