4 use vars qw( @ISA $conf $start $stop );
6 use FS::Record qw( qsearchs );
11 @ISA = qw(FS::Record);
13 $FS::UID::callback{'FS::session'} = sub {
15 $start = $conf->exists('session-start') ? $conf->config('session-start') : '';
16 $stop = $conf->exists('session-stop') ? $conf->config('session-stop') : '';
21 FS::session - Object methods for session records
27 $record = new FS::session \%hash;
28 $record = new FS::session {
31 'login' => $timestamp,
32 'logout' => $timestamp,
35 $error = $record->insert;
37 $error = $new_record->replace($old_record);
39 $error = $record->delete;
41 $error = $record->check;
43 $error = $record->nas_heartbeat($timestamp);
47 An FS::session object represents an user login session. FS::session inherits
48 from FS::Record. The following fields are currently supported:
52 =item sessionnum - primary key
54 =item portnum - NAS port for this session - see L<FS::port>
56 =item svcnum - User for this session - see L<FS::svc_acct>
58 =item login - timestamp indicating the beginning of this user session.
60 =item logout - timestamp indicating the end of this user session. May be null,
61 which indicates a currently open session.
71 Creates a new session. To add the session to the database, see L<"insert">.
73 Note that this stores the hash reference, not a distinct copy of the hash it
74 points to. You can ask the object for a copy with the I<hash> method.
78 # the new method can be inherited from FS::Record, if a table method is defined
80 sub table { 'session'; }
84 Adds this record to the database. If there is an error, returns the error,
85 otherwise returns false. If the `login' field is empty, it is replaced with
94 local $SIG{HUP} = 'IGNORE';
95 local $SIG{INT} = 'IGNORE';
96 local $SIG{QUIT} = 'IGNORE';
97 local $SIG{TERM} = 'IGNORE';
98 local $SIG{TSTP} = 'IGNORE';
99 local $SIG{PIPE} = 'IGNORE';
101 $error = $self->check;
102 return $error if $error;
104 my $oldAutoCommit = $FS::UID::AutoCommit;
105 local $FS::UID::AutoCommit = 0;
108 if ( qsearchs('session', { 'portnum' => $self->portnum, 'logout' => '' } ) ) {
109 $dbh->rollback if $oldAutoCommit;
110 return "a session on that port is already open!";
113 $self->setfield('login', time()) unless $self->getfield('login');
115 $error = $self->SUPER::insert;
117 $dbh->rollback if $oldAutoCommit;
121 $self->nas_heartbeat($self->getfield('login'));
123 #session-starting callback
124 #redundant with heartbeat, yuck
125 my $port = qsearchs('port',{'portnum'=>$self->portnum});
126 my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
128 my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
129 system( eval qq("$start") ) if $start;
131 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
138 Delete this record from the database.
142 # the delete method can be inherited from FS::Record
144 =item replace OLD_RECORD
146 Replaces the OLD_RECORD with this one in the database. If there is an error,
147 returns the error, otherwise returns false. If the `logout' field is empty,
148 it is replaced with the current time.
153 my($self, $old) = @_;
156 local $SIG{HUP} = 'IGNORE';
157 local $SIG{INT} = 'IGNORE';
158 local $SIG{QUIT} = 'IGNORE';
159 local $SIG{TERM} = 'IGNORE';
160 local $SIG{TSTP} = 'IGNORE';
161 local $SIG{PIPE} = 'IGNORE';
163 my $oldAutoCommit = $FS::UID::AutoCommit;
164 local $FS::UID::AutoCommit = 0;
167 $error = $self->check;
169 $dbh->rollback if $oldAutoCommit;
173 $self->setfield('logout', time()) unless $self->getfield('logout');
175 $error = $self->SUPER::replace($old);
177 $dbh->rollback if $oldAutoCommit;
181 $self->nas_heartbeat($self->getfield('logout'));
183 #session-ending callback
184 #redundant with heartbeat, yuck
185 my $port = qsearchs('port',{'portnum'=>$self->portnum});
186 my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
188 my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
189 system( eval qq("$stop") ) if $stop;
191 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
198 Checks all fields to make sure this is a valid session. If there is
199 an error, returns the error, otherwise returns false. Called by the insert
204 # the check method should currently be supplied - FS::Record contains some
205 # data checking routines
210 $self->ut_numbern('sessionnum')
211 || $self->ut_number('portnum')
212 || $self->ut_number('svcnum')
213 || $self->ut_numbern('login')
214 || $self->ut_numbern('logout')
216 return $error if $error;
217 return "Unknown svcnum"
218 unless qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
224 Heartbeats the nas associated with this session (see L<FS::nas>).
230 my $port = qsearchs('port',{'portnum'=>$self->portnum});
231 my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
232 $nas->heartbeat(shift);
237 Returns the svc_acct record associated with this session (see L<FS::svc_acct>).
243 qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
250 Maybe you shouldn't be able to insert a session if there's currently an open
251 session on that port. Or maybe the open session on that port should be flagged
252 as problematic? autoclosed? *sigh*
254 Hmm, sessions refer to current svc_acct records... probably need to constrain
255 deletions to svc_acct records such that no svc_acct records are deleted which
256 have a session (even if long-closed).
260 L<FS::Record>, schema.html from the base documentation.