export enhancements for suspend reasons
[freeside.git] / FS / FS / part_export / sqlradius.pm
index 10bccb0..c276d7c 100644 (file)
@@ -2,7 +2,7 @@ package FS::part_export::sqlradius;
 
 use vars qw(@ISA $DEBUG %info %options $notes1 $notes2);
 use Tie::IxHash;
-use FS::Record qw( dbh qsearch );
+use FS::Record qw( dbh qsearch qsearchs );
 use FS::part_export;
 use FS::svc_acct;
 use FS::export_svc;
@@ -31,6 +31,11 @@ tie %options, 'Tie::IxHash',
     type  => 'checkbox',
     label => 'Show the Called-Station-ID on session reports',
   },
+  'groups_susp_reason' => { label =>
+                             'Radius group mapping to reason (via template user)',
+                            type  => 'textarea',
+                          },
+
 ;
 
 $notes1 = <<'END';
@@ -75,6 +80,10 @@ END
                 $notes2
 );
 
+sub _groups_susp_reason_map { map { reverse( /^\s*(\S+)\s*(.*)$/ ) } 
+                              split( "\n", shift->option('groups_susp_reason'));
+}
+
 sub rebless { shift; }
 
 sub export_username {
@@ -170,50 +179,99 @@ sub _export_replace {
     }
   }
 
-  # (sorta) false laziness with FS::svc_acct::replace
-  my @oldgroups = @{$old->usergroup}; #uuuh
-  my @newgroups = $new->radius_groups;
-  my @delgroups = ();
-  foreach my $oldgroup ( @oldgroups ) {
-    if ( grep { $oldgroup eq $_ } @newgroups ) {
-      @newgroups = grep { $oldgroup ne $_ } @newgroups;
-      next;
-    }
-    push @delgroups, $oldgroup;
+  my $error;
+  my (@oldgroups) = $old->radius_groups;
+  my (@newgroups) = $new->radius_groups;
+  $error = $self->sqlreplace_usergroups( $new->svcnum,
+                                         $self->export_username($new),
+                                         $jobnum ? $jobnum : '',
+                                         \@oldgroups,
+                                         \@newgroups,
+                                       );
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
   }
 
-  if ( @delgroups ) {
-    my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'usergroup_delete',
-      $self->export_username($new), @delgroups );
-    unless ( ref($err_or_queue) ) {
-      $dbh->rollback if $oldAutoCommit;
-      return $err_or_queue;
-    }
-    if ( $jobnum ) {
-      my $error = $err_or_queue->depend_insert( $jobnum );
-      if ( $error ) {
-        $dbh->rollback if $oldAutoCommit;
-        return $error;
-      }
-    }
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  '';
+}
+
+sub _export_suspend {
+  my( $self, $svc_acct ) = (shift, shift);
+
+  my $new = $svc_acct->clone_suspended;
+
+  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 $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'insert',
+    'check', $self->export_username($new), $new->radius_check );
+  unless ( ref($err_or_queue) ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $err_or_queue;
   }
 
-  if ( @newgroups ) {
-    my $err_or_queue = $self->sqlradius_queue( $new->svcnum, 'usergroup_insert',
-      $self->export_username($new), @newgroups );
-    unless ( ref($err_or_queue) ) {
-      $dbh->rollback if $oldAutoCommit;
-      return $err_or_queue;
-    }
-    if ( $jobnum ) {
-      my $error = $err_or_queue->depend_insert( $jobnum );
-      if ( $error ) {
-        $dbh->rollback if $oldAutoCommit;
-        return $error;
-      }
-    }
+  my $error;
+  my (@newgroups) = $self->suspended_usergroups($svc_acct);
+  $error =
+    $self->sqlreplace_usergroups( $new->svcnum,
+                                  $self->export_username($new),
+                                 '',
+                                  $svc_acct->usergroup,
+                                 \@newgroups,
+                               );
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
   }
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
+  '';
+}
+
+sub _export_unsuspend {
+  my( $self, $svc_acct ) = (shift, shift);
+
+  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 $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my $err_or_queue = $self->sqlradius_queue( $svc_acct->svcnum, 'insert',
+    'check', $self->export_username($svc_acct), $svc_acct->radius_check );
+  unless ( ref($err_or_queue) ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $err_or_queue;
+  }
+
+  my $error;
+  my (@oldgroups) = $self->suspended_usergroups($svc_acct);
+  $error = $self->sqlreplace_usergroups( $svc_acct->svcnum,
+                                         $self->export_username($svc_acct),
+                                         '',
+                                        \@oldgroups,
+                                        $svc_acct->usergroup,
+                                      );
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
   '';
@@ -240,6 +298,37 @@ sub sqlradius_queue {
   ) or $queue;
 }
 
