| 1 | 1 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||
| 4 | package Jifty::Web::Session; | ||||||||||||||||||||||||
| 5 | 1 | ▶ | use base qw/Jifty::Object/; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 1 | ▶ | use DateTime (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||
| 10 | Jifty::Web::Session - A Jifty session handler | ||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||
| 14 | In your F<etc/config.yml> (optional): | ||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||
| 16 | framework: | ||||||||||||||||||||||||
| 17 | Web: | ||||||||||||||||||||||||
| 18 | # The default ($PORT is replaced by the port the app is running on) | ||||||||||||||||||||||||
| 19 | SessionCookieName: JIFTY_SID_$PORT | ||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||
| 21 | =head2 new | ||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||
| 23 | Returns a new, empty session. | ||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||
| 27 | 0 | ▶ | sub new { | ||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 28 | 0 | my $class = shift; | |||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||
| 30 | 0 | ▶ | my $session_class = Jifty->config->framework('Web')->{'SessionClass'} || 'Jifty::Web::Session::JDBI'; | ||||||||||||||||||||||
|
Condition0%0/2
'Jifty'->config->framework('Web')->{'SessionClass'} || 'Jifty::Web::Session::JDBI'
MC/DC0%0/1
'Jifty'->config->framework('Web')->{'SessionClass'} || 'Jifty::Web::Session::JDBI'
'Jifty'->config->framework('Web')->{'SessionClass'}
Truth table0%0/2
A
A= 'Jifty'->config->framework('Web')->{'SessionClass'}
| |||||||||||||||||||||||||
| 31 | 0 | my $cookie_name = Jifty->config->framework('Web')->{'SessionCookieName'}; | |||||||||||||||||||||||
| 32 | 0 | Jifty::Util->require($session_class); | |||||||||||||||||||||||
| 33 | 0 | return $session_class->new(@_); | |||||||||||||||||||||||
| 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 | 0 | ▶ | sub id { | ||||||||||||||||||||||
|
Subroutine
id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 43 | 0 | die "Subclass must implement 'id'"; | |||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||
| 46 | =head2 create | ||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||
| 48 | Assign a new ID, and store it server-side if necessary. | ||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | 0 | ▶ | sub create { | ||||||||||||||||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 53 | 0 | die "Subclass must implement 'create'"; | |||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||
| 56 | =head2 load [ID] | ||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||
| 58 | Load up the current session from the given C<ID>, or the appropriate | ||||||||||||||||||||||||
| 59 | cookie (see L</cookie_name>) otherwise. If both of those fail, | ||||||||||||||||||||||||
| 60 | creates a session in the database. | ||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||
| 64 | 0 | ▶ | sub load { | ||||||||||||||||||||||
|
Subroutine
load: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 65 | 0 | die "Subclass must implement 'load'"; | |||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||
| 68 | =head2 load_by_kv key => value | ||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | Load up the current session from the given (key, value) pair. If no matching | ||||||||||||||||||||||||
| 71 | session could be found, it will create a new session with the key, value set. | ||||||||||||||||||||||||
| 72 | Be sure that what you're loading by is unique. If you're loading a session | ||||||||||||||||||||||||
| 73 | based on, say, a timestamp, then you're asking for trouble. | ||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||
| 77 | 0 | ▶ | sub load_by_kv { | ||||||||||||||||||||||
|
Subroutine
load_by_kv: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 78 | 0 | die "Subclass must implement load_by_kv"; | |||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | =head2 unload | ||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||
| 83 | Flush the session, and leaves the session object blank. | ||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||
| 87 | 0 | ▶ | sub unload { | ||||||||||||||||||||||
|
Subroutine
unload: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 88 | 0 | my $self = shift; | |||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | 0 | ▶ | return unless $self->loaded; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 91 | 0 | $self->_session(undef); | |||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||
| 94 | =head2 loaded | ||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||
| 96 | Returns true if the session has already been loaded. | ||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||
| 100 | 0 | ▶ | sub loaded { | ||||||||||||||||||||||
|
Subroutine
loaded: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 101 | 0 | my $self = shift; | |||||||||||||||||||||||
| 102 | 0 | return $self->_session; | |||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||
| 105 | 0 | ▶ | sub _session { | ||||||||||||||||||||||
|
Subroutine
_session: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 106 | 0 | my $self = shift; | |||||||||||||||||||||||
| 107 | 0 | ▶ | $self->{'_session'} = shift if (@_); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 108 | 0 | return ( $self->{'_session'} ); | |||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||
| 111 | =head2 get KEY [TYPE] | ||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||
| 113 | Returns the value for C<KEY> for the current user's session. C<TYPE>, | ||||||||||||||||||||||||
| 114 | which defaults to "key", allows accessing of other namespaces in the | ||||||||||||||||||||||||
| 115 | session, including "metadata" and "continuation". | ||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||
| 119 | 0 | ▶ | sub get { | ||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||
| 121 | 0 | die "subclass must implement 'get'" | |||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||
| 124 | =head2 set KEY => VALUE, [TYPE] | ||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||
| 126 | Sets the value C<VALUE> for C<KEY> for the session. C<TYPE>, which | ||||||||||||||||||||||||
| 127 | defaults to "key", allows values to be set in other namespaces, | ||||||||||||||||||||||||
| 128 | including "metadata" and "continuation". C<VALUE> can be an arbitrary | ||||||||||||||||||||||||
| 129 | perl data structure -- C<Jifty::Web::Session> will serialize it for | ||||||||||||||||||||||||
| 130 | you. | ||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||
| 134 | 0 | ▶ | sub set { | ||||||||||||||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 135 | 0 | die "subclass must implement 'set'" | |||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||
| 138 | =head2 remove KEY, [TYPE] | ||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||
| 140 | Remove key C<KEY> from the cache. C<TYPE> defaults to "key". | ||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||
| 144 | 0 | ▶ | sub remove { | ||||||||||||||||||||||
|
Subroutine
remove: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 145 | 0 | die "subclass must implement 'remove'" | |||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||
| 148 | =head2 remove_all | ||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||
| 150 | Removes the session from the database entirely. | ||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||
| 154 | 0 | ▶ | sub remove_all { | ||||||||||||||||||||||
|
Subroutine
remove_all: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 155 | 0 | die "Subclass must implement 'remove_all'" | |||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||
| 158 | =head2 set_continuation ID CONT | ||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||
| 160 | Stores a continuation in the session. | ||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||
| 164 | 0 | ▶ | sub set_continuation { | ||||||||||||||||||||||
|
Subroutine
set_continuation: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 165 | 0 | my $self = shift; | |||||||||||||||||||||||
| 166 | 0 | $self->set( @_, "continuation" ); | |||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||
| 169 | =head2 get_continuation ID | ||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||
| 171 | Pulls a continuation from the current session. Expects a continuation | ||||||||||||||||||||||||
| 172 | C<ID>. | ||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||
| 176 | 0 | ▶ | sub get_continuation { | ||||||||||||||||||||||
|
Subroutine
get_continuation: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 177 | 0 | my $self = shift; | |||||||||||||||||||||||
| 178 | 0 | $self->get( @_, "continuation" ); | |||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||
| 182 | =head2 remove_continuation ID | ||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||
| 184 | Removes a continuation with id C<ID> from the store. | ||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||
| 188 | 0 | ▶ | sub remove_continuation { | ||||||||||||||||||||||
|
Subroutine
remove_continuation: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 189 | 0 | my $self = shift; | |||||||||||||||||||||||
| 190 | 0 | $self->remove( @_, "continuation" ); | |||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||
| 193 | =head2 continuations | ||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||
| 195 | Return a hash of all the continuations in this session, keyed by the | ||||||||||||||||||||||||
| 196 | continuations' C<id>. | ||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||
| 200 | 0 | ▶ | sub continuations { | ||||||||||||||||||||||
|
Subroutine
continuations: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 201 | 0 | die "Subclass must implement 'continuations'"; | |||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||
| 204 | =head2 set_cookie | ||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||
| 206 | Sets the session cookie. | ||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||
| 210 | 0 | ▶ | sub set_cookie { | ||||||||||||||||||||||
|
Subroutine
set_cookie: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||
| 211 | 0 | my $self = shift; | |||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||
| 213 | # never send a cookie with cached content. misbehaving proxies cause | ||||||||||||||||||||||||
| 214 | # terrific problems | ||||||||||||||||||||||||
| 215 | 0 | ▶ | return if Jifty->web->response->header('Expires'); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||
| 217 | 0 | ▶ | $self->load unless $self->loaded; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||
| 219 | 0 | my $cookie_name = $self->cookie_name; | |||||||||||||||||||||||
| 220 | 0 | ▶ | my $cookies = Jifty->web->request ? Jifty->web->request->cookies : {}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 221 | 0 | my $cookie = { | |||||||||||||||||||||||
| 222 | value => $self->id, | ||||||||||||||||||||||||
| 223 | path => '/', | ||||||||||||||||||||||||
| 224 | expires => $self->expires, | ||||||||||||||||||||||||
| 225 | }; | ||||||||||||||||||||||||
| 226 | # XXX: do we every need to check the existing cookie to decide if | ||||||||||||||||||||||||
| 227 | # we want to set the cookie this time? | ||||||||||||||||||||||||
| 228 | 0 | Jifty->web->response->cookies->{$cookie_name} = $cookie; | |||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||
| 231 | =head2 cookie_name | ||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||
| 233 | Returns the current session's cookie_name -- it is the same for all | ||||||||||||||||||||||||
| 234 | users, but varies according to the port the server is running on. | ||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||
| 238 | 0 | ▶ | sub cookie_name { | ||||||||||||||||||||||
|
Subroutine
cookie_name: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||
| 239 | 0 | my $self = shift; | |||||||||||||||||||||||
| 240 | 0 | my $cookie_name = $self->{'_cookie_name'}; | |||||||||||||||||||||||
| 241 | 0 | ▶ | my $port = ( ( Jifty->web->request && Jifty->web->request->port) || 'NOPORT' ); | ||||||||||||||||||||||
|
Condition0%0/5
'Jifty'->web->request && 'Jifty'->web->request->port || 'NOPORT'
"Jifty"->web->request and "Jifty"->web->request->port
MC/DC0%0/2
'Jifty'->web->request && 'Jifty'->web->request->port || 'NOPORT'
"Jifty"->web->request
"Jifty"->web->request->port
Truth table0%0/3
A and B
A= "Jifty"->web->requestB= "Jifty"->web->request->port
| |||||||||||||||||||||||||
| 242 | 0 | $cookie_name =~ s/\$PORT/$port/g; | |||||||||||||||||||||||
| 243 | 0 | return ($cookie_name); | |||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||
| 246 | =head2 expires [VALUE] | ||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||
| 248 | Get or set the session's expiration date, in a format expected by | ||||||||||||||||||||||||
| 249 | Cache::Cache. | ||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||
| 251 | =cut | ||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||
| 253 | 0 | ▶ | sub expires { | ||||||||||||||||||||||
|
Subroutine
expires: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 254 | 0 | my $self = shift; | |||||||||||||||||||||||
| 255 | 0 | ▶ | $self->set( 'expires' => shift, "metadata" ) if @_; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 256 | 0 | return ( $self->get( 'expires', "metadata" ) ); | |||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||
| 259 | 1 | 1; | |||||||||||||||||||||||