File Coverage

blib/lib/Maypole/Plugin/Session.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Maypole::Plugin::Session;
2              
3 1     1   24009 use 5.005;
  1         4  
  1         45  
4 1     1   6 use warnings;
  1         3  
  1         32  
5 1     1   6 use strict;
  1         7  
  1         41  
6              
7 1     1   535 use Maypole();
  0            
  0            
8             use Maypole::Config();
9             use Maypole::Constants();
10              
11             use Apache::Session::Wrapper 0.24;
12              
13             Maypole::Config->mk_accessors('session');
14             Maypole->mk_accessors( 'apache_session_wrapper' );
15              
16             =head1 NAME
17              
18             Maypole::Plugin::Session - simple sessions for Maypole
19              
20             =cut
21              
22             our $VERSION = 0.2;
23              
24             =head1 SYNOPSIS
25              
26             use Maypole::Application qw( Session );
27            
28             # Elsewhere in your app:
29             my $session = $r->session;
30            
31             =head1 API CHANGES
32              
33             This version is a re-write using L. As such, the configuration parameters
34             have changed - use L settings rather than L settings.
35             See B.
36            
37             =head1 DESCRIPTION
38              
39             Provides C and C methods for your Maypole request class. The session is
40             implemented using L, and as such, a range of session store mechanisms
41             are available.
42              
43             =head1 CONFIGURATION
44              
45             The B section of the L docs lists all the
46             available parameters. These should be placed in the C slot as a hashref.
47              
48             =over 4
49              
50             =item setup
51              
52             If there are no settings in C<< Maypole::Config->session >>, then default settings for L
53             are placed there. Also, cookies are turned on by default:
54              
55             $config->{session} = { class => 'File',
56             directory => "/tmp/sessions",
57             lock_directory => "/tmp/sessionlock",
58            
59             use_cookie => 1,
60             cookie_name => 'maypole-plugin-session-cookie',
61             };
62            
63             You need to create these directories with appropriate permissions if you
64             want to use these defaults.
65              
66             You can place custom settings there either before (preferably) or after (probably OK)
67             calling C<< Maypole->setup >>, e.g.
68              
69             $r->config->session( { class => "Flex",
70             store => 'DB_File',
71             lock => 'Null',
72             generate => 'MD5',
73             serialize => 'Storable'
74             } );
75              
76             =cut
77              
78             sub setup
79             {
80             my $r = shift; # class name
81            
82             warn "Running " . __PACKAGE__ . " setup for $r" if $r->debug;
83            
84             # Apache::Session::Wrapper will use add() to set the cookie under CGI
85             *Maypole::Headers::add = \&Maypole::Headers::push;
86            
87             my %defaults = ( class => 'File',
88             directory => "/tmp/sessions",
89             lock_directory => "/tmp/sessionlock",
90            
91             use_cookie => 1,
92             cookie_name => 'maypole-plugin-session-cookie',
93             );
94            
95             my $cfg = $r->config->session || {};
96            
97             if ( keys %$cfg )
98             {
99             exists $cfg->{use_cookie} or $cfg->{use_cookie} = $defaults{use_cookie};
100             exists $cfg->{cookie_name} or $cfg->{cookie_name} = $defaults{cookie_name};
101             }
102             else
103             {
104             %$cfg = %defaults;
105             }
106            
107             $r->NEXT::DISTINCT::setup( @_ );
108             }
109              
110             =back
111              
112             =head1 METHODS
113              
114             =over 4
115              
116             =item session
117              
118             Returns the session hashref.
119              
120             =item delete_session
121              
122             Deletes the session and cookie.
123              
124             =cut
125              
126             sub session { shift->apache_session_wrapper->session( @_ ) }
127             sub delete_session { shift->apache_session_wrapper->delete_session( @_ ) }
128              
129             =back
130              
131             =head1 PRIVATE METHODS
132              
133             These are only necessary if you are writing custom C method(s).
134             Otherwise, they are called for you.
135              
136             =over 4
137              
138             =item authenticate
139              
140             This is called early in the Maypole request workflow, and is used as the hook to
141             call C. If you are writing your own C method(s), either in
142             model classes or in the request classes, make sure your C method calls
143             C.
144            
145             =cut
146              
147             sub authenticate
148             {
149             my ( $r ) = @_;
150            
151             $r->get_session;
152            
153             return Maypole::Constants::OK;
154             }
155              
156             =item get_session
157              
158             Retrieves the cookie from the browser and matches it up with a session in the store.
159              
160             You should call this method inside any custom C methods.
161              
162             =cut
163              
164             sub get_session
165             {
166             my ( $r ) = @_;
167            
168             # returning 1 silences an anonymous warning
169             $r->can( 'ar' ) && $r->ar && $r->ar->register_cleanup( sub { $r->apache_session_wrapper->cleanup_session; 1 } );
170            
171             $r->{apache_session_wrapper} =
172             Apache::Session::Wrapper->new( header_object => $r,
173             param_object => $r,
174             %{ $r->config->session },
175             );
176             }
177              
178             =back
179              
180             =head1 SEE ALSO
181              
182             L.
183              
184             =head1 AUTHOR
185              
186             David Baird, C<< >>
187              
188             =head1 BUGS
189              
190             Please report any bugs or feature requests to
191             C, or through the web interface at
192             L.
193             I will be notified, and then you'll automatically be notified of progress on
194             your bug as I make changes.
195              
196             =head1 COPYRIGHT & LICENSE
197              
198             Copyright 2005 David Baird, All Rights Reserved.
199              
200             This program is free software; you can redistribute it and/or modify it
201             under the same terms as Perl itself.
202              
203             =cut
204              
205             1; # End of Maypole::Plugin::Session