4 use vars qw( @ISA $conf $start $stop );
5 use FS::Record qw( qsearchs );
10 @ISA = qw(FS::Record);
12 $FS::UID::callback{'FS::session'} = sub {
14 $start = $conf->exists('session-start') ? $conf->config('session-start') : '';
15 $stop = $conf->exists('session-stop') ? $conf->config('session-stop') : '';
20 FS::session - Object methods for session records
26 $record = new FS::session \%hash;
27 $record = new FS::session {
30 'login' => $timestamp,
31 'logout' => $timestamp,
34 $error = $record->insert;
36 $error = $new_record->replace($old_record);
38 $error = $record->delete;
40 $error = $record->check;
42 $error = $record->nas_heartbeat($timestamp);
46 An FS::session object represents an user login session. FS::session inherits
47 from FS::Record. The following fields are currently supported:
51 =item sessionnum - primary key
53 =item portnum - NAS port for this session - see L<FS::port>
55 =item svcnum - User for this session - see L<FS::svc_acct>
57 =item login - timestamp indicating the beginning of this user session.
59 =item logout - timestamp indicating the end of this user session. May be null,
60 which indicates a currently open session.
70 Creates a new session. To add the session to the database, see L<"insert">.
72 Note that this stores the hash reference, not a distinct copy of the hash it
73 points to. You can ask the object for a copy with the I<hash> method.
77 # the new method can be inherited from FS::Record, if a table method is defined
79 sub table { 'session'; }
83 Adds this record to the database. If there is an error, returns the error,
84 otherwise returns false. If the `login' field is empty, it is replaced with
93 local $SIG{HUP} = 'IGNORE';
94 local $SIG{INT} = 'IGNORE';
95 local $SIG{QUIT} = 'IGNORE';
96 local $SIG{TERM} = 'IGNORE';
97 local $SIG{TSTP} = 'IGNORE';
98 local $SIG{PIPE} = 'IGNORE';
100 $error = $self->check;
101 return $error if $error;
103 return "a session on that port is already open!"
104 if qsearchs('session', { 'portnum' => $self->portnum, 'logout' => '' } );
106 $self->setfield('login', time()) unless $self->getfield('login');
108 $error = $self->SUPER::insert;
109 return $error if $error;
111 $self->nas_heartbeat($self->getfield('login'));
113 #session-starting callback
114 #redundant with heartbeat, yuck
115 my $port = qsearchs('port',{'portnum'=>$self->portnum});
116 my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
118 my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
119 system( eval qq("$start") ) if $start;
127 Delete this record from the database.
131 # the delete method can be inherited from FS::Record
133 =item replace OLD_RECORD
135 Replaces the OLD_RECORD with this one in the database. If there is an error,
136 returns the error, otherwise returns false. If the `logout' field is empty,
137 it is replaced with the current time.
142 my($self, $old) = @_;
145 local $SIG{HUP} = 'IGNORE';
146 local $SIG{INT} = 'IGNORE';
147 local $SIG{QUIT} = 'IGNORE';
148 local $SIG{TERM} = 'IGNORE';
149 local $SIG{TSTP} = 'IGNORE';
150 local $SIG{PIPE} = 'IGNORE';
152 $error = $self->check;
153 return $error if $error;
155 $self->setfield('logout', time()) unless $self->getfield('logout');
157 $error = $self->SUPER::replace($old);
158 return $error if $error;
160 $self->nas_heartbeat($self->getfield('logout'));
162 #session-ending callback
163 #redundant with heartbeat, yuck
164 my $port = qsearchs('port',{'portnum'=>$self->portnum});
165 my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
167 my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
168 session( eval qq("$stop") ) if $stop;
175 Checks all fields to make sure this is a valid session. If there is
176 an error, returns the error, otherwise returns false. Called by the insert
181 # the check method should currently be supplied - FS::Record contains some
182 # data checking routines
187 $self->ut_numbern('sessionnum')
188 || $self->ut_number('portnum')
189 || $self->ut_number('svcnum')
190 || $self->ut_numbern('login')
191 || $self->ut_numbern('logout')
193 return $error if $error;
194 return "Unknown svcnum"
195 unless qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
201 Heartbeats the nas associated with this session (see L<FS::nas>).
207 my $port = qsearchs('port',{'portnum'=>$self->portnum});
208 my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
209 $nas->heartbeat(shift);
214 Returns the svc_acct record associated with this session (see L<FS::svc_acct>).
220 qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
227 $Id: session.pm,v 1.4 2000-12-08 22:22:31 ivan Exp $
231 Maybe you shouldn't be able to insert a session if there's currently an open
232 session on that port. Or maybe the open session on that port should be flagged
233 as problematic? autoclosed? *sigh*
235 Hmm, sessions refer to current svc_acct records... probably need to constrain
236 deletions to svc_acct records such that no svc_acct records are deleted which
237 have a session (even if long-closed).
241 L<FS::Record>, schema.html from the base documentation.