communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[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   $self->nas_heartbeat($self->getfield('login'));
122
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});
127     #kcuy
128   my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
129   system( eval qq("$start") ) if $start;
130   
131   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
132   '';
133
134 }
135
136 =item delete
137
138 Delete this record from the database.
139
140 =cut
141
142 # the delete method can be inherited from FS::Record
143
144 =item replace OLD_RECORD
145
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.
149
150 =cut
151
152 sub replace {
153   my($self, $old) = @_;
154   my $error;
155
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';
162
163   my $oldAutoCommit = $FS::UID::AutoCommit;
164   local $FS::UID::AutoCommit = 0;
165   my $dbh = dbh;
166
167   $error = $self->check;
168   if ( $error ) {
169     $dbh->rollback if $oldAutoCommit;
170     return $error;
171   }
172
173   $self->setfield('logout', time()) unless $self->getfield('logout');
174
175   $error = $self->SUPER::replace($old);
176   if ( $error ) {
177     $dbh->rollback if $oldAutoCommit;
178     return $error;
179   }
180
181   $self->nas_heartbeat($self->getfield('logout'));
182
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});
187     #kcuy
188   my( $ip, $nasip, $nasfqdn ) = ( $port->ip, $nas->nasip, $nas->nasfqdn );
189   system( eval qq("$stop") ) if $stop;
190
191   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
192
193   '';
194 }
195
196 =item check
197
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
200 and replace methods.
201
202 =cut
203
204 # the check method should currently be supplied - FS::Record contains some
205 # data checking routines
206
207 sub check {
208   my $self = shift;
209   my $error =
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')
215   ;
216   return $error if $error;
217   return "Unknown svcnum"
218     unless qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
219   $self->SUPER::check;
220 }
221
222 =item nas_heartbeat
223
224 Heartbeats the nas associated with this session (see L<FS::nas>).
225
226 =cut
227
228 sub nas_heartbeat {
229   my $self = shift;
230   my $port = qsearchs('port',{'portnum'=>$self->portnum});
231   my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
232   $nas->heartbeat(shift);
233 }
234
235 =item svc_acct
236
237 Returns the svc_acct record associated with this session (see L<FS::svc_acct>).
238
239 =cut
240
241 sub svc_acct {
242   my $self = shift;
243   qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
244 }
245
246 =back
247
248 =head1 BUGS
249
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*
253
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).
257
258 =head1 SEE ALSO
259
260 L<FS::Record>, schema.html from the base documentation.
261
262 =cut
263
264 1;
265