4 use vars qw( @ISA $conf );
6 use FS::Record qw( qsearchs );
11 @ISA = qw(FS::Record);
13 $FS::UID::callback{'FS::session'} = sub {
19 FS::session - Object methods for session records
25 $record = new FS::session \%hash;
26 $record = new FS::session {
29 'login' => $timestamp,
30 'logout' => $timestamp,
33 $error = $record->insert;
35 $error = $new_record->replace($old_record);
37 $error = $record->delete;
39 $error = $record->check;
41 $error = $record->nas_heartbeat($timestamp);
45 An FS::session object represents an user login session. FS::session inherits
46 from FS::Record. The following fields are currently supported:
50 =item sessionnum - primary key
52 =item portnum - NAS port for this session - see L<FS::port>
54 =item svcnum - User for this session - see L<FS::svc_acct>
56 =item login - timestamp indicating the beginning of this user session.
58 =item logout - timestamp indicating the end of this user session. May be null,
59 which indicates a currently open session.
69 Creates a new session. To add the session to the database, see L<"insert">.
71 Note that this stores the hash reference, not a distinct copy of the hash it
72 points to. You can ask the object for a copy with the I<hash> method.
76 # the new method can be inherited from FS::Record, if a table method is defined
78 sub table { 'session'; }
82 Adds this record to the database. If there is an error, returns the error,
83 otherwise returns false. If the `login' field is empty, it is replaced with
92 local $SIG{HUP} = 'IGNORE';
93 local $SIG{INT} = 'IGNORE';
94 local $SIG{QUIT} = 'IGNORE';
95 local $SIG{TERM} = 'IGNORE';
96 local $SIG{TSTP} = 'IGNORE';
97 local $SIG{PIPE} = 'IGNORE';
99 $error = $self->check;
100 return $error if $error;
102 my $oldAutoCommit = $FS::UID::AutoCommit;
103 local $FS::UID::AutoCommit = 0;
106 if ( qsearchs('session', { 'portnum' => $self->portnum, 'logout' => '' } ) ) {
107 $dbh->rollback if $oldAutoCommit;
108 return "a session on that port is already open!";
111 $self->setfield('login', time()) unless $self->getfield('login');
113 $error = $self->SUPER::insert;
115 $dbh->rollback if $oldAutoCommit;
119 $self->nas_heartbeat($self->getfield('login'));
121 #session-starting callback
122 #redundant with heartbeat, yuck
123 my $port = qsearchs('port',{'portnum'=>$self->portnum});
124 my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
126 my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
128 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
135 Delete this record from the database.
139 # the delete method can be inherited from FS::Record
141 =item replace OLD_RECORD
143 Replaces the OLD_RECORD with this one in the database. If there is an error,
144 returns the error, otherwise returns false. If the `logout' field is empty,
145 it is replaced with the current time.
150 my($self, $old) = @_;
153 local $SIG{HUP} = 'IGNORE';
154 local $SIG{INT} = 'IGNORE';
155 local $SIG{QUIT} = 'IGNORE';
156 local $SIG{TERM} = 'IGNORE';
157 local $SIG{TSTP} = 'IGNORE';
158 local $SIG{PIPE} = 'IGNORE';
160 my $oldAutoCommit = $FS::UID::AutoCommit;
161 local $FS::UID::AutoCommit = 0;
164 $error = $self->check;
166 $dbh->rollback if $oldAutoCommit;
170 $self->setfield('logout', time()) unless $self->getfield('logout');
172 $error = $self->SUPER::replace($old);
174 $dbh->rollback if $oldAutoCommit;
178 $self->nas_heartbeat($self->getfield('logout'));
180 #session-ending callback
181 #redundant with heartbeat, yuck
182 my $port = qsearchs('port',{'portnum'=>$self->portnum});
183 my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
185 my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
187 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
194 Checks all fields to make sure this is a valid session. If there is
195 an error, returns the error, otherwise returns false. Called by the insert
200 # the check method should currently be supplied - FS::Record contains some
201 # data checking routines
206 $self->ut_numbern('sessionnum')
207 || $self->ut_number('portnum')
208 || $self->ut_number('svcnum')
209 || $self->ut_numbern('login')
210 || $self->ut_numbern('logout')
212 return $error if $error;
213 return "Unknown svcnum"
214 unless qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
220 Heartbeats the nas associated with this session (see L<FS::nas>).
226 my $port = qsearchs('port',{'portnum'=>$self->portnum});
227 my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
228 $nas->heartbeat(shift);
233 Returns the svc_acct record associated with this session (see L<FS::svc_acct>).
239 qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
246 Maybe you shouldn't be able to insert a session if there's currently an open
247 session on that port. Or maybe the open session on that port should be flagged
248 as problematic? autoclosed? *sigh*
250 Hmm, sessions refer to current svc_acct records... probably need to constrain
251 deletions to svc_acct records such that no svc_acct records are deleted which
252 have a session (even if long-closed).
256 L<FS::Record>, schema.html from the base documentation.