Labyrinth/Session.pm

filter: Statementstmt 14.6% 44 / 301 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 76 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 32.3% 11 / 34 Podpod 100% 18 / 18 totaltotal 11.7% 73 / 624 CC 114 SCAR 50.7
CC
114
Cov
8%
_get_session
600.0
_save_session
240.0
Retrieve
132.0
CRAP 159.6
« Labyrinth/Media.pm ↑ Summary Labyrinth/Metadata.pm »
1 package Labyrinth::Session;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 $VERSION = '5.32';
8  
9 =head1 NAME
10  
11 Labyrinth::Session - Session Management for Labyrinth.
12  
13 =head1 SYNOPSIS
14  
15 use Labyrinth::Session;
16 Login($username,$password);
17 my $logged_in = 1 if(my $user = ValidSession());
18  
19 =head1 DESCRIPTION
20  
21 Provides the session management functionality, including Login & Logout
22 functions, to maintain a user's access to the system.
23  
24 =cut
25  
26 # -------------------------------------
27 # Export Details
28  
29 12 require Exporter;
30 12 @ISA = qw(Exporter);
31 12 %EXPORT_TAGS = (
32 'all' => [ qw(
33 ValidSession VerifyUser Authorised UserAccess FolderAccess
34 ResetLanguage UpdateSession
35 ) ]
36 );
37  
38 12 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
39 12 @EXPORT = ( @{ $EXPORT_TAGS{'all'} } );
40  
41 # -------------------------------------
42 # Library Modules
43  
44 12 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 12 ▶use Labyrinth::Globals;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 12 ▶use Labyrinth::DTUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 12 ▶use Labyrinth::CookieLib;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 12 ▶use Labyrinth::Mailer;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 12 ▶use Labyrinth::Users;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 12 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 12 ▶use Session::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 # -------------------------------------
55 # Variables
56  
57 12 my (%USERS,%FOLDERS);
58  
59 # -------------------------------------
60 # The Functional Interface
61  
62 =head1 FUNCTIONS
63  
64 =over 4
65  
66 =item Login
67  
68 Handles login capabilities, including bad logins.
69  
70 =item InternalLogin
71  
72 Saves the internal session of a successful login. Also used for automatic
73 authenticated logins.
74  
75 =item Logout
76  
77 Handles logout capabilities.
78  
79 =cut
80  
81 0 ▶sub Login {
Subroutine Login: not called CC=6 SCAR=37.4
Pod documented
82 # forgotten password?
83 0 ▶ return _forgotten() if($cgiparams{cause} && $cgiparams{forgot});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Session::cgiparams{"cause"} and $Labyrinth::Session::cgiparams{"forgot"} 0 0 0
Condition0%0/3
$Labyrinth::Session::cgiparams{"cause"} and $Labyrinth::Session::cgiparams{"forgot"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Labyrinth::Session::cgiparams{"cause"} and $Labyrinth::Session::cgiparams{"forgot"}
$Labyrinth::Session::cgiparams{"cause"} $Labyrinth::Session::cgiparams{"forgot"}
Truth table0%0/3
A and B
A= $Labyrinth::Session::cgiparams{"cause"}B= $Labyrinth::Session::cgiparams{"forgot"}
ABresult
0X0
100
111
84  
85 # values complete?
86 0 ▶ return SetError('ERROR',1) unless($cgiparams{cause} && $cgiparams{effect});
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Session::cgiparams{"cause"} and $Labyrinth::Session::cgiparams{"effect"} 0 0 0
Condition0%0/3
$Labyrinth::Session::cgiparams{"cause"} and $Labyrinth::Session::cgiparams{"effect"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Labyrinth::Session::cgiparams{"cause"} and $Labyrinth::Session::cgiparams{"effect"}
$Labyrinth::Session::cgiparams{"cause"} $Labyrinth::Session::cgiparams{"effect"}
Truth table0%0/3
A and B
A= $Labyrinth::Session::cgiparams{"cause"}B= $Labyrinth::Session::cgiparams{"effect"}
ABresult
0X0
100
111
87  
88 # verify username/password
89 0 my @rows = CheckUser($cgiparams{cause},$cgiparams{effect});
90 0 ▶ return SetError('BADUSER',1) unless(@rows);
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 0 0
91  
92 0 InternalLogin($rows[0]);
93 }
94  
95 0 ▶sub InternalLogin {
Subroutine InternalLogin: not called CC=3 SCAR=24.8
Pod documented
96 0 my $user = shift;
97  
98 0 $tvars{user} = $user;
99  
100 # add entry to session table
101 0 my $session;
102 ( $session,
103 $tvars{user}{name},
104 $tvars{'loginid'},
105 $tvars{realm},
106 $tvars{langcode}
107 0 ) = _save_session($user->{realname},$user->{userid},$user->{realm},$user->{langcode});
108  
109 # set template variables
110 0 $tvars{'loggedin'} = 1;
111 0 $tvars{user}{folder} = 1;
112 0 $tvars{user}{option} = 0;
113 0 $tvars{user}{userid} = $tvars{'loginid'};
114 0 $tvars{user}{access} = VerifyUser($tvars{'loginid'});
115  
116 0 ▶ $tvars{realm} = $user->{realm} || 'public';
Condition0%0/2
$user->{'realm'} || 'public'
l!l
00
MC/DC0%0/1
$user->{'realm'} || 'public'
$user->{'realm'}
Truth table0%0/2
A
A= $user->{'realm'}
Aresult
11
00
117  
118 0 ▶ if($tvars{realm} ne 'public') {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::Session::tvars{"realm"} ne "public") 0 0 0
119 0 SetCommand('home-' . $tvars{realm});
120 }
121 }
122  
123 0 ▶sub Logout {
Subroutine Logout: not called CC=4 SCAR=30.0
Pod documented
124 0 my @rows = CheckUser('GUEST','GUEST');
125 0 ▶ unless(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rows) 0 0 0
126 0 push @rows, {realname => 'Guest', userid => 0, realm => 'public', langcode => 'en'};
127 }
128  
129 0 my $session;
130 ( $session,
131 $tvars{user}{name},
132 $tvars{'loginid'},
133 $tvars{realm},
134 $tvars{langcode}
135 0 ) = _save_session($rows[0]->{realname},$rows[0]->{userid},$rows[0]->{realm},$rows[0]->{langcode});
136 0 $tvars{loggedin} = 0;
137 0 $tvars{user}{folder} = 1;
138 0 $tvars{user}{option} = 0;
139 0 $tvars{user}{userid} = $tvars{'loginid'};
140 0 $tvars{user}{access} = VerifyUser($tvars{'loginid'});
141  
142 $tvars{redirect} = $settings{'logout-redirect'}
143 0 ▶ if($settings{'logout-redirect'} && $settings{'logout-redirect'} ne $cgiparams{act});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Session::settings{"logout-redirect"} and $Labyrinth::Session::settings{"logout-redirect"} ne $Labyrinth::Session::cgiparams{"act"} 0 0 0
Condition0%0/3
$Labyrinth::Session::settings{"logout-redirect"} and $Labyrinth::Session::settings{"logout-redirect"} ne $Labyrinth::Session::cgiparams{"act"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Labyrinth::Session::settings{"logout-redirect"} and $Labyrinth::Session::settings{"logout-redirect"} ne $Labyrinth::Session::cgiparams{"act"}
$Labyrinth::Session::settings{"logout-redirect"} $Labyrinth::Session::settings{"logout-redirect"} ne $Labyrinth::Session::cgiparams{"act"}
Truth table0%0/3
A and B
A= $Labyrinth::Session::settings{"logout-redirect"}B= $Labyrinth::Session::settings{"logout-redirect"} ne $Labyrinth::Session::cgiparams{"act"}
ABresult
0X0
100
111
144 0 return($session,$tvars{user}{name},$tvars{'loginid'},$tvars{realm});
145 }
146  
147 =item ValidSession
148  
149 Reloads an existing session, or creates a new one.
150  
151 =item Store
152  
153 Stores the current request, while the user logs in. (A simple form of continuations)
154  
155 =item Retrieve
156  
157 Retrieves the last request, if the user has logged in. (A simple form of continuations)
158 If the user is already login will set according to their realm.
159  
160 =cut
161  
162 0 ▶sub ValidSession {
Subroutine ValidSession: not called CC=3 SCAR=24.8
Pod documented
163 # read cookie
164 0 my ($userid,$name,$realm,$folder,$langcode,$option) = _get_session();
165 0 ▶ $tvars{'loggedin'} = ($name && lc $name ne 'guest') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$name && lc $name ne 'guest' ? : 0 0 0
Condition0%0/3
$name and lc $name ne "guest"
!ll&&!rl&&r
000
MC/DC0%0/2
$name and lc $name ne "guest"
$name lc $name ne "guest"
Truth table0%0/3
A and B
A= $nameB= lc $name ne "guest"
ABresult
0X0
100
111
166 0 $tvars{'loginid'} = $userid;
167 0 $tvars{'langcode'} = $langcode;
168  
169 0 $tvars{user}{name} = $name;
170 0 $tvars{user}{userid} = $userid;
171 0 $tvars{user}{folder} = $folder;
172 0 $tvars{user}{option} = $option;
173 0 $tvars{user}{access} = VerifyUser($userid);
174  
175 0 my $user = Labyrinth::Session->new($userid,$name,$realm);
176 0 return $user;
177 }
178  
179 0 ▶sub Store {
Subroutine Store: not called CC=9 SCAR=45.0
Pod documented
180 # we don't want to continually logout!
181 0 ▶ return if($cgiparams{act} eq 'user-logout');
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Session::cgiparams{"act"} eq "user-logout" 0 0 0
182  
183 # store ready for continuation after login
184 0 ▶ if (&GetCookies('sessionid')){
Branch: 1 branch
BranchTrueFalseTotal
if (&GetCookies("sessionid")) 0 0 0
185 0 my $session = $main::Cookies{'sessionid'};
186 0 ▶ if($session && $session ne 'expired') {
Branch: 1 branch
BranchTrueFalseTotal
if ($session and $session ne "expired") 0 0 0
Condition0%0/3
$session and $session ne "expired"
!ll&&!rl&&r
000
MC/DC0%0/2
$session and $session ne "expired"
$session $session ne "expired"
Truth table0%0/3
A and B
A= $sessionB= $session ne "expired"
ABresult
0X0
100
111
187 0 ▶ if(my @rows = $dbi->GetQuery('array','CheckSession',$session)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rows) = $Labyrinth::Session::dbi->GetQuery("array", "CheckSession", $session)) 0 0 0
188 0 my $query;
189 0 ▶ if($cgiparams{lastpage} && $settings{lastpagereturn}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Labyrinth::Session::cgiparams{'lastpage'} and $Labyrinth::Session::settings{'lastpagereturn'}) { } 0 0 0
elsif ($Labyrinth::Session::settings{'lastpagereturn'}) { } 0 0 0
Condition0%0/3
$Labyrinth::Session::cgiparams{"lastpage"} and $Labyrinth::Session::settings{"lastpagereturn"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Labyrinth::Session::cgiparams{"lastpage"} and $Labyrinth::Session::settings{"lastpagereturn"}
$Labyrinth::Session::cgiparams{"lastpage"} $Labyrinth::Session::settings{"lastpagereturn"}
Truth table0%0/3
A and B
A= $Labyrinth::Session::cgiparams{"lastpage"}B= $Labyrinth::Session::settings{"lastpagereturn"}
ABresult
0X0
100
111
190 0 $query = $cgiparams{lastpage};
191 0 $query =~ y/~/=/;
192 0 $query =~ y/ /&/;
193 } elsif($settings{lastpagereturn}) {
194 0 $query = join("&",map {"$_=$cgiparams{$_}"} keys %cgiparams);
195 }
196 0 $dbi->DoQuery('StoreSession',$query,$session);
197 }
198 }
199 }
200 }
201  
202 0 ▶sub Retrieve {
Subroutine Retrieve: not called CC=11 SCAR=48.8
Pod documented
203 0 my $act = 'home-' . $tvars{realm};
204 0 LogDebug("Retrieve: 1.=$act");
205  
206 0 ▶ if(my @rows = $dbi->GetQuery('hash','GetRealmByName',$tvars{realm})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rows) = $Labyrinth::Session::dbi->GetQuery("hash", "GetRealmByName", $Labyrinth::Session::tvars{"realm"})) 0 0 0
207 0 $act = $rows[0]->{command};
208 }
209 0 LogDebug("Retrieve: 2.=$act");
210  
211 0 ▶ if (&GetCookies('sessionid')){
Branch: 1 branch
BranchTrueFalseTotal
if (&GetCookies("sessionid")) 0 0 0
212 0 my $session = $main::Cookies{'sessionid'};
213 0 ▶ if($session && $session ne 'expired') {
Branch: 1 branch
BranchTrueFalseTotal
if ($session and $session ne "expired") 0 0 0
Condition0%0/3
$session and $session ne "expired"
!ll&&!rl&&r
000
MC/DC0%0/2
$session and $session ne "expired"
$session $session ne "expired"
Truth table0%0/3
A and B
A= $sessionB= $session ne "expired"
ABresult
0X0
100
111
214 0 ▶ if(my @rows = $dbi->GetQuery('array','RetrieveSession',$session)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rows) = $Labyrinth::Session::dbi->GetQuery("array", "RetrieveSession", $session)) 0 0 0
215 0 ▶LogDebug("Retrieve: 3.=[".($rows[0]->[0]||'')."]");
Condition0%0/2
$rows[0][0] || ''
l!l
00
MC/DC0%0/1
$rows[0][0] || ''
$rows[0][0]
Truth table0%0/2
A
A= $rows[0][0]
Aresult
11
00
216 0 ▶ my @parts = $rows[0]->[0] ? split("&",$rows[0]->[0]) : ();
Branch: 1 branch
BranchTrueFalseTotal
$rows[0][0] ? : 0 0 0
217 0 for my $part (@parts) {
218 0 ▶ $cgiparams{$1} = $2 if($part =~ /(.*?)=(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /(.*?)=(.*)/ 0 0 0
219 }
220 0 ▶ $act = $cgiparams{act} if(@parts);
Branch: 1 branch
BranchTrueFalseTotal
if @parts 0 0 0
221 0 $dbi->DoQuery('StoreSession','',$session);
222 0 LogDebug("Retrieve: 4.=$act");
223 }
224 }
225 }
226  
227 0 LogDebug("Retrieve: NEXT=$act");
228 0 SetCommand($act);
229 }
230  
231 =item Authorised($level[,$userid])
232  
233 Verifies the user has authorisation to the requested level. If userid is
234 omitted, the current user is assumed.
235  
236 =item UserAccess
237  
238 Returns the folders the user (and associated groups) has access to.
239  
240 =item VerifyUser
241  
242 Looks up the user's authorisation level, based on their user id and any groups
243 they belong to.
244  
245 =item CheckUser
246  
247 Given a username and password checks the database to ensure that the user
248 exists. Note that this uses both SHA1 (new encryption) and OLD_PASSWORD (old
249 encyription) to find the user. The latter is preserved for older
250 implementations.
251  
252 =cut
253  
254 0 ▶sub Authorised {
Subroutine Authorised: not called CC=5 SCAR=34.0
Pod documented
255 0 my $needed = shift;
256 0 ▶ return 0 if($needed && !$tvars{loggedin});
Branch: 1 branch
BranchTrueFalseTotal
if $needed and not $Labyrinth::Session::tvars{"loggedin"} 0 0 0
Condition0%0/3
$needed and not $Labyrinth::Session::tvars{"loggedin"}
!ll&&!rl&&r
000
MC/DC0%0/2
$needed and not $Labyrinth::Session::tvars{"loggedin"}
$needed not $Labyrinth::Session::tvars{"loggedin"}
Truth table0%0/3
A and not(B)
A= $neededB= not $Labyrinth::Session::tvars{"loggedin"}
ABresult
0X0
100
111
257  
258 0 ▶ my $userid = shift || $tvars{'loginid'};
Condition0%0/3
shift() || $Labyrinth::Session::tvars{'loginid'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $Labyrinth::Session::tvars{'loginid'}
shift() $Labyrinth::Session::tvars{'loginid'}
Truth table0%0/3
A || B
A= shift()B= $Labyrinth::Session::tvars{'loginid'}
ABresult
1X1
011
000
259 0 my $actual = VerifyUser($userid);
260  
261 # LogDebug("Authorised - needed=[$needed], actual=[$actual], result=[".($actual >= $needed ? 1 : 0)."]");
262  
263 0 ▶ return $actual >= $needed ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$actual >= $needed ? : 0 0 0
264 }
265  
266 0 ▶sub UserAccess {
Subroutine UserAccess: not called CC=2 SCAR=17.9
Pod documented
267 0 my $folderid = shift;
268 0 my $groups = shift;
269  
270 0 my @rows = $dbi->GetQuery('array','FolderAccess',$tvars{loginid},$groups);
271 0 ▶ return 0 unless(@rows);
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 0 0
272 0 return $rows[0]->[0];
273 }
274  
275 12 my %folderaccess;
276  
277 0 ▶sub VerifyUser {
Subroutine VerifyUser: not called CC=9 SCAR=45.0
Pod documented
278 0 ▶ my $userid = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
279 0 ▶ my $folder = shift || 'public';
Condition0%0/2
shift() || 'public'
l!l
00
MC/DC0%0/1
shift() || 'public'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
280 0 my $access = 0;
281 0 LogDebug("VerifyUser($userid,'$folder')");
282  
283 0 ▶ return $access unless($userid);
Branch: 1 branch
BranchTrueFalseTotal
unless $userid 0 0 0
284  
285 # return if known
286 return $folderaccess{$userid}{$folder}
287 0 ▶ if($folderaccess{$userid}{$folder});
Branch: 1 branch
BranchTrueFalseTotal
if $folderaccess{$userid}{$folder} 0 0 0
288  
289 # check base access
290 0 my $user = GetUser($userid);
291 0 $access = $user->{accessid};
292 0 $tvars{user}{$_} = $user->{$_} for(qw(realname nickname email));
293  
294 0 ▶ my @folders = ($folder ? GetFolderIDs( ref => $folder ) : (1));
Branch: 1 branch
BranchTrueFalseTotal
$folder ? : 0 0 0
295 0 my $folders = join(',',grep {$_} @folders);
296 0 my $groups = GetGroupIDs($userid);
297  
298 # check folder permissions
299 0 my @rows = $dbi->GetQuery('hash','GetPermission',{folders=>$folders,groups=>$groups,user=>$userid});
300 0 foreach my $rec (@rows) {
301 0 ▶ $access = $rec->{accessid} if($access < $rec->{accessid});
Branch: 1 branch
BranchTrueFalseTotal
if $access < $rec->{"accessid"} 0 0 0
302 }
303  
304 0 LogDebug("-access=$access");
305  
306 0 $folderaccess{$userid}{$folder} = $access;
307 0 return $access;
308 }
309  
310 0 ▶sub CheckUser {
Subroutine CheckUser: not called CC=4 SCAR=30.0
Pod documented
311 0 my ($user,$pass) = @_;
312  
313 0 ▶ return @{$USERS{$user}} if($USERS{$user});
Branch: 1 branch
BranchTrueFalseTotal
if $USERS{$user} 0 0 0
314  
315 # SHA1 encryption
316 0 my @rows = $dbi->GetQuery('hash','CheckUser',$user,$pass);
317 0 ▶ if(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows) 0 0 0
318 0 $USERS{$user} = \@rows;
319 0 return @rows;
320 }
321  
322 # OLD PASSWORD encryption
323 0 @rows = $dbi->GetQuery('hash','CheckUserOld',$user,$pass);
324 0 ▶ if(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows) 0 0 0
325 0 $USERS{$user} = \@rows;
326 0 return @rows;
327 }
328  
329 # user not found
330 0 return;
331 }
332  
333 =item LoadFolders
334  
335 Convienence function to load all folders when required.
336  
337 =item GetFolderIDs
338  
339 Returns the list of folders for the given leaf folder.
340  
341 =item FolderAccess
342  
343 Returns true or false as to whether the given user has access to the specified
344 folder. If no folder is given the default 'public' folder is used. If no user
345 is given the currently logged in user is used.
346  
347 =cut
348  
349 0 ▶sub LoadFolders {
Subroutine LoadFolders: not called CC=3 SCAR=24.8
Pod documented
350 0 ▶ return if(%FOLDERS);
Branch: 1 branch
BranchTrueFalseTotal
if %FOLDERS 0 0 0
351  
352 0 my @rows = $dbi->GetQuery('hash','AllFolders');
353 0 for my $row (@rows) {
354 0 $FOLDERS{$row->{folderid}} = $row;
355 }
356 }
357  
358 0 ▶sub GetFolderIDs {
Subroutine GetFolderIDs: not called CC=9 SCAR=45.0
Pod documented
359 0 my %hash = @_;
360 0 my ($id,%ids,@ids);
361  
362 0 LoadFolders();
363  
364 0 ▶ if($hash{id}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hash{'id'}) { } 0 0 0
elsif ($hash{'ref'}) { } 0 0 0
365 0 $id = $hash{id};
366  
367 } elsif($hash{ref}) {
368 0 for my $folderid (keys %FOLDERS) {
369 0 ▶ if($FOLDERS{$folderid}->{path} eq $hash{ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FOLDERS{$folderid}{"path"} eq $hash{"ref"}) 0 0 0
370 0 $id = $folderid;
371 0 last;
372 }
373 }
374 }
375  
376 0 ▶ return '0' unless($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
377  
378 0 ▶ while($FOLDERS{$id} && $FOLDERS{$id}->{parent} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FOLDERS{$id} and $FOLDERS{$id}{"parent"} > 0) 0 0 0
Condition0%0/3
$FOLDERS{$id} and $FOLDERS{$id}{"parent"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$FOLDERS{$id} and $FOLDERS{$id}{"parent"} > 0
$FOLDERS{$id} $FOLDERS{$id}{"parent"} > 0
Truth table0%0/3
A and B
A= $FOLDERS{$id}B= $FOLDERS{$id}{"parent"} > 0
ABresult
0X0
100
111
379 0 $ids{$id} = 1;
380 0 $id = $FOLDERS{$hash{id}}->{parent};
381 }
382 0 $ids{$id} = 1;
383 0 @ids = keys %ids;
384  
385 0 ▶ return @ids if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
386 0 return join(",",@ids);
387 }
388  
389 0 ▶sub FolderAccess {
Subroutine FolderAccess: not called CC=5 SCAR=34.0
Pod documented
390 0 ▶ my $folder = shift || 'public';
Condition0%0/2
shift() || 'public'
l!l
00
MC/DC0%0/1
shift() || 'public'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
391 0 ▶ my $userid = shift || $tvars{loginid};
Condition0%0/3
shift() || $Labyrinth::Session::tvars{'loginid'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $Labyrinth::Session::tvars{'loginid'}
shift() $Labyrinth::Session::tvars{'loginid'}
Truth table0%0/3
A || B
A= shift()B= $Labyrinth::Session::tvars{'loginid'}
ABresult
1X1
011
000
392  
393 0 LogDebug("FolderAccess('$folder',$userid)");
394  
395 0 my @rows = $dbi->GetQuery('hash','GetFolderByPath',$folder);
396 0 ▶ return 0 unless(@rows);
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 0 0
397  
398 0 my $access = VerifyUser($userid,$folder);
399 0 ▶ return 1 if($access >= $rows[0]->{accessid});
Branch: 1 branch
BranchTrueFalseTotal
if $access >= $rows[0]{"accessid"} 0 0 0
400 0 return 0;
401 }
402  
403 =item GetGroupIDs
404  
405 Returns the list of groups the given user has access to.
406  
407 =cut
408  
409 0 ▶sub GetGroupIDs {
Subroutine GetGroupIDs: not called CC=7 SCAR=40.3
Pod documented
410 0 my $userid = shift;
411 0 my %groups;
412  
413 0 $groups{1} = 1; # everyone is public
414  
415 # find primary groups for user
416 0 my @rows = $dbi->GetQuery('array','GetGroupUserMap',$userid);
417  
418 0 ▶ while(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows) 0 0 0
419 0 my (@parents);
420 0 foreach (@rows) {
421 0 ▶ next if($_->[0] == 0); # a bad entry
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] == 0 0 0 0
422 0 ▶ next if($groups{$_->[0]}); # already seen group
Branch: 1 branch
BranchTrueFalseTotal
if $groups{$_->[0]} 0 0 0
423 0 $groups{$_->[0]} = 1;
424 0 push @parents, $_->[0];
425 }
426  
427 0 ▶ last unless(@parents);
Branch: 1 branch
BranchTrueFalseTotal
unless @parents 0 0 0
428  
429 # find associated groups for user
430 0 @rows = $dbi->GetQuery('array','GetGroupParents',{groups=>join(",",@parents)});
431 }
432  
433 0 ▶ return keys %groups if(wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
434 0 return join(",",keys %groups);
435 }
436  
437 =item ResetLanguage
438  
439 Within the current session, this function allows the user to change the
440 language associated within the system.
441  
442 Currently this language element is under used, and could be used for error and
443 message strings pulled from a phrasebook.
444  
445 =cut
446  
447 0 ▶sub ResetLanguage {
Subroutine ResetLanguage: not called CC=3 SCAR=24.8
Pod documented
448 0 my $lang = shift;
449 0 ▶ return unless($lang);
Branch: 1 branch
BranchTrueFalseTotal
unless $lang 0 0 0
450  
451 0 my @rows = $dbi->GetQuery('array','GetLang',$lang);
452 0 ▶ return unless(@rows);
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 0 0
453  
454 0 $dbi->DoQuery('SetLangUser',$lang,$tvars{loginid});
455 0 $dbi->DoQuery('SetLangSession',$lang,$settings{session});
456 0 $tvars{langcode} = $lang;
457 }
458  
459 =item UpdateSession
460  
461 Updates specific fields for the current session.
462  
463 =back
464  
465 =cut
466  
467 0 ▶sub UpdateSession {
Subroutine UpdateSession: not called CC=5 SCAR=34.0
Pod documented
468 0 my %hash = @_;
469 0 my $session = delete $hash{session};
470 0 ▶ $session ||= $main::Cookies{'sessionid'};
Condition0%0/3
$session ||= $main::Cookies{"sessionid"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$session ||= $main::Cookies{"sessionid"}
$session $main::Cookies{"sessionid"}
Truth table0%0/3
A ||= B
A= $sessionB= $main::Cookies{"sessionid"}
ABresult
1X1
011
000
471 0 for(keys %hash) {
472 0 ▶ next unless($hash{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless $hash{$_} 0 0 0
473 0 $dbi->DoQuery('UpdateSession',{field=>$_},$hash{$_},$session);
474 }
475  
476 0 ▶ if($hash{optionid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{"optionid"}) 0 0 0
477 0 $tvars{user}{option} = $hash{optionid};
478 }
479 }
480  
481 # -------------------------------------
482 # The Object Interface
483  
484 =head1 OBJECT METHODS
485  
486 In addition to the above functions, the Session Management also allows for an
487 object interface.
488  
489 =over 4
490  
491 =item new
492  
493 Create a new session object.
494  
495 =item realm
496  
497 Returns the current realm.
498  
499 =cut
500  
501 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
502 0 my $self = shift;
503  
504 0 my $atts = {
505 'userid' => $_[0],
506 'name' => $_[1],
507 'realm' => $_[2],
508 };
509  
510 # create the object
511 0 bless $atts, $self;
512 0 return $atts;
513 }
514  
515 0 ▶sub realm {
Subroutine realm: not called CC=1 SCAR=6.9
Pod documented
516 0 my $self = shift;
517 0 return $self->{realm};
518 }
519  
520 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
521  
522 # -------------------------------------
523 # Internal Functions
524  
525 0 ▶sub _create_session_key {
Subroutine _create_session_key: not called CC=1 SCAR=6.9
526 0 my $gen = Session::Token->new(length => 24);
527 0 return $gen->get();
528 }
529  
530 0 ▶sub _get_session {
Subroutine _get_session: not called CC=24 SCAR=64.0
531 0 my $tsnow = formatDate(0);
532  
533 0 ▶ if($settings{delete_sessions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::Session::settings{"delete_sessions"}) 0 0 0
534 # delete timed out sessions, including this one if necessary (self cleaning)
535 0 ▶ my $timeout = $settings{timeout} || 0;
Condition0%0/2
$Labyrinth::Session::settings{'timeout'} || 0
l!l
00
MC/DC0%0/1
$Labyrinth::Session::settings{'timeout'} || 0
$Labyrinth::Session::settings{'timeout'}
Truth table0%0/2
A
A= $Labyrinth::Session::settings{'timeout'}
Aresult
11
00
536 0 my $tsthen = $tsnow - $timeout;
537 0 $dbi->DoQuery('DeleteSessions',$tsthen);
538 }
539  
540 # default settings
541 0 my ($userid,$name,$realm,$folder,$langcode,$option) = (0,'guest','public',1,'en',0);
542 0 my $session;
543  
544 # retrieve the cookie
545 0 ▶ if($settings{testing}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Labyrinth::Session::settings{'testing'}) { } 0 0 0
elsif (&GetCookies('sessionid')) { } 0 0 0
546 0 ▶ $userid = $cgiparams{cluserid} if($cgiparams{cluserid});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Session::cgiparams{"cluserid"} 0 0 0
547 0 ▶ $name = $cgiparams{clname} if($cgiparams{clname});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Session::cgiparams{"clname"} 0 0 0
548 0 ▶ $realm = $cgiparams{clrealm} if($cgiparams{clrealm});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Session::cgiparams{"clrealm"} 0 0 0
549 0 ▶ $folder = $cgiparams{clfolder} if($cgiparams{clfolder});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Session::cgiparams{"clfolder"} 0 0 0
550 0 ▶ $langcode = $cgiparams{cllangcode} if($cgiparams{cllangcode});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Session::cgiparams{"cllangcode"} 0 0 0
551 #LogDebug("get_session: testing: ($userid,$name,$realm,$folder)");
552 } elsif (&GetCookies('sessionid')){
553 0 $session = $main::Cookies{'sessionid'};
554 0 LogDebug("session=$session");
555 } else {
556 0 LogDebug("session=<no session>");
557 }
558  
559 0 ▶ if(!$userid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($userid) 0 0 0
560 0 my @rows = CheckUser('GUEST','GUEST');
561 0 $userid = $rows[0]->{userid};
562 }
563  
564 0 ▶ $session = undef if($session && $session eq 'expired');
Branch: 1 branch
BranchTrueFalseTotal
if $session and $session eq "expired" 0 0 0
Condition0%0/3
$session and $session eq "expired"
!ll&&!rl&&r
000
MC/DC0%0/2
$session and $session eq "expired"
$session $session eq "expired"
Truth table0%0/3
A and B
A= $sessionB= $session eq "expired"
ABresult
0X0
100
111
565  
566 # try and time stamp the session
567 0 ▶ if($session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 0 0 0
568 0 my @rows = $dbi->GetQuery('array','CheckSession',$session);
569 0 ▶ LogDebug("CheckSession: 1.".(@rows ? 'found' : 'no')." session");
Branch: 1 branch
BranchTrueFalseTotal
@rows ? : 0 0 0
570 0 ▶ if(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows) { } 0 0 0
571 0 ($userid,$name,$realm,$folder,$langcode,$option) = @{$rows[0]};
572 0 ▶ $option = $cgiparams{option} if($cgiparams{option});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Session::cgiparams{"option"} 0 0 0
573 0 UpdateSession(timeout => $tsnow, optionid => $option, session => $session);
574 } else {
575 0 $session = undef;
576 }
577 }
578  
579 # check we actually updated in time
580 0 ▶ if($session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 0 0 0
581 0 my @rows = $dbi->GetQuery('array','CheckSession',$session);
582 0 ▶ LogDebug("CheckSession: 2.".(@rows ? 'found' : 'no')." session");
Branch: 1 branch
BranchTrueFalseTotal
@rows ? : 0 0 0
583 0 ▶ $session = undef unless(@rows);
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 0 0
584 }
585  
586 # create a new session if necessary
587 0 ▶ unless($session) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session) 0 0 0
588 0 ▶ if($settings{testing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::Session::settings{'testing'}) { } 0 0 0
589 0 ($session) = Logout();
590 } else {
591 0 ($session,$name,$userid,$realm,$langcode) = Logout();
592 }
593 }
594 0 $settings{session} = $session;
595  
596 0 ▶ LogDebug('GetSession:name=['.($name||'').'], realm=['.($realm||'').']');
Condition0%0/4
$name || ''
l!l
00
$realm || ''
l!l
00
MC/DC0%0/1
$name || ''
$name
MC/DC0%0/1
$realm || ''
$realm
Truth table0%0/2
A
A= $name
Aresult
11
00
Truth table0%0/2
A
A= $realm
Aresult
11
00
597  
598 0 return $userid,$name,$realm,$folder,$langcode,$option;
599 }
600  
601 0 ▶sub _save_session {
Subroutine _save_session: not called CC=15 SCAR=54.8
602 0 my @fields = @_;
603 0 my $session;
604  
605 0 ▶ LogDebug('SaveSession:1 fields=['.join('][',map {$_ || ''} @fields).']');
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
606  
607 0 ▶ $fields[0] ||= 'guest';
Condition0%0/2
$fields[0] ||= "guest"
l!l
00
MC/DC0%0/1
$fields[0] ||= "guest"
$fields[0]
Truth table0%0/2
A
A= $fields[0]
Aresult
11
00
608 0 ▶ $fields[1] ||= 0;
Condition0%0/2
$fields[1] ||= 0
l!l
00
MC/DC0%0/1
$fields[1] ||= 0
$fields[1]
Truth table0%0/2
A
A= $fields[1]
Aresult
11
00
609 0 ▶ $fields[2] ||= 'public';
Condition0%0/2
$fields[2] ||= "public"
l!l
00
MC/DC0%0/1
$fields[2] ||= "public"
$fields[2]
Truth table0%0/2
A
A= $fields[2]
Aresult
11
00
610 0 ▶ $fields[3] ||= 'en';
Condition0%0/2
$fields[3] ||= "en"
l!l
00
MC/DC0%0/1
$fields[3] ||= "en"
$fields[3]
Truth table0%0/2
A
A= $fields[3]
Aresult
11
00
611 0 ▶ $fields[4] ||= 0;
Condition0%0/2
$fields[4] ||= 0
l!l
00
MC/DC0%0/1
$fields[4] ||= 0
$fields[4]
Truth table0%0/2
A
A= $fields[4]
Aresult
11
00
612  
613 0 ▶ if($fields[1] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[1] == 0) 0 0 0
614 0 my @rows = CheckUser('GUEST','GUEST');
615 0 $fields[1] = $rows[0]->{userid};
616 }
617  
618 0 ▶ LogDebug('SaveSession:2 fields=['.join('][',map {$_ || ''} @fields).']');
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
619  
620 0 ▶ $session = $main::Cookies{'sessionid'} if(GetCookies('sessionid'));
Branch: 1 branch
BranchTrueFalseTotal
if GetCookies("sessionid") 0 0 0
621 0 ▶ if($session && $session ne 'expired') {
Branch: 1 branch
BranchTrueFalseTotal
if ($session and $session ne 'expired') { } 0 0 0
Condition0%0/3
$session and $session ne "expired"
!ll&&!rl&&r
000
MC/DC0%0/2
$session and $session ne "expired"
$session $session ne "expired"
Truth table0%0/3
A and B
A= $sessionB= $session ne "expired"
ABresult
0X0
100
111
622 # check the session has been recorded in case it's been reaped, a user
623 # can relogin with the same session key
624 0 my @rows = $dbi->GetQuery('array','CheckSession',$session);
625 0 ▶ LogDebug("CheckSession: 3.".(@rows ? 'found' : 'no')." session");
Branch: 1 branch
BranchTrueFalseTotal
@rows ? : 0 0 0
626 0 ▶ if(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows) { } 0 0 0
627 0 $dbi->DoQuery('UpdateSessionX',formatDate(0),@fields,$session);
628 } else {
629 0 $dbi->DoQuery('CreateSession',formatDate(0),@fields,$session);
630 }
631 } else {
632 # add entry to session table
633 0 $session = _create_session_key($cgiparams{cause});
634 0 $dbi->DoQuery('CreateSession',formatDate(0),@fields,$session);
635 }
636  
637 0 SetCookiePath('/');
638 0 $tvars{cookie} = SetCookie('sessionid',$session);
639 0 ▶ LogDebug('SaveSession:4 fields=['.join('][',map {$_ || ''} @fields).']');
Condition0%0/2
$_ || ''
l!l
00
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
640 0 return ($session,@fields);
641 }
642  
643 0 ▶sub _forgotten {
Subroutine _forgotten: not called CC=5 SCAR=34.0
644 0 my @rows = $dbi->GetQuery('hash','FindUser',$cgiparams{cause});
645 0 ▶ return SetError('BADUSER') unless(@rows);
Branch: 1 branch
BranchTrueFalseTotal
unless @rows 0 0 0
646 0 ▶ return SetError('BANUSER') if($rows[0]->{password} eq '-banned-');
Branch: 1 branch
BranchTrueFalseTotal
if $rows[0]{"password"} eq "-banned-" 0 0 0
647  
648 0 my $password = FreshPassword();
649 0 ▶ my $name = $rows[0]->{'realname'} || 'User';
Condition0%0/2
$rows[0]{'realname'} || 'User'
l!l
00
MC/DC0%0/1
$rows[0]{'realname'} || 'User'
$rows[0]{'realname'}
Truth table0%0/2
A
A= $rows[0]{'realname'}
Aresult
11
00
650  
651 0 $dbi->DoQuery('ChangePassword',$password,$rows[0]->{userid});
652 MailSend( template => 'mailer/forgot.eml',
653 name => $name,
654 password => $password,
655 email => $cgiparams{cause}
656 0 );
657  
658 0 ▶ if(MailSent()) {
Branch: 1 branch
BranchTrueFalseTotal
if (MailSent()) { } 0 0 0
659 0 SetCommand('user-forgot');
660 } else {
661 0 SetError('BADMAIL');
662 }
663 }
664  
665 12 1;
666  
667 __END__
« Labyrinth/Media.pm ↑ Summary Labyrinth/Metadata.pm »