session monitor updates
[freeside.git] / FS / FS / session.pm
1 package FS::session;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearchs );
6 use FS::svc_acct;
7 use FS::port;
8 use FS::nas;
9
10 @ISA = qw(FS::Record);
11
12 =head1 NAME
13
14 FS::session - Object methods for session records
15
16 =head1 SYNOPSIS
17
18   use FS::session;
19
20   $record = new FS::session \%hash;
21   $record = new FS::session {
22     'portnum' => 1,
23     'svcnum'  => 2,
24     'login'   => $timestamp,
25     'logout'  => $timestamp,
26   };
27
28   $error = $record->insert;
29
30   $error = $new_record->replace($old_record);
31
32   $error = $record->delete;
33
34   $error = $record->check;
35
36   $error = $record->nas_heartbeat($timestamp);
37
38 =head1 DESCRIPTION
39
40 An FS::session object represents an user login session.  FS::session inherits
41 from FS::Record.  The following fields are currently supported:
42
43 =over 4
44
45 =item sessionnum - primary key
46
47 =item portnum - NAS port for this session - see L<FS::port>
48
49 =item svcnum - User for this session - see L<FS::svc_acct>
50
51 =item login - timestamp indicating the beginning of this user session.
52
53 =item logout - timestamp indicating the end of this user session.  May be null,
54                which indicates a currently open session.
55
56 =back
57
58 =head1 METHODS
59
60 =over 4
61
62 =item new HASHREF
63
64 Creates a new session.  To add the session to the database, see L<"insert">.
65
66 Note that this stores the hash reference, not a distinct copy of the hash it
67 points to.  You can ask the object for a copy with the I<hash> method.
68
69 =cut
70
71 # the new method can be inherited from FS::Record, if a table method is defined
72
73 sub table { 'session'; }
74
75 =item insert
76
77 Adds this record to the database.  If there is an error, returns the error,
78 otherwise returns false.  If the `login' field is empty, it is replaced with
79 the current time.
80
81 =cut
82
83 sub insert {
84   my $self = shift;
85   my $error;
86
87   local $SIG{HUP} = 'IGNORE';
88   local $SIG{INT} = 'IGNORE';
89   local $SIG{QUIT} = 'IGNORE';
90   local $SIG{TERM} = 'IGNORE';
91   local $SIG{TSTP} = 'IGNORE';
92   local $SIG{PIPE} = 'IGNORE';
93
94   $error = $self->check;
95   return $error if $error;
96
97   return "a session on that port is already open!"
98     if qsearchs('session', { 'portnum' => $self->portnum, 'logout' => '' } );
99
100   $self->setfield('login', time()) unless $self->getfield('login');
101
102   $error = $self->SUPER::insert;
103   return $error if $error;
104
105   $self->nas_heartbeat($self->getfield('login'));
106
107   #session-starting callback!
108
109   '';
110
111 }
112
113 =item delete
114
115 Delete this record from the database.
116
117 =cut
118
119 # the delete method can be inherited from FS::Record
120
121 =item replace OLD_RECORD
122
123 Replaces the OLD_RECORD with this one in the database.  If there is an error,
124 returns the error, otherwise returns false.  If the `logout' field is empty,
125 it is replaced with the current time.
126
127 =cut
128
129 sub replace {
130   my($self, $old) = @_;
131   my $error;
132
133   local $SIG{HUP} = 'IGNORE';
134   local $SIG{INT} = 'IGNORE';
135   local $SIG{QUIT} = 'IGNORE';
136   local $SIG{TERM} = 'IGNORE';
137   local $SIG{TSTP} = 'IGNORE';
138   local $SIG{PIPE} = 'IGNORE';
139
140   $error = $self->check;
141   return $error if $error;
142
143   $self->setfield('logout', time()) unless $self->getfield('logout');
144
145   $error = $self->SUPER::replace($old);
146   return $error if $error;
147
148   $self->nas_heartbeat($self->getfield('logout'));
149
150   #session-ending callback!
151
152   '';
153 }
154
155 =item check
156
157 Checks all fields to make sure this is a valid session.  If there is
158 an error, returns the error, otherwise returns false.  Called by the insert
159 and replace methods.
160
161 =cut
162
163 # the check method should currently be supplied - FS::Record contains some
164 # data checking routines
165
166 sub check {
167   my $self = shift;
168   my $error =
169     $self->ut_numbern('sessionnum')
170     || $self->ut_number('portnum')
171     || $self->ut_number('svcnum')
172     || $self->ut_numbern('login')
173     || $self->ut_numbern('logout')
174   ;
175   return $error if $error;
176   return "Unknown svcnum"
177     unless qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
178   '';
179 }
180
181 =item nas_heartbeat
182
183 Heartbeats the nas associated with this session (see L<FS::nas>).
184
185 =cut
186
187 sub nas_heartbeat {
188   my $self = shift;
189   my $port = qsearchs('port',{'portnum'=>$self->portnum});
190   my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
191   $nas->heartbeat(shift);
192 }
193
194 =item svc_acct
195
196 Returns the svc_acct record associated with this session (see L<FS::svc_acct>).
197
198 =cut
199
200 sub svc_acct {
201   my $self = shift;
202   qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
203 }
204
205 =back
206
207 =head1 VERSION
208
209 $Id: session.pm,v 1.3 2000-12-03 20:25:20 ivan Exp $
210
211 =head1 BUGS
212
213 Maybe you shouldn't be able to insert a session if there's currently an open
214 session on that port.  Or maybe the open session on that port should be flagged
215 as problematic?  autoclosed?  *sigh*
216
217 Hmm, sessions refer to current svc_acct records... probably need to constrain
218 deletions to svc_acct records such that no svc_acct records are deleted which
219 have a session (even if long-closed).
220
221 =head1 SEE ALSO
222
223 L<FS::Record>, schema.html from the base documentation.
224
225 =cut
226
227 1;
228