session callbacks
[freeside.git] / FS / FS / session.pm
index b85a582..61dc899 100644 (file)
@@ -1,7 +1,7 @@
 package FS::session;
 
 use strict;
-use vars qw( @ISA );
+use vars qw( @ISA $conf $start $stop );
 use FS::Record qw( qsearchs );
 use FS::svc_acct;
 use FS::port;
@@ -9,6 +9,12 @@ use FS::nas;
 
 @ISA = qw(FS::Record);
 
+$FS::UID::callback{'FS::session'} = sub {
+  $conf = new FS::Conf;
+  $start = $conf->exists('session-start') ? $conf->config('session-start') : '';
+  $stop = $conf->exists('session-stop') ? $conf->config('session-stop') : '';
+};
+
 =head1 NAME
 
 FS::session - Object methods for session records
@@ -94,6 +100,9 @@ sub insert {
   $error = $self->check;
   return $error if $error;
 
+  return "a session on that port is already open!"
+    if qsearchs('session', { 'portnum' => $self->portnum, 'logout' => '' } );
+
   $self->setfield('login', time()) unless $self->getfield('login');
 
   $error = $self->SUPER::insert;
@@ -101,7 +110,13 @@ sub insert {
 
   $self->nas_heartbeat($self->getfield('login'));
 
-  #session-starting callback!
+  #session-starting callback
+    #redundant with heartbeat, yuck
+  my $port = qsearchs('port',{'portnum'=>$self->portnum});
+  my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
+    #kcuy
+  my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
+  system( eval qq("$start") ) if $start;
 
   '';
 
@@ -124,7 +139,7 @@ it is replaced with the current time.
 =cut
 
 sub replace {
-  my $self = shift;
+  my($self, $old) = @_;
   my $error;
 
   local $SIG{HUP} = 'IGNORE';
@@ -139,12 +154,18 @@ sub replace {
 
   $self->setfield('logout', time()) unless $self->getfield('logout');
 
-  $error = $self->SUPER::replace;
+  $error = $self->SUPER::replace($old);
   return $error if $error;
 
   $self->nas_heartbeat($self->getfield('logout'));
 
-  #session-ending callback!
+  #session-ending callback
+  #redundant with heartbeat, yuck
+  my $port = qsearchs('port',{'portnum'=>$self->portnum});
+  my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
+    #kcuy
+  my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
+  session( eval qq("$stop") ) if $stop;
 
   '';
 }
@@ -188,15 +209,32 @@ sub nas_heartbeat {
   $nas->heartbeat(shift);
 }
 
+=item svc_acct
+
+Returns the svc_acct record associated with this session (see L<FS::svc_acct>).
+
+=cut
+
+sub svc_acct {
+  my $self = shift;
+  qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
+}
+
 =back
 
 =head1 VERSION
 
-$Id: session.pm,v 1.2 2000-11-07 15:00:37 ivan Exp $
+$Id: session.pm,v 1.4 2000-12-08 22:22:31 ivan Exp $
 
 =head1 BUGS
 
-The author forgot to customize this manpage.
+Maybe you shouldn't be able to insert a session if there's currently an open
+session on that port.  Or maybe the open session on that port should be flagged
+as problematic?  autoclosed?  *sigh*
+
+Hmm, sessions refer to current svc_acct records... probably need to constrain
+deletions to svc_acct records such that no svc_acct records are deleted which
+have a session (even if long-closed).
 
 =head1 SEE ALSO