+sub suspended_usergroups {
+  my ($self, $svc_acct) = (shift, shift);
+
+  return () unless $svc_acct;
+
+  #false laziness with FS::part_export::shellcommands
+  #subclass part_export?
+
+  my $r = $svc_acct->cust_svc->cust_pkg->last_reason;
+  my %reasonmap = $self->_groups_susp_reason_map;
+  my $userspec = '';
+  $userspec = $reasonmap{$r->reasonnum}
+    if exists($reasonmap{$r->reasonnum});
+  $userspec = $reasonmap{$r->reason}
+    if (!$userspec && exists($reasonmap{$r->reason}));
+  my $suspend_user;
+  if ($userspec =~ /^d+$/ ){
+    $suspend_user = qsearchs( 'svc_acct', { 'svcnum' => $userspec } );
+  }elsif ($userspec =~ /^\S+\@\S+$/){
+    my ($username,$domain) = split(/\@/, $userspec);
+    for my $user (qsearch( 'svc_acct', { 'username' => $username } )){
+      $suspend_user = $user if $userspec eq $user->email;
+    }
+  }elsif ($userspec){
+    $suspend_user = qsearchs( 'svc_acct', { 'username' => $userspec } );
+  }
+  #esalf
+  return $suspend_user->radius_groups if $suspend_user;
+  ();
+}
+
 sub sqlradius_insert { #subroutine, not method
   my $dbh = sqlradius_connect(shift, shift, shift);
   my( $table, $username, %attributes ) = @_;
@@ -350,6 +439,46 @@ sub sqlradius_connect {
   DBI->connect(@_) or die $DBI::errstr;
 }
 
+sub sqlreplace_usergroups {
+  my ($self, $svcnum, $username, $jobnum, $old, $new) = @_;
+
+  # (sorta) false laziness with FS::svc_acct::replace
+  my @oldgroups = @$old;
+  my @newgroups = @$new;
+  my @delgroups = ();
+  foreach my $oldgroup ( @oldgroups ) {
+    if ( grep { $oldgroup eq $_ } @newgroups ) {
+      @newgroups = grep { $oldgroup ne $_ } @newgroups;
+      next;
+    }
+    push @delgroups, $oldgroup;
+  }
+
+  if ( @delgroups ) {
+    my $err_or_queue = $self->sqlradius_queue( $svcnum, 'usergroup_delete',
+      $username, @delgroups );
+    return $err_or_queue
+      unless ref($err_or_queue);
+    if ( $jobnum ) {
+      my $error = $err_or_queue->depend_insert( $jobnum );
+      return $error if $error;
+    }
+  }
+
+  if ( @newgroups ) {
+    my $err_or_queue = $self->sqlradius_queue( $svcnum, 'usergroup_insert',
+      $username, @newgroups );
+    return $err_or_queue
+      unless ref($err_or_queue);
+    if ( $jobnum ) {
+      my $error = $err_or_queue->depend_insert( $jobnum );
+      return $error if $error;
+    }
+  }
+  '';
+}
+
+
 #--
 
 =item usage_sessions TIMESTAMP_START TIMESTAMP_END [ SVC_ACCT [ IP [ PREFIX [ SQL_SELECT ] ] ] ]