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