line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Plugin::Session::State::Stash; # git description: 0.12-14-g745a242 |
2
|
|
|
|
|
|
|
# ABSTRACT: Maintain session IDs using the stash |
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
1664167
|
use Moose; |
|
3
|
|
|
|
|
617223
|
|
|
3
|
|
|
|
|
20
|
|
5
|
3
|
|
|
3
|
|
16236
|
use 5.008; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
93
|
|
6
|
3
|
|
|
3
|
|
14
|
use MRO::Compat; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
56
|
|
7
|
3
|
|
|
3
|
|
1292
|
use namespace::autoclean; |
|
3
|
|
|
|
|
12656
|
|
|
3
|
|
|
|
|
20
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
extends 'Catalyst::Plugin::Session::State'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has _deleted_session_id => ( is => 'rw' ); |
14
|
|
|
|
|
|
|
has _prepared => ( is => 'rw' ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub _stash_key_components { |
17
|
17
|
|
|
17
|
|
19
|
my ($c) = @_; |
18
|
17
|
|
|
|
|
44
|
my $config = $c->_session_plugin_config; |
19
|
17
|
50
|
|
|
|
858
|
return ($config->{stash_delim}) ? |
20
|
|
|
|
|
|
|
split $config->{stash_delim}, $config->{stash_key} : |
21
|
|
|
|
|
|
|
$config->{stash_key}; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _get_session { |
25
|
17
|
|
|
17
|
|
22
|
my ($c) = @_; |
26
|
|
|
|
|
|
|
# This turns the list of path components into a nested tree of hashrefs for obtaining info/storing in: 123/456 = {123}->{456} |
27
|
17
|
|
|
|
|
46
|
my $ref = $c->stash; |
28
|
17
|
|
100
|
|
|
767
|
$ref = ($ref->{$_} ||= {}) foreach $c->_stash_key_components; |
29
|
17
|
|
|
|
|
58
|
$ref; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _set_session { |
33
|
1
|
|
|
1
|
|
2
|
my ( $c,$key,$value) = @_; |
34
|
1
|
|
|
|
|
5
|
$c->_get_session->{$key} = $value; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub setup_session { |
38
|
3
|
|
|
3
|
1
|
518382
|
my $c = shift; |
39
|
|
|
|
|
|
|
|
40
|
3
|
|
|
|
|
14
|
$c->maybe::next::method(@_); |
41
|
|
|
|
|
|
|
|
42
|
3
|
|
50
|
|
|
126
|
$c->_session_plugin_config->{stash_key} ||= '_session'; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub prepare_action { |
46
|
5
|
|
|
5
|
1
|
135912
|
my $c = shift; |
47
|
5
|
|
|
|
|
22
|
my $id = $c->get_session_id; |
48
|
5
|
|
|
|
|
230
|
$c->_prepared(1); |
49
|
5
|
50
|
|
|
|
9
|
if ( $id ) { |
50
|
0
|
|
|
|
|
0
|
$c->sessionid( $id ); |
51
|
|
|
|
|
|
|
} |
52
|
5
|
|
|
|
|
15
|
$c->maybe::next::method( @_ ); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub get_session_id { |
56
|
17
|
|
|
17
|
1
|
30653
|
my $c = shift; |
57
|
17
|
100
|
66
|
|
|
700
|
if(!$c->_deleted_session_id and my $session = $c->_get_session) { |
58
|
15
|
|
|
|
|
18
|
my $sid = $session->{id}; |
59
|
15
|
100
|
|
|
|
50
|
return $sid if $sid; |
60
|
|
|
|
|
|
|
} |
61
|
9
|
|
|
|
|
32
|
$c->maybe::next::method(@_); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub set_session_id { |
65
|
1
|
|
|
1
|
1
|
5015
|
my ( $c, $sid ) = @_; |
66
|
1
|
|
|
|
|
9
|
$c->_set_session(id => $sid); |
67
|
1
|
|
|
|
|
4
|
$c->maybe::next::method($sid); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub get_session_expires { |
71
|
0
|
|
|
0
|
1
|
0
|
my $c = shift; |
72
|
0
|
|
|
|
|
0
|
my $session = $c->_get_session; |
73
|
0
|
0
|
|
|
|
0
|
defined $session->{expires} ? $session->{expires} : undef; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub set_session_expires { |
77
|
0
|
|
|
0
|
1
|
0
|
my ( $c, $expires ) = @_; |
78
|
0
|
|
|
|
|
0
|
$c->_set_session(expires => time() + $expires); |
79
|
0
|
|
|
|
|
0
|
$c->maybe::next::method($expires) |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub delete_session_id { |
83
|
1
|
|
|
1
|
1
|
281
|
my ($c, $sid ) = @_; |
84
|
1
|
|
|
|
|
42
|
$c->_deleted_session_id(1); |
85
|
|
|
|
|
|
|
#Empty the tip |
86
|
1
|
|
|
|
|
1
|
%{$c->_get_session} = (); |
|
1
|
|
|
|
|
3
|
|
87
|
1
|
|
|
|
|
4
|
$c->maybe::next::method($sid); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=pod |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=encoding UTF-8 |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 NAME |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Catalyst::Plugin::Session::State::Stash - Maintain session IDs using the stash |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 VERSION |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
version 0.14 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 SYNOPSIS |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
use Catalyst qw/Session Session::State::Stash Session::Store::Foo/; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 DESCRIPTION |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
An alternative state storage plugin that allows you some more flexibility in |
114
|
|
|
|
|
|
|
dealing with session storage. This plugin loads and saves the session ID from |
115
|
|
|
|
|
|
|
and to the stash. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 METHODS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=over 4 |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item delete_session_id |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Deletes the session. Unfortunately I've been unable to squash a bug that will |
124
|
|
|
|
|
|
|
stop you from opening a new session in the same execution, however. |
125
|
|
|
|
|
|
|
Patches welcome! |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item get_session_id |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Gets the current session id. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item set_session_id |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Sets the session id to the C<shift>. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item get_session_expires |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Gets when the current session expires. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=item set_session_expires |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Sets how many seconds from now the session should expire. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=back |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 EXTENDED METHODS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=over 4 |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item prepare_action |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Loads the id off the stash. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item setup_session |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Defaults the C<stash_key> parameter to C<_session>. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=back |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 CONFIGURATION |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=over 4 |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item stash_key |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
The name of the hash key to use. Defaults to C<_session>. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item stash_delim |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
If present, splits C<stash_key> at this character to nest. E.g. a C<delim> of '/' |
170
|
|
|
|
|
|
|
and C<stash_key> of '123/456' will store it as $c->stash->{123}->{456} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item expires |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
How long the session should last in seconds. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=back |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
For example, you could stick this in F<MyApp.pm>: |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
__PACKAGE__->config( 'Plugin::Session' => { |
181
|
|
|
|
|
|
|
stash_key => 'session_id', |
182
|
|
|
|
|
|
|
}); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 BUGS |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
You can't delete a session then create a new one. If this is important to you, |
187
|
|
|
|
|
|
|
patches welcome! |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 CAVEATS |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Manual work may be involved to make better use of this. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=for stopwords stateful |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
If you are writing a stateful web service with |
196
|
|
|
|
|
|
|
L<Catalyst::Plugin::Server::XMLRPC>, you will probably only have to deal with |
197
|
|
|
|
|
|
|
loading, as when saving, the ID will already be on the stash. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 SEE ALSO |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
L<Catalyst>, L<Catalyst::Plugin::Session>, L<Catalyst::Plugin::Session::State>, |
202
|
|
|
|
|
|
|
L<Catalyst::Plugin::Session::State::Cookie> (what you probably want). |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 AUTHOR |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
James Laver <perl -e 'printf qw/%s@%s.com cpan jameslaver/'> |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=over 4 |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=item * |
213
|
|
|
|
|
|
|
This module is derived from L<Catalyst::Plugin::Session::State::Cookie> code. |
214
|
|
|
|
|
|
|
Thanks to anyone who wrote code for that. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item * |
217
|
|
|
|
|
|
|
Thanks to Kent Fredric for a patch for nested keys |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=back |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=for stopwords Florian Ragwitz Karen Etheridge Tomas Doran James Laver |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=over 4 |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item * |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Florian Ragwitz <rafl@debian.org> |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item * |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Karen Etheridge <ether@cpan.org> |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item * |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Tomas Doran <bobtfish@bobtfish.net> |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=item * |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
James Laver <jjl@baozi.local> |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=item * |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
James Laver <james@jameslaver.com> |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=back |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
This software is copyright (c) 2009 by James Laver. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
252
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=cut |