default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / access_user_session.pm
1 package FS::access_user_session;
2 use base qw( FS::Record );
3
4 use strict;
5
6 =head1 NAME
7
8 FS::access_user_session - Object methods for access_user_session records
9
10 =head1 SYNOPSIS
11
12   use FS::access_user_session;
13
14   $record = new FS::access_user_session \%hash;
15   $record = new FS::access_user_session { 'column' => 'value' };
16
17   $error = $record->insert;
18
19   $error = $new_record->replace($old_record);
20
21   $error = $record->delete;
22
23   $error = $record->check;
24
25 =head1 DESCRIPTION
26
27 An FS::access_user_session object represents a backoffice web session.
28 FS::access_user_session inherits from FS::Record.  The following fields are
29 currently supported:
30
31 =over 4
32
33 =item sessionnum
34
35 Database primary key
36
37 =item sessionkey
38
39 Session key
40
41 =item usernum
42
43 Employee (see L<FS::access_user>)
44
45 =item start_date
46
47 Session start timestamp
48
49 =item last_date
50
51 Last session activity timestamp
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new session.  To add the session to the database, see L<"insert">.
62
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to.  You can ask the object for a copy with the I<hash> method.
65
66 =cut
67
68 # the new method can be inherited from FS::Record, if a table method is defined
69
70 sub table { 'access_user_session'; }
71
72 =item insert
73
74 Adds this record to the database.  If there is an error, returns the error,
75 otherwise returns false.
76
77 =item delete
78
79 Delete this record from the database.
80
81 =item replace OLD_RECORD
82
83 Replaces the OLD_RECORD with this one in the database.  If there is an error,
84 returns the error, otherwise returns false.
85
86 =item check
87
88 Checks all fields to make sure this is a valid session.  If there is
89 an error, returns the error, otherwise returns false.  Called by the insert
90 and replace methods.
91
92 =cut
93
94 sub check {
95   my $self = shift;
96
97   my $error = 
98     $self->ut_numbern('sessionnum')
99     || $self->ut_text('sessionkey')
100     || $self->ut_foreign_key('usernum', 'access_user', 'usernum')
101     || $self->ut_number('start_date')
102     || $self->ut_numbern('last_date')
103   ;
104   return $error if $error;
105
106   $self->last_date( $self->start_date ) unless $self->last_date;
107
108   $self->SUPER::check;
109 }
110
111 =item access_user
112
113 Returns the employee (see L<FS::access_user>) for this session.
114
115 =item touch_last_date
116
117 =cut
118
119 sub touch_last_date {
120   my $self = shift;
121   my $old_last_date = $self->last_date;
122   $self->last_date(time);
123   return if $old_last_date >= $self->last_date;
124   my $error = $self->replace;
125   die $error if $error;
126 }
127
128 =item logout
129
130 =cut
131
132 sub logout {
133   my $self = shift;
134   my $error = $self->delete;
135   die $error if $error;
136 }
137
138 =back
139
140 =head1 BUGS
141
142 =head1 SEE ALSO
143
144 L<FS::Record>, schema.html from the base documentation.
145
146 =cut
147
148 1;
149