0d766bd221c904fe28055cb8c39e56230717f14f
[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
8 @ISA = qw(FS::Record);
9
10 =head1 NAME
11
12 FS::session - Object methods for session records
13
14 =head1 SYNOPSIS
15
16   use FS::session;
17
18   $record = new FS::session \%hash;
19   $record = new FS::session {
20     'portnum' => 1,
21     'svcnum'  => 2,
22     'login'   => $timestamp,
23     'logout'  => $timestamp,
24   };
25
26   $error = $record->insert;
27
28   $error = $new_record->replace($old_record);
29
30   $error = $record->delete;
31
32   $error = $record->check;
33
34 =head1 DESCRIPTION
35
36 An FS::session object represents an user login session.  FS::session inherits
37 from FS::Record.  The following fields are currently supported:
38
39 =over 4
40
41 =item sessionnum - primary key
42
43 =item portnum - NAS port for this session - see L<FS::port>
44
45 =item svcnum - User for this session - see L<FS::svc_acct>
46
47 =item login - timestamp indicating the beginning of this user session.
48
49 =item logout - timestamp indicating the end of this user session.  May be null,
50                which indicates a currently open session.
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new example.  To add the example to the database, see L<"insert">.
61
62 Note that this stores the hash reference, not a distinct copy of the hash it
63 points to.  You can ask the object for a copy with the I<hash> method.
64
65 =cut
66
67 # the new method can be inherited from FS::Record, if a table method is defined
68
69 sub table { 'session'; }
70
71 =item insert
72
73 Adds this record to the database.  If there is an error, returns the error,
74 otherwise returns false.  If the `login' field is empty, it is replaced with
75 the current time.
76
77 =cut
78
79 sub insert {
80   my $self = shift;
81   my $error;
82
83   local $SIG{HUP} = 'IGNORE';
84   local $SIG{INT} = 'IGNORE';
85   local $SIG{QUIT} = 'IGNORE';
86   local $SIG{TERM} = 'IGNORE';
87   local $SIG{TSTP} = 'IGNORE';
88   local $SIG{PIPE} = 'IGNORE';
89
90   $error = $self->check;
91   return $error if $error;
92
93   $self->setfield('login', time()) unless $self->getfield('login');
94
95   $error = $self->SUPER::insert;
96   return $error if $error;
97
98   #session-starting callback!
99
100   '';
101
102 }
103
104 =item delete
105
106 Delete this record from the database.
107
108 =cut
109
110 # the delete method can be inherited from FS::Record
111
112 =item replace OLD_RECORD
113
114 Replaces the OLD_RECORD with this one in the database.  If there is an error,
115 returns the error, otherwise returns false.  If the `logout' field is empty,
116 it is replaced with the current time.
117
118 =cut
119
120 sub replace {
121   my $self = shift;
122   my $error;
123
124   local $SIG{HUP} = 'IGNORE';
125   local $SIG{INT} = 'IGNORE';
126   local $SIG{QUIT} = 'IGNORE';
127   local $SIG{TERM} = 'IGNORE';
128   local $SIG{TSTP} = 'IGNORE';
129   local $SIG{PIPE} = 'IGNORE';
130
131   $error = $self->check;
132   return $error if $error;
133
134   $self->setfield('logout', time()) unless $self->getfield('logout');
135
136   $error = $self->SUPER::replace;
137   return $error if $error;
138
139   #session-ending callback!
140
141   '';
142 }
143
144 =item check
145
146 Checks all fields to make sure this is a valid example.  If there is
147 an error, returns the error, otherwise returns false.  Called by the insert
148 and replace methods.
149
150 =cut
151
152 # the check method should currently be supplied - FS::Record contains some
153 # data checking routines
154
155 sub check {
156   my $self = shift;
157   my $error =
158     $self->ut_numbern('sessionnum')
159     || $self->ut_number('portnum')
160     || $self->ut_number('svcnum')
161     || $self->ut_numbern('login')
162     || $self->ut_numbern('logout')
163   ;
164   return $error if $error;
165   return "Unknown svcnum"
166     unless qsearchs('svc_acct', { 'svcnum' => $self->svcnum } );
167   '';
168 }
169
170 =back
171
172 =head1 VERSION
173
174 $Id: session.pm,v 1.1 2000-10-27 20:18:32 ivan Exp $
175
176 =head1 BUGS
177
178 The author forgot to customize this manpage.
179
180 =head1 SEE ALSO
181
182 L<FS::Record>, schema.html from the base documentation.
183
184 =cut
185
186 1;
187