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