| 1 | package Jifty::Web::Session::ApacheSession; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Jifty::Web::Session::ApacheSession - Jifty Sessions based on Apache::Session | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | In your F<etc/config.yml>, using the L<Apache::Session::File> backend: | ||
| 10 | |||
| 11 | framework: | ||
| 12 | Web: | ||
| 13 | SessionClass: Jifty::Web::Session::ApacheSession | ||
| 14 | SessionBackend: File | ||
| 15 | SessionOptions: | ||
| 16 | Directory: /tmp/sessions | ||
| 17 | LockDirectory: /var/lock/sessions | ||
| 18 | |||
| 19 | Or with L<Apache::Session::Memorycached> backend: | ||
| 20 | |||
| 21 | framework: | ||
| 22 | Web: | ||
| 23 | SessionClass: Jifty::Web::Session::ApacheSession | ||
| 24 | SessionBackend: Memorycached | ||
| 25 | SessionOptions: { servers: [ '127.0.0.1:11211' ] } | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | use warnings; | ||
| 31 | use Jifty::Model::Session (); | ||
| 32 | use base 'Jifty::Web::Session'; | ||
| 33 | |||
| 34 | =head2 new | ||
| 35 | |||
| 36 | Returns a new, empty session handler, subclassing L<Jifty::Web::Session>. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my $class = shift; | ||
| 42 | my $cookie_name = Jifty->config->framework('Web')->{'SessionCookieName'}; | ||
| 43 | my $backend_class = Jifty->config->framework('Web')->{'SessionBackend'} | ||
| 44 | or die "Please set SessionBackend in your framework/Web settings"; | ||
| 45 | $backend_class = "Apache::Session::$backend_class" unless $backend_class =~ /::/; | ||
| 46 | Jifty::Util->require($backend_class); | ||
| 47 | |||
| 48 | return bless { _cookie_name => $cookie_name, _backend_class => $backend_class }, $class; | ||
| 49 | } | ||
| 50 | |||
| 51 | =head2 id | ||
| 52 | |||
| 53 | Returns the session's id if it has been loaded, or C<undef> otherwise. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub id { | ||
| 58 | my $self = shift; | ||
| 59 | return $self->loaded ? $self->_session->{_session_id} : undef; | ||
| 60 | } | ||
| 61 | |||
| 62 | =head2 create | ||
| 63 | |||
| 64 | Creates a new session. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub create { | ||
| 69 | my $self = shift; | ||
| 70 | my %session; | ||
| 71 | my $options = Jifty->config->framework('Web')->{'SessionOptions'}; | ||
| 72 | tie %session => $self->{_backend_class}, undef, $options; | ||
| 73 | $self->{_session} = \%session; | ||
| 74 | } | ||
| 75 | |||
| 76 | =head2 load [ID] | ||
| 77 | |||
| 78 | Load up the current session from the given C<ID>, or the appropriate | ||
| 79 | cookie (see L<Jifty::Web::Session/cookie_name>) otherwise. | ||
| 80 | |||
| 81 | If both of those fail, creates a session in memory. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub load { | ||
| 86 | my $self = shift; | ||
| 87 | my $session_id = shift; | ||
| 88 | |||
| 89 | unless ($session_id) { | ||
| 90 | my $cookie_name = $self->cookie_name; | ||
| 91 | $session_id = Jifty->web->request->cookies->{$cookie_name} | ||
| 92 | || Jifty::Model::Session->new_session_id, | ||
| 93 | } | ||
| 94 | |||
| 95 | my $options = Jifty->config->framework('Web')->{'SessionOptions'}; | ||
| 96 | |||
| 97 | my %session; | ||
| 98 | local $@; | ||
| 99 | eval { | ||
| 100 | tie %session => $self->{_backend_class}, $session_id, $options; | ||
| 101 | 1; | ||
| 102 | } or do { | ||
| 103 | tie %session => $self->{_backend_class}, undef, $options; | ||
| 104 | }; | ||
| 105 | |||
| 106 | $self->{_session} = \%session; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 get KEY [TYPE] | ||
| 110 | |||
| 111 | See L<Jifty::Web::Session/get>. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub get { | ||
| 116 | my $self = shift; | ||
| 117 | my $key = shift; | ||
| 118 | my $key_type = shift || "key"; | ||
| 119 | |||
| 120 | return undef unless $self->loaded; | ||
| 121 | return $self->_session->{$key_type,$key}; | ||
| 122 | } | ||
| 123 | |||
| 124 | =head2 set KEY => VALUE, [TYPE] | ||
| 125 | |||
| 126 | See L<Jifty::Web::Session/set>. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub set { | ||
| 131 | my $self = shift; | ||
| 132 | my $key = shift; | ||
| 133 | my $value = shift; | ||
| 134 | my $key_type = shift || "key"; | ||
| 135 | |||
| 136 | return undef unless $self->loaded; | ||
| 137 | $self->_session->{$key_type,$key} = $value; | ||
| 138 | } | ||
| 139 | |||
| 140 | =head2 remove KEY, [TYPE] | ||
| 141 | |||
| 142 | See L<Jifty::Web::Session/remove>. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub remove { | ||
| 147 | my $self = shift; | ||
| 148 | my $key = shift; | ||
| 149 | my $key_type = shift || "key"; | ||
| 150 | |||
| 151 | return undef unless $self->loaded; | ||
| 152 | delete $self->_session->{$key_type,$key}; | ||
| 153 | } | ||
| 154 | |||
| 155 | =head2 remove_all | ||
| 156 | |||
| 157 | See L<Jifty::Web::Session/remove_all>. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub remove_all { | ||
| 162 | my $self = shift; | ||
| 163 | |||
| 164 | return undef unless $self->loaded; | ||
| 165 | undef %{$self->_session}; | ||
| 166 | } | ||
| 167 | |||
| 168 | =head2 continuations | ||
| 169 | |||
| 170 | See L<Jifty::Web::Session/continuations>. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub continuations { | ||
| 175 | my $self = shift; | ||
| 176 | return () unless $self->loaded; | ||
| 177 | |||
| 178 | my $session = $self->_session; | ||
| 179 | my %continuations; | ||
| 180 | foreach my $key (keys %$session) { | ||
| 181 | if ($key =~ /^continuation$;(.*)/os) { | ||
| 182 | $continuations{$1} = $session->{$key}; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | return %continuations; | ||
| 186 | } | ||
| 187 | |||
| 188 | 1; |