File Coverage

blib/lib/OpenFrame/WebApp/Session.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             OpenFrame::WebApp::Session - sessions for OpenFrame-WebApp
4              
5             =head1 SYNOPSIS
6              
7             use OpenFrame::WebApp::Session;
8              
9             my $session = new OpenFrame::WebApp::Session()
10             ->set($key1, $val1)->set($key2, $val2);
11              
12             $val = $session->get( $key );
13              
14             my $id = $session->store();
15              
16             my $restored = OpenFrame::WebApp::Session->fetch( $id );
17              
18             =cut
19              
20             package OpenFrame::WebApp::Session;
21              
22 4     4   22408 use strict;
  4         7  
  4         132  
23 4     4   19 use warnings::register;
  4         7  
  4         472  
24              
25 4     4   1966 use Error;
  4         12493  
  4         21  
26 4     4   201 use Digest::MD5 qw( md5_hex );
  4         7  
  4         231  
27 4     4   3897 use Time::ParseDate;
  4         59968  
  4         270  
28 4     4   1388 use OpenFrame::WebApp::Error::Abstract;
  4         9  
  4         41  
29              
30             our $VERSION = (split(/ /, '$Revision: 1.7 $'))[1];
31              
32 4     4   353 use base qw( OpenFrame::Object );
  4         7  
  4         3520  
33              
34             our $TYPES = {
35             file_cache => 'OpenFrame::WebApp::Session::FileCache',
36             mem_cache => 'OpenFrame::WebApp::Session::MemCache',
37             };
38              
39             sub types {
40             my $self = shift;
41             if (@_) {
42             $TYPES = shift;
43             return $self;
44             } else {
45             return $TYPES;
46             }
47             }
48              
49             sub init {
50             my $self = shift;
51             $self->id( $self->generate_id );
52             }
53              
54             sub id {
55             my $self = shift;
56             if (@_) {
57             $self->{session_id} = shift;
58             return $self;
59             } else {
60             return $self->{session_id};
61             }
62             }
63              
64             sub expiry {
65             my $self = shift;
66             if (@_) {
67             $self->{expiry_period} = shift;
68             return $self;
69             } else {
70             return $self->{expiry_period};
71             }
72             }
73              
74             sub get_expiry_seconds {
75             my $self = shift;
76              
77             # using NOW => 0 causes parsedate() to uses the current time
78             my ($time, $err) = parsedate($self->expiry, NOW => 1);
79              
80             if ($err) {
81             $self->error( "got [$err] parsing expiry time: " . $self->expiry );
82             return;
83             }
84              
85             return ($time-1);
86             }
87              
88             sub get {
89             my $self = shift;
90             my $key = shift;
91             return $self->{$key};
92             }
93              
94             sub set {
95             my $self = shift;
96             my $key = shift;
97             $self->{$key} = shift;
98             return $self;
99             }
100              
101             sub generate_id {
102             my $self = shift;
103             substr( md5_hex( time() . md5_hex(time(). {}. rand(). $$) ), 0, 32 );
104             }
105              
106             sub store {
107             my $self = shift;
108             throw OpenFrame::WebApp::Error::Abstract( class => ref($self) );
109             }
110              
111             sub fetch {
112             my $class = shift;
113             $class = ref($class) || $class;
114             throw OpenFrame::WebApp::Error::Abstract( class => $class );
115             }
116              
117             sub remove {
118             my $class = shift;
119             $class = ref($class) || $class;
120             throw OpenFrame::WebApp::Error::Abstract( class => $class );
121             }
122              
123              
124             1;
125              
126             =head1 DESCRIPTION
127              
128             The C class is an abstract wrapper around session
129             storing classes like C, C, C,
130             etc.
131              
132             In WebApp, sessions are a storable hash with a session id, and an expiry
133             period.
134              
135             Just incase something like Pixie is used to store the sessions, you should
136             always use the set/get methods to retrieve keys from the hash.
137              
138             This class was meant to be used with C.
139              
140             =head1 METHODS
141              
142             =over 4
143              
144             =item types
145              
146             set/get the hash of $session_types => $class_names known to this class.
147              
148             =item $session->id
149              
150             set/get the session id (stored as 'session_id', FYI).
151              
152             =item $session->expiry
153              
154             set/get the expiry period (stored as 'expiry_period', FYI). This should be
155             a string compatible with C.
156              
157             =item $time = $session->get_expiry_seconds
158              
159             parses the expiry period with C, and returns the result in
160             seconds.
161              
162             Note: C<$time == undef> implies no expiry time, whereas C<$time <= 0> implies
163             expires immediately.
164              
165             =item $session->set( $key, $val )
166              
167             associates $key with $val, and returns this object.
168              
169             =item $val = $session->get( $key )
170              
171             returns value associated with $key.
172              
173             =item $id = $session->store
174              
175             abstract method, saves the session to disk and returns the session id.
176              
177             =item $session = $class->fetch( $id )
178              
179             abstract method, returns the session with the given $id or undef if not found
180             or expired.
181              
182             =item $session->remove( [ $id ] )
183              
184             abstract method, removes this object from the store, and returns this object.
185             if called as a class method, $id is expected.
186              
187             =item $id = $session->generate_id
188              
189             internal method to generate a new session id.
190              
191             =back
192              
193             =head1 SUB-CLASSING
194              
195             Read through the source of this package and the known sub-classes first.
196             The minumum you need to do is this:
197              
198             use base qw( OpenFrame::WebApp::Session );
199              
200             OpenFrame::WebApp::Session->types->{my_type} = __PACKAGE__;
201              
202             sub store {
203             ...
204             return $id;
205             }
206              
207             sub fetch {
208             ...
209             return new Some::Session();
210             }
211              
212             sub remove {
213             ...
214             return $self;
215             }
216              
217             You must register your session type if you want to use the Session::Factory.
218              
219             =head1 AUTHOR
220              
221             Steve Purkis
222              
223             Based on C by James A. Duncan
224              
225             =head1 COPYRIGHT
226              
227             Copyright (c) 2003 Steve Purkis. All rights reserved.
228             Released under the same license as Perl itself.
229              
230             =head1 SEE ALSO
231              
232             L,
233             L,
234             L,
235             L
236              
237             Similar modules:
238              
239             L,
240             L,
241             L
242              
243             =cut