Jifty/Web/Session/JDBI.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 154 CC 13 SCAR 19.4
CC
13
Cov
0%
load
12.0
set
12.0
remove
12.0
CRAP 6.9
1 use warnings;
2 use strict;
3  
4 package Jifty::Web::Session::JDBI;
5 use base qw/Jifty::Web::Session/;
6 use DateTime ();
7 use Storable ();
8 $Storable::Deparse = 1;
9 $Storable::Eval = 1;
10 $Storable::forgive_me = 1;
11  
12 =head1 NAME
13  
14 Jifty::Web::Session - A Jifty session handler
15  
16 =head1 SYNOPSIS
17  
18 In your F<etc/config.yml> (optional):
19  
20 framework:
21 Web:
22 # The default ($PORT is replaced by the port the app is running on)
23 SessionCookieName: JIFTY_SID_$PORT
24  
25 =head2 new
26  
27 Returns a new, empty session.
28  
29 =cut
30  
31 sub new {
32 my $class = shift;
33 return bless { _cookie_name => Jifty->config->framework('Web')->{'SessionCookieName'} }, $class;
34 }
35  
36 =head2 id
37  
38 Returns the session's id if it has been loaded, or C<undef> otherwise.
39  
40 =cut
41  
42 sub id {
43 my $self = shift;
44 return $self->loaded ? $self->_session->session_id : undef;
45 }
46  
47 =head2 create
48  
49 Creates a new row in the L<Jifty::Model::Session> table.
50  
51 =cut
52  
53 sub create {
54 my $self = shift;
55 my $session = Jifty::Model::Session->new;
56 $session->create( key_type => "session" );
57 $self->_session($session);
58 }
59  
60 =head2 load [ID]
61  
62 Load up the current session from the given C<ID>, or the appropriate
63 cookie (see L</cookie_name>) otherwise. If both of those fail,
64 creates a session in the database.
65  
66 =cut
67  
68 sub load {
69 my $self = shift;
70 my $session_id = shift;
71  
72 $session_id ||= $self->_get_session_id_from_client();
73  
74 my $session = Jifty::Model::Session->new;
75 $session->load_by_cols(
76 session_id => $session_id,
77 key_type => "session"
78 ) if $session_id;
79  
80 $session->create( key_type => "session" ) unless $session->id;
81 $self->_session($session);
82 }
83  
84 =head2 load_by_kv key => value
85  
86 Load up the current session from the given (key, value) pair. If no matching
87 session could be found, it will create a new session with the key, value set.
88 Be sure that what you're loading by is unique. If you're loading a session
89 based on, say, a timestamp, then you're asking for trouble.
90  
91 =cut
92  
93 sub load_by_kv {
94 my $self = shift;
95 my $k = shift;
96 my $v = shift;
97  
98 # XXX TODO: we store this data in a storable. so we now want to match on the storable version
99 # It would be so nice if Jifty::DBI could do this for us.
100 my $encoded = Storable::nfreeze( \$v );
101  
102 my $session = Jifty::Model::Session->new;
103 $session->load_by_cols(
104 key_type => 'key',
105 data_key => $k,
106 value => $encoded,
107 );
108 my $session_id = $session->session_id;
109  
110 # XXX: if $session_id is undef, then bad things happen. This *can* happen.
111  
112 $self->load($session_id);
113 $self->set( $k => $v ) if !$session_id;
114 }
115  
116  
117 sub _get_session_id_from_client {
118 my $self = shift;
119 my $cookies = Jifty->web->request
120 ? Jifty->web->request->cookies : {};
121 return $cookies->{$self->cookie_name};
122 }
123  
124 =head2 get KEY [TYPE]
125  
126 Returns the value for C<KEY> for the current user's session. C<TYPE>,
127 which defaults to "key", allows accessing of other namespaces in the
128 session, including "metadata" and "continuation".
129  
130 =cut
131  
132 sub get {
133 my $self = shift;
134 my $key = shift;
135 my $key_type = shift || "key";
136  
137 return undef unless $self->loaded;
138  
139 my $setting = Jifty::Model::Session->new;
140 $setting->load_by_cols(
141 session_id => $self->id,
142 key_type => $key_type,
143 data_key => $key
144 );
145 return $setting->value;
146  
147 }
148  
149 =head2 set KEY => VALUE, [TYPE]
150  
151 Sets the value C<VALUE> for C<KEY> for the session. C<TYPE>, which
152 defaults to "key", allows values to be set in other namespaces,
153 including "metadata" and "continuation". C<VALUE> can be an arbitrary
154 perl data structure -- C<Jifty::Web::Session> will serialize it for
155 you.
156  
157 =cut
158  
159 sub set {
160 my $self = shift;
161 my $key = shift;
162 my $value = shift;
163 my $key_type = shift || "key";
164  
165 return undef unless $self->loaded;
166 $self->_session->set_updated( DateTime->now );
167  
168 my $setting = Jifty::Model::Session->new;
169 $setting->load_by_cols(
170 session_id => $self->id,
171 key_type => $key_type,
172 data_key => $key
173 );
174 if ( $setting->id ) {
175 $setting->set_value($value);
176 } else {
177 $setting->create(
178 session_id => $self->id,
179 key_type => $key_type,
180 data_key => $key,
181 value => $value
182 );
183 }
184  
185 }
186  
187 =head2 remove KEY, [TYPE]
188  
189 Remove key C<KEY> from the cache. C<TYPE> defaults to "key".
190  
191 =cut
192  
193 sub remove {
194 my $self = shift;
195 my $key = shift;
196 my $key_type = shift || "key";
197  
198 return undef unless $self->loaded;
199 $self->_session->set_updated( DateTime->now );
200  
201 my $setting = Jifty::Model::Session->new;
202 $setting->load_by_cols(
203 session_id => $self->id,
204 key_type => $key_type,
205 data_key => $key
206 );
207 $setting->delete if $setting->id;
208 }
209  
210 =head2 remove_all
211  
212 Removes the session from the database entirely.
213  
214 =cut
215  
216 sub remove_all {
217 my $self = shift;
218 return unless $self->loaded;
219 my $settings = Jifty::Model::SessionCollection->new;
220 $settings->limit( column => "session_id", value => $self->id );
221 while (my $setting = $settings->next) {
222 $setting->delete;
223 }
224  
225 $self->unload;
226 }
227  
228 =head2 continuations
229  
230 Return a hash of all the continuations in this session, keyed by the
231 continuations' C<id>.
232  
233 =cut
234  
235 sub continuations {
236 my $self = shift;
237  
238 return () unless $self->loaded;
239  
240 my $conts = Jifty::Model::SessionCollection->new;
241 $conts->limit( column => "key_type", value => "continuation" );
242 $conts->limit( column => "session_id", value => $self->id );
243  
244 my %continuations;
245 while (my $cont = $conts->next) {
246 $continuations{ $cont->data_key } = $cont->value;
247 }
248 return %continuations;
249 }
250  
251 1;