[freeside-commits] freeside/FS/FS/part_export shellcommands.pm, 1.40, 1.41 sqlradius.pm, 1.19, 1.20

Jeff Finucane,420,, jeff at wavetail.420.am
Thu Oct 26 19:12:46 PDT 2006


Update of /home/cvs/cvsroot/freeside/FS/FS/part_export
In directory wavetail:/tmp/cvs-serv21156/FS/FS/part_export

Modified Files:
	shellcommands.pm sqlradius.pm 
Log Message:
export enhancements for suspend reasons

Index: shellcommands.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_export/shellcommands.pm,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -d -r1.40 -r1.41
--- shellcommands.pm	29 Jun 2006 15:45:57 -0000	1.40
+++ shellcommands.pm	27 Oct 2006 02:12:44 -0000	1.41
@@ -4,6 +4,7 @@
 use Tie::IxHash;
 use String::ShellQuote;
 use FS::part_export;
+use FS::Record qw( qsearch qsearchs );
 
 @ISA = qw(FS::part_export);
 
@@ -60,6 +61,10 @@
                type=>'select', options=>[qw(crypt md5)],
                default => 'crypt',
              },
+  'groups_susp_reason' => { label =>
+                             'Radius group mapping to reason (via template user)',
+			    type  => 'textarea',
+			  },
 ;
 
 %info = (
@@ -162,11 +167,22 @@
   <LI><code>$shell</code>
   <LI><code>$quota</code>
   <LI><code>@radius_groups</code>
+  <LI><code>$reasonnum (when suspending)</code>
+  <LI><code>$reasontext (when suspending)</code>
+  <LI><code>$reasontypenum (when suspending)</code>
+  <LI><code>$reasontypetext (when suspending)</code>
   <LI>All other fields in <a href="../docs/schema.html#svc_acct">svc_acct</a> are also available.
 </UL>
 END
 );
 
+sub _groups_susp_reason_map { shift->_map('groups_susp_reason'); }
+
+sub _map {
+  my $self = shift;
+  map { reverse(/^\s*(\S+)\s*(.*)\s*$/) } split("\n", $self->option(shift) );
+}
+
 sub rebless { shift; }
 
 sub _export_insert {
@@ -239,6 +255,39 @@
 
   @radius_groups = $svc_acct->radius_groups;
 
+  my ($reasonnum, $reasontext, $reasontypenum, $reasontypetext);
+  if ( $cust_pkg && $action eq 'suspend' && (my $r = $cust_pkg->last_reason)){
+    $reasonnum = $r->reasonnum;
+    $reasontext = $r->reason;
+    $reasontypenum = $r->reason_type;
+    $reasontypetext = $r->reasontype->type;
+
+    my %reasonmap = $self->_groups_susp_reason_map;
+    my $userspec = '';
+    $userspec = $reasonmap{$reasonnum}
+      if exists($reasonmap{$reasonnum});
+    $userspec = $reasonmap{$reasontext}
+      if (!$userspec && exists($reasonmap{$reasontext}));
+
+    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 } );
+    }
+
+    @radius_groups = $suspend_user->radius_groups
+      if $suspend_user;  
+
+  }else{
+    $reasonnum = $reasontext = $reasontypenum = $reasontypetext = '';
+  }
+
   $self->shellcommands_queue( $svc_acct->svcnum,
     user         => $self->option('user')||'root',
     host         => $self->machine,

Index: sqlradius.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_export/sqlradius.pm,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- sqlradius.pm	9 Aug 2005 21:38:18 -0000	1.19
+++ sqlradius.pm	27 Oct 2006 02:12:44 -0000	1.20
@@ -2,7 +2,7 @@
 
 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 @@
     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 @@
                 $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 @@
     }
   }
 
-  # (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 @@
   ) 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 @@
   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 ] ] ] ]



More information about the freeside-commits mailing list