session monitor
[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   $self->setfield('login', time()) unless $self->getfield('login');
98
99   $error = $self->SUPER::insert;
100   return $error if $error;
101
102   $self->nas_heartbeat($self->getfield('login'));
103
104   #session-starting callback!
105
106   '';
107
108 }
109
110 =item delete
111
112 Delete this record from the database.
113
114 =cut
115
116 # the delete method can be inherited from FS::Record
117
118 =item replace OLD_RECORD
119
120 Replaces the OLD_RECORD with this one in the database.  If there is an error,
121 returns the error, otherwise returns false.  If the `logout' field is empty,
122 it is replaced with the current time.
123
124 =cut
125
126 sub replace {
127   my $self = shift;
128   my $error;
129
130   local $SIG{HUP} = 'IGNORE';
131   local $SIG{INT} = 'IGNORE';
132   local $SIG{QUIT} = 'IGNORE';
133   local $SIG{TERM} = 'IGNORE';
134   local $SIG{TSTP} = 'IGNORE';
135   local $SIG{PIPE} = 'IGNORE';
136
137   $error = $self->check;
138   return $error if $error;
139
140   $self->setfield('logout', time()) unless $self->getfield('logout');
141
142   $error = $self->SUPER::replace;
143   return $error if $error;
144
145   $self->nas_heartbeat($self->getfield('logout'));
146
147   #session-ending callback!
148
149   '';
150 }
151
152 =item check
153
154 Checks all fields to make sure this is a valid session.  If there is
155 an error, returns the error, otherwise returns false.  Called by the insert
156 and replace methods.
157
158 =cut
159
160 # the check method should currently be supplied - FS::Record contains some
161 # data checking routines
162
163 sub check {
164   my $self = shift;
165   my $error =
166     $self->ut_numbern('sessionnum')
167     || $self->ut_number('portnum')
168     || $self->ut_number('svcnum')
169     || $self->ut_numbern('login')
170     || $self->ut_numbern('logout')
171   ;
172   return $error if $error;
173   return "Unknown svcnum"
174     unless qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
175   '';
176 }
177
178 =item nas_heartbeat
179
180 Heartbeats the nas associated with this session (see L<FS::nas>).
181
182 =cut
183
184 sub nas_heartbeat {
185   my $self = shift;
186   my $port = qsearchs('port',{'portnum'=>$self->portnum});
187   my $nas = qsearchs('nas',{'nasnum'=>$port->nasnum});
188   $nas->heartbeat(shift);
189 }
190
191 =back
192
193 =head1 VERSION
194
195 $Id: session.pm,v 1.2 2000-11-07 15:00:37 ivan Exp $
196
197 =head1 BUGS
198
199 The author forgot to customize this manpage.
200
201 =head1 SEE ALSO
202
203 L<FS::Record>, schema.html from the base documentation.
204
205 =cut
206
207 1;
208