File Coverage

blib/lib/Lemonldap/NG/Common/Session.pm
Criterion Covered Total %
statement 36 71 50.7
branch 9 26 34.6
condition 5 20 25.0
subroutine 6 9 66.6
pod 1 4 25.0
total 57 130 43.8


line stmt bran cond sub pod time code
1             ##@file
2             # Base package for LemonLDAP::NG session object
3              
4             ##@class
5             # Specify a session object, how to create/update/remove session
6              
7             package Lemonldap::NG::Common::Session;
8              
9             our $VERSION = 1.4.1;
10              
11 1     1   24214 use Mouse;
  1         35327  
  1         5  
12 1     1   1320 use Lemonldap::NG::Common::Apache::Session;
  1         2  
  1         687  
13              
14             has 'id' => (
15             is => 'rw',
16             isa => 'Str|Undef',
17             );
18              
19             has 'force' => (
20             is => 'rw',
21             isa => 'Bool',
22             default => 0,
23             );
24              
25             has 'kind' => (
26             is => 'rw',
27             isa => 'Str|Undef',
28             );
29              
30             has 'data' => (
31             is => 'rw',
32             isa => 'HashRef',
33             );
34              
35             has 'options' => (
36             is => 'rw',
37             isa => 'HashRef',
38             );
39              
40             has 'storageModule' => (
41             is => 'ro',
42             isa => 'Str',
43             required => 1,
44             );
45              
46             has 'storageModuleOptions' => (
47             is => 'ro',
48             isa => 'HashRef|Undef',
49             );
50              
51             has 'cacheModule' => (
52             is => 'rw',
53             isa => 'Str|Undef',
54             );
55              
56             has 'cacheModuleOptions' => (
57             is => 'rw',
58             isa => 'HashRef|Undef',
59             );
60              
61             has 'error' => (
62             is => 'rw',
63             isa => 'Str|Undef',
64             );
65              
66             sub BUILD {
67 2     2 1 3353 my $self = shift;
68              
69             # Load Apache::Session module
70 2 100       43 unless ( $self->storageModule->can('populate') ) {
71 1         143 eval "require " . $self->storageModule;
72 1 50       16148 return undef if $@;
73             }
74              
75             # Register options for common Apache::Session module
76 2   50     22 my $moduleOptions = $self->storageModuleOptions || {};
77 2         37 my %options = (
78             %$moduleOptions,
79             backend => $self->storageModule,
80             localStorage => $self->cacheModule,
81             localStorageOptions => $self->cacheModuleOptions
82             );
83              
84 2         46 $self->options( \%options );
85              
86 2         9 my $data = $self->_tie_session;
87              
88             # Is it a session creation request?
89 2 50 0     15 my $creation = 1
      0        
      33        
90             if ( !$self->id or ( $self->id and !$data and $self->force ) );
91              
92             # If session id was submitted but session is not found
93             # And we want to force id
94             # Then use setId to create session
95 2 50 33     12 if ( $self->id and $creation ) {
96 0         0 $options{setId} = $self->id;
97 0         0 $self->options( \%options );
98 0         0 $self->id(undef);
99 0         0 $data = $self->_tie_session;
100             }
101              
102             # If session is created
103             # Then set session kind in session
104 2 50 33     16 if ( $creation and $self->kind ) {
105 2         25 $data->{_session_kind} = $self->kind;
106             }
107              
108             # Load session data into object
109 2 50       31 if ($data) {
110 2         8 $self->_save_data($data);
111 2         11 $self->kind( $data->{_session_kind} );
112 2         28 $self->id( $data->{_session_id} );
113              
114 2         35 untie(%$data);
115             }
116             }
117              
118             sub _tie_session {
119 2     2   5 my $self = shift;
120              
121 2         4 my %h;
122              
123 2         5 eval {
124             # SOAP session module must be directly tied
125 2 50       33 if ( $self->storageModule =~
126             /Lemonldap::NG::Common::Apache::Session::SOAP/ )
127             {
128 0         0 tie %h, $self->storageModule, $self->id, $self->options;
129             }
130             else {
131 2         32 tie %h, 'Lemonldap::NG::Common::Apache::Session', $self->id,
132             $self->options;
133             }
134             };
135              
136 2 50 33     1212 if ( $@ or not tied(%h) ) {
137 0         0 my $msg = "Session cannot be tied";
138 0 0       0 $msg .= ": $@" if $@;
139 0         0 $self->error($msg);
140 0         0 return undef;
141             }
142              
143 2         7 return \%h;
144             }
145              
146             sub _save_data {
147 2     2   4 my ( $self, $data ) = @_;
148              
149 2         16 my %saved_data = %$data;
150 2         103 $self->data( \%saved_data );
151             }
152              
153             sub update {
154 0     0 0   my $self = shift;
155 0           my $infos = shift;
156              
157 0 0         unless ( ref $infos eq "HASH" ) {
158 0           $self->error("You need to provide a HASHREF");
159 0           return 0;
160             }
161              
162 0           my $data = $self->_tie_session;
163              
164 0 0         if ($data) {
165 0           foreach ( keys %$infos ) {
166 0 0         if ( defined $infos->{$_} ) {
167 0           $data->{$_} = $infos->{$_};
168             }
169             else {
170 0           delete $data->{$_};
171             }
172             }
173              
174 0           $self->_save_data($data);
175              
176 0           untie(%$data);
177 0           return 1;
178             }
179              
180 0           $self->error("No data found in session");
181 0           return 0;
182             }
183              
184             sub remove {
185 0     0 0   my $self = shift;
186              
187 0           my $data = $self->_tie_session;
188              
189 0           eval { tied(%$data)->delete(); };
  0            
190              
191 0 0         if ($@) {
192 0           $self->error("Unable to delete session: $@");
193 0           return 0;
194             }
195              
196 0           return 1;
197             }
198              
199             sub cacheUpdate {
200 0     0 0   my $self = shift;
201              
202             # Update a data to force update from cache
203 0           return $self->update( { '_session_id' => $self->id } );
204             }
205              
206 1     1   5 no Mouse;
  1         1  
  1         8  
207              
208             1;