MLite/AuthSsn.pm

filter: Statementstmt 9.7% 18 / 184 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 57 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 24.0% 6 / 25 Podpod n/a totaltotal 6.8% 24 / 352 CC 52 SCAR 36.1
CC
52
Cov
6%
new
110.0
authz
72.0
access
42.0
CRAP 37.1
« MLite/Helper/Skel.pm ↑ Summary MLite/Util.pm »
1 package WWW::MLite::AuthSsn; # $Id: AuthSsn.pm 29 2014-08-01 06:53:56Z minus $
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3
4 =head1 NAME
5
6 WWW::MLite::AuthSsn - AAA mechanism support via sessions
7
8 =head1 VERSION
9
10 Version 1.00
11
12 =head1 SYNOPSIS
13
14 use WWW::MLite::AuthSsn;
15
16 my $ssn = new WWW::MLite::AuthSsn(
17 -dsn => "driver:sqlite",
18 -sid => ($q->param("SID") || $q->cookie("SID") || undef),
19 -key => "SID",
20 -expire => "+3M",
21 -args => { DataSource => '/my/folder/sessions.sqlt' },
22 );
23
24 # Authentication && Authorization
25 $ssn->authn or die("Bad authentication");
26 $ssn->authz or die("Bad authorization");
27
28 ...
29
30 # Access/Accounting
31 $ssn->access or die("Access denied");
32
33
34 =head1 DESCRIPTION
35
36 Authorisation/Authentication/Access (AAA) mechanism support via sessions
37
38 =head2 METHODS
39
40 =over 8
41
42 =item B<new>
43
44 my $ssn = new WWW::MLite::AuthSsn(
45 -dsn => $dsn, # See CGI::Session
46 -sid => $sid || undef), # Session IDentifier
47 -key => "SID", # Key name
48 -expire => "+3M", # Expires
49 -args => { ... args ... }, # See CGI::Session
50 );
51
52 Creating AuthSsn object
53
54 =item B<init>
55
56 $ssn->init;
57
58 Initialising the session. For internal use only. Please do not use it
59
60 Method returns status operation: 1 - successfully; 0 - not successfully
61
62 =item B<update>
63
64 $ssn->update;
65
66 Updating static data of the session. For internal use only. Please do not use it
67
68 =item B<authen>
69
70 $ssn->authen;
71 $ssn->authen( $callback, ...arguments... );
72
73 AAA Authentication.
74
75 The method returns status operation: 1 - successfully; 0 - not successfully
76
77 =item B<authz>
78
79 $ssn->authz;
80 $ssn->authz( $callback, ...arguments... );
81
82 AAA Authorization.
83
84 The method returns status operation: 1 - successfully; 0 - not successfully
85
86 =item B<access>
87
88 $ssn->access;
89 $ssn->access( $callback, ...arguments... );
90
91 AAA Accounting (AAA Access).
92
93 The method returns status operation: 1 - successfully; 0 - not successfully
94
95 =item B<get>
96
97 $ssn->get( $key );
98
99 Returns user session value by $key
100
101 =item B<set>
102
103 $ssn->set( $key, $value );
104
105 Sets user session value by $key
106
107 =item B<delete>
108
109 $ssn->delete;
110
111 Delete the session
112
113 =item B<sid, usid>
114
115 $ssn->sid;
116
117 Returns current usid value
118
119 =item B<expires>
120
121 $ssn->expires;
122
123 Returns current expires value
124
125 =item B<status>
126
127 $ssn->status;
128 $ssn->status( $newstatus );
129
130 Returns status of a previously executed operation. If you specify $newstatus, there will push installation $newstatus
131
132 =item B<reason, reason_translate>
133
134 $ssn->reason;
135 $ssn->reason( $newreason );
136 $ssn->reason_translate;
137
138 Returns reason of a previously executed operation. If you specify $newreason, there will push installation $newreason
139
140 Now supported following values: DEFAULT, OK, UNAUTHORIZED, ERROR, SERVER_ERROR, NEW, TIMEOUT, LOGIN_INCORRECT,
141 PASSWORD_INCORRECT, DECLINED, AUTH_REQUIRED, FORBIDDEN.
142
143 For translating this values to regular form please use method reason_translate like that
144
145 =item B<error>
146
147 $ssn->error();
148 $ssn->error( $newerror );
149
150 Returns error of a previously executed operation. If you specify $newerror, there will push installation $newerror
151
152 =item B<toexpire>
153
154 $ssn->toexpire( $time );
155
156 Returns expiration interval relative to ctime() form.
157
158 If used with no arguments, returns the expiration interval if it was ever set.
159 If no expiration was ever set, returns undef.
160
161 All the time values should be given in the form of seconds.
162 Following keywords are also supported for your convenience:
163
164 +-----------+---------------+
165 | alias | meaning |
166 +-----------+---------------+
167 | s | Second |
168 | m | Minute |
169 | h | Hour |
170 | d | Day |
171 | w | Week |
172 | M | Month |
173 | y | Year |
174 +-----------+---------------+
175
176 Examples:
177
178 $ssn->toexpire("2h"); # expires in two hours
179 $ssn->toexpire(3600); # expires in one hour
180
181 Note: all the expiration times are relative to session's last access time, not to its creation time.
182 To expire a session immediately, call delete() method.
183
184 =item B<get_atime, get_ctime>
185
186 $ssn->get_atime;
187 $ssn->get_ctime;
188
189 Returns current atime and ctime values. Value atime - access time; ctime - create time
190
191 =item B<get_data>
192
193 $ssn->get_data;
194
195 Returns user data of current session as hash-ref
196
197 =item B<get_expires>
198
199 $ssn->get_expires;
200
201 Returns expiring interval of current session
202
203 =back
204
205 =head1 CONFIGURATION
206
207 Sample in file conf/auth.conf:
208
209 <Auth>
210 expires +3m
211 #sidkey usid
212 </Auth>
213
214 =head1 EXAMPLES
215
216 =over 8
217
218 =item B<SQLite>
219
220 my $dbh = DBI->connect("dbi:SQLite:dbname=/tmp/sessions.db", "","", { RaiseError => 1, sqlite_unicode => 1, });
221 $dbh->do('CREATE TABLE IF NOT EXISTS sessions ( id CHAR(32) NOT NULL PRIMARY KEY, a_session TEXT NOT NULL )');
222 my $ssn = new WWW::MLite::AuthSsn(
223 -dsn => "driver:sqlite",
224 -sid => ($q->param("SID") || $q->cookie("SID") || undef),
225 -key => "SID",
226 -expire => "+3M",
227 -args => { Handle => $dbh },
228 );
229
230 See L<CGI::Session::Driver::sqlite>
231
232 =back
233
234 =head1 HISTORY
235
236 See C<CHANGES> file
237
238 =head1 TO DO
239
240 See C<TODO> file
241
242 =head1 SEE ALSO
243
244 L<WWW::MLite>, L<CGI::Session>
245
246 =head1 AUTHOR
247
248 Serz Minus (Lepenkov Sergey) L<http://www.serzik.com> E<lt>minus@mail333.comE<gt>
249
250 =head1 COPYRIGHT
251
252 Copyright (C) 1998-2014 D&D Corporation. All Rights Reserved
253
254 =head1 LICENSE
255
256 This program is free software: you can redistribute it and/or modify
257 it under the terms of the GNU General Public License as published by
258 the Free Software Foundation, either version 3 of the License, or
259 (at your option) any later version.
260
261 This program is distributed in the hope that it will be useful,
262 but WITHOUT ANY WARRANTY; without even the implied warranty of
263 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
264 GNU General Public License for more details.
265
266 See C<LICENSE> file
267
268 =cut
269
270 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
271 $VERSION = '1.00';
272
273 2 ▶use CGI::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 2 ▶use CTK::Util qw/ :API :FORMAT :DATE /;
Subroutine BEGIN: called CC=1 SCAR=0.0
275 2 ▶use CTK::TFVals qw/ :ALL /;
Subroutine BEGIN: called CC=1 SCAR=0.0
276
277 use constant {
278 2 SIDKEY => 'usid',
279 EXPIRES => '+1h', # 3600sec as default
280
281 # Îïðåäåëÿåì òàáëèöó ïåðåâîäà ñîñòîÿíèé
282 STAT => {
283 DEFAULT => to_utf8('Ñòàòóñ íåîïðåäåëåí'),
284 NEW => to_utf8('Ñåñèèÿ ñîçäàíà óñïåøíî'), # new/authen
285 OK => to_utf8('Îïåðàöèÿ ïðîøëà óñïåøíî'), # new/authen/authez/access
286 ERROR => to_utf8('Ñåññèÿ íå ñóùåñòâóåò èëè âîçíèêëà îøèáêà ñîçäàíèÿ ñåññèè'), # new
287 SERVER_ERROR => to_utf8('Îøèáêà ñåðâåðà'),
288 TIMEOUT => to_utf8('Ïåðèîä æèçíè ñåññèè èñòåê'), # new/access
289 UNAUTHORIZED => to_utf8('Âû íåàâòîðèçèðîâàíû'), # authz/access/delete
290 AUTH_REQUIRED => to_utf8('Òðåáóåòñÿ àâòîðèçàöèÿ'), # new
291 FORBIDDEN => to_utf8('Äîñòóï çàïðåùåí'), # authz/access
292 DECLINED => to_utf8('Ó÷åòíàÿ çàïèñü îòñóòñòâóåò'), # authen
293 LOGIN_INCORRECT => to_utf8('Íåïðàâèëüíûé ëîãèí'), # authen
294 PASSWORD_INCORRECT => to_utf8('Íåïðàâèëüíûé ïàðîëü'), # authen
295 },
296 2 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
297
298
299 sub new { # Êîíñòðóêòîð: ñîçäàåì èëè èñïîëüçóåì ðàíåå ñîçäàííóþ ñåññèèþ
300 0 ▶ my $class = shift;
Subroutine new: not called CC=10 SCAR=47.0
301 0 my @in = read_attributes(
302 [
303 [qw/ DSN DRIVE DRIVER /], # 0 - DSN (See CGI::Session)
304 [qw/ SID USID /], # 1 - USID User Session IDentifier
305 [qw/ DSNARGS DSN_ARGS ARGS /], # 2 - DSN_ARGS (See CGI::Session)
306 [qw/ EXPIRE EXPIRES TIME /], # 3 - Expires (Êîëè÷åñòâî ñåêóíä êîòîðîå ìîæåò ñóùåñòâîâàòü ñåññèÿ)
307 [qw/ KEY SIDKEY USIDKEY NAME /],# 4 - Name of session key (See CGI::Session)
308 ],
309 ,@_);
310
311 # Defines & Checks
312 0 my $dsn = fv2null($in[0]);
313 0 my $sid = fv2undef($in[1]);
314 0 my $args = fv2undef($in[2]);
315 0 ▶ my $expires = $in[3] || EXPIRES;
Condition0%0/2
$in[3] || '+1h'
l!l
00
MC/DC0%0/1
$in[3] || '+1h'
$in[3]
Truth table0%0/2
A
A= $in[3]
Aresult
11
00
316 0 ▶ my $sidkey = $in[4] || SIDKEY;
Condition0%0/2
$in[4] || 'usid'
l!l
00
MC/DC0%0/1
$in[4] || 'usid'
$in[4]
Truth table0%0/2
A
A= $in[4]
Aresult
11
00
317 0 ▶ croak("Can't define DSN") unless $dsn;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn 0 0 0
318
319 0 my $status = 0;
320 0 my $reason = "ERROR";
321 0 my $error = _translate($reason);
322
323 0 CGI::Session->name($sidkey);
324 0 my $session = CGI::Session->load($dsn, $sid, $args);
325 0 ▶ if ($sid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sid) { } 0 0 0
326 # Áûë ïåðåäàí SID çíà÷èò îæèäàåì êàêóþ-íèáóäü ñåññèþ!
327 0 ▶ if ($session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) { } 0 0 0
328 #my $session = new CGI::Session($dsn, undef, $args);
329 0 ▶ if ( $session->is_expired ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->is_expired) { } 0 0 0
330 0 $reason = "TIMEOUT";
331 0 $error = "Session timed out (expired)";
332 0 $sid = undef;
333 } else {
334 0 ▶ if ( $session->is_empty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->is_empty) { } 0 0 0
335 #carp(">>>>>!!!! SESSION EMPTY !!!<<<");
336 0 $reason = "TIMEOUT";
337 0 $error = "Session timed out (empty)";
338 0 $sid = undef;
339 } else {
340 0 $status = 1;
341 0 $reason = "OK";
342 0 $error = '';
343 0 ▶ $sid = $session->id() || undef;
Condition0%0/2
$session->id || undef
l!l
00
MC/DC0%0/1
$session->id || undef
$session->id
Truth table0%0/2
A
A= $session->id
Aresult
11
00
344 }
345 }
346 } else {
347 0 $error = CGI::Session->errstr();
348 0 carp(sprintf(__PACKAGE__." LOAD ERROR>", $error));
349 0 $sid = undef;
350 }
351 } else {
352 # Íå ñîçäà¸ì ñåññèþ, åñëè íå ïåðåäàí USID. Ïðè àâòîðèçàöèè ñîçäà¸ì ñåññèþ îòäåëüíî
353 0 $error = "";
354 0 ▶ unless ($session) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session) 0 0 0
355 0 $error = CGI::Session->errstr();
356 0 carp(sprintf(__PACKAGE__." LOAD ERROR>", $error));
357 }
358 0 return bless {
359 status => 0,
360 reason => "AUTH_REQUIRED",
361 error => $error,
362 session => $session,
363 expire => $expires, # Îðèãèíàëüíîå çíà÷åíèå
364 expires => $class->toexpire($expires), # Ïðåîáðàçîâàííîå (ïåðåñ÷èòàííîå) çíà÷åíèå
365 $sidkey => undef,
366 sidkey => $sidkey,
367 predata => {},
368 }, $class;
369
370 }
371 0 my $self = bless {
372 status => $status,
373 reason => $reason,
374 error => $error,
375 session => $session,
376 expire => $expires, # îðèãèíàëüíîå çíà÷åíèå
377 expires => $class->toexpire($expires), # ñåññèÿ ñ ìîìåíòà ïîñëåäíåãî äîñòóïà ê íåé!
378 $sidkey => $sid, # ñîçäàííûé USID (âñòàâëÿåòñÿ åñëè óäà÷íî ñîçäàëäñÿ èëè ïðî÷èòàëñÿ)
379 sidkey => $sidkey,
380 predata => {},
381 }, $class;
382
383
384 #
385 # !!! Èñïîëüçîâàòü ìåòîä expire ó îáúåêòà CGI::Session; åãî íóæíî óñòàíîâèòü êàê äëÿ êóêè, òàê è
386 # !!! äëÿ ñàìîé ñåññèè, îíî óäàëèòñÿ ñàìî. Óñòàíàâëèâàòü äàííîå çíà÷åíèå íóæíî òîëüêî ïðè ëîãèíå
387 #
388 #$self->init if $status && $reason eq "NEW";
389 0 return $self;
390 }
391 sub init { # Èíèöèàëèçàöèÿ ñòàòèñòè÷åñêèõ äàííûõ â ÑÅÑÑÈÈ
392 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
393 0 my $session = $self->{session};
394 0 $session->param("ctime", time()); # Âðåìÿ ñîçäàíèÿ
395 0 $session->param("atime", time()); # Âðåìÿ äîñòóïà
396 0 $session->param("expires", $self->{expires}); # Âðåìÿ êàñòîìèçèðîâàííîå äëÿ äàííîé ñåññèè
397 0 $session->param("data", $self->{predata});
398 0 return 1;
399 }
400 sub update { # Îáíîâëåíèå ñòàòèñòè÷åñêèõ äàííûõ â ÑÅÑÑÈÈ
401 0 ▶ my $self = shift;
Subroutine update: not called CC=1 SCAR=6.9
402 0 my $session = $self->{session};
403
404 0 $session->param("atime", time()); # Âðåìÿ äîñòóïà
405 0 return 1;
406 }
407 sub authen { # AAA-authen
408 #
409 # Àóòåíòèôèêàöèÿ. Ïðîâåðêà - ïðàâèëüíî ëè ââåäåíû ëîãèí è ïàðîëü
410 #
411 # Ìîæåò ïðèíèìàòü çíà÷åíèÿ:
412 # LOGIN_INCORRECT / PASSWORD_INCORRECT / DECLINED / OK
413 #
414
415 0 ▶ my $self = shift;
Subroutine authen: not called CC=4 SCAR=30.0
416 0 my $callback = shift;
417
418 # !!! callback here !!!
419 0 ▶ if ($callback && ref($callback) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback and ref $callback eq "CODE") 0 0 0
Condition0%0/3
$callback and ref $callback eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$callback and ref $callback eq "CODE"
$callback ref $callback eq "CODE"
Truth table0%0/3
A and B
A= $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
420 0 ▶ return 0 unless $callback->($self,@_);
Branch: 1 branch
BranchTrueFalseTotal
unless &$callback($self, @_) 0 0 0
421 }
422
423 0 $self->status(1);
424 0 $self->reason("OK");
425 0 $self->error('');
426
427 0 return 1;
428 }
429 sub authz { # AAA-authz
430 #
431 # Àâòîðèçàöèÿ. Ïðîâåðêà ðîëåé è áàí-ëèñòîâ óðîâíÿ ÁÄ
432 #
433 # Ìîæåò ïðèíèìàòü çíà÷åíèÿ:
434 # UNAUTHORIZED / FORBIDDEN / OK
435 #
436
437 0 ▶ my $self = shift;
Subroutine authz: not called CC=8 SCAR=42.8
438 0 my $callback = shift;
439 0 my $_session = $self->{session};
440
441 # !!! callback here !!!
442 0 ▶ if ($callback && ref($callback) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback and ref $callback eq "CODE") 0 0 0
Condition0%0/3
$callback and ref $callback eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$callback and ref $callback eq "CODE"
$callback ref $callback eq "CODE"
Truth table0%0/3
A and B
A= $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
443 0 ▶ return 0 unless $callback->($self,@_);
Branch: 1 branch
BranchTrueFalseTotal
unless &$callback($self, @_) 0 0 0
444 }
445
446 # Àâòîðèçàöèÿ ïðîøëà óñïåøíî. Ìîæíî ñîçäàâàòü ñåññèþ
447 #return 0 unless $self->init(undef,1);
448 0 my $session;
449 0 ▶ if ($self->sid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sid) { } 0 0 0
450 0 $session = $_session;
451 } else {
452 0 $session = $_session->new();
453 0 ▶ $self->init if $session;
Branch: 1 branch
BranchTrueFalseTotal
if $session 0 0 0
454 }
455 0 ▶ if ($session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session) 0 0 0
456 0 $session->expire($self->{expire});
457 0 $self->status(1);
458 0 $self->reason("NEW");
459 0 $self->error('');
460 0 ▶ $self->{$self->{sidkey}} = $session->id() || undef;
Condition0%0/2
$session->id || undef
l!l
00
MC/DC0%0/1
$session->id || undef
$session->id
Truth table0%0/2
A
A= $session->id
Aresult
11
00
461 0 return 1;
462 }
463
464 0 my $error = $session->errstr();
465 0 carp(sprintf(__PACKAGE__." NEW ERROR>", $error));
466 0 $self->status(0);
467 0 $self->reason("UNAUTHORIZED");
468 0 $self->error($error);
469 0 $self->{sid} = undef;
470
471 0 return 0;
472
473 }
474 sub access { # AAA-access
475 #
476 # Ïðîâåðêà äàííûõ ñåññèè íà ïðåäìåò ðàçðåøåíèÿ äîñòóïà ê ðàñøèðåííîìó ôóíêöèîíàëó
477 # Áàí-ëèñòû è ïðî÷åå
478 #
479
480 0 ▶ my $self = shift;
Subroutine access: not called CC=6 SCAR=37.4
481 0 my $callback = shift;
482
483 # Ïðîâåðêà - à åñòü ëè îøèáêè ïðè èíèöèàëèçàöèè ??
484 0 ▶ return 0 unless $self->status;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status 0 0 0
485
486 # Ïðîâåðêà expires è îáíîâëåíèå äàííûõ ïîñëåäíåãî äîñòóïà.
487 0 my $expires = $self->get_expires;
488 0 my $lastaccess = $self->get_atime;
489 0 my $newaccess = time();
490 #carp(">> expires: $expires; lastaccess: $lastaccess; newaccess: $newaccess");
491 0 my $accessto = (($newaccess - $lastaccess) > $expires); # true - timeout
492 0 ▶ if ($accessto) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accessto) 0 0 0
493 0 $self->delete(); # Óäàëÿåì åñëè âðåìÿ èñòåêëî
494 0 $self->status(0);
495 0 $self->reason("TIMEOUT");
496 0 $self->error(_translate("TIMEOUT"));
497 0 return 0;
498 }
499
500 # !!! callback here !!!
501 0 ▶ if ($callback && ref($callback) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback and ref $callback eq "CODE") 0 0 0
Condition0%0/3
$callback and ref $callback eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$callback and ref $callback eq "CODE"
$callback ref $callback eq "CODE"
Truth table0%0/3
A and B
A= $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
502 0 ▶ return 0 unless $callback->($self,@_);
Branch: 1 branch
BranchTrueFalseTotal
unless &$callback($self, @_) 0 0 0
503 }
504
505 # Âñå ïðîâåðêè ïðîøëè óñïåøíî. Äîñòóï ðàçðåøåí!
506 0 $self->update; # îáíîâëÿåì âðåìÿ äîñòóïà
507 0 $self->status(1);
508 0 $self->reason("OK");
509 0 $self->error('');
510 0 return 1;
511 }
512
513 sub sid { # Ïîëó÷åíèå USID
514 0 ▶ my $self = shift;
Subroutine sid: not called CC=2 SCAR=17.9
515 0 ▶ return $self->{$self->{sidkey}} || undef;
Condition0%0/2
$$self{$self->{'sidkey'}} || undef
l!l
00
MC/DC0%0/1
$$self{$self->{'sidkey'}} || undef
$$self{$self->{'sidkey'}}
Truth table0%0/2
A
A= $$self{$self->{'sidkey'}}
Aresult
11
00
516 }
517 0 ▶sub usid { goto &sid }
Subroutine usid: not called CC=1 SCAR=6.9
518 sub get_expires { # Ïîëó÷åíèå expires èç ñåññèè
519 0 ▶ my $self = shift;
Subroutine get_expires: not called CC=5 SCAR=34.0
520 0 my $session = $self->{session};
521 0 ▶ if (defined $session) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session) 0 0 0
522 0 ▶ return $session->param("expires") || $self->{expires} || 0;
Condition0%0/5
$session->param('expires') || $self->{'expires'} || 0
l!l
00
$session->param("expires") or $self->{"expires"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$session->param('expires') || $self->{'expires'} || 0
$session->param("expires") $self->{"expires"}
Truth table0%0/3
A or B
A= $session->param("expires")B= $self->{"expires"}
ABresult
1X1
011
000
523 }
524 0 ▶ return $self->{expires} || 0;
Condition0%0/2
$self->{'expires'} || 0
l!l
00
MC/DC0%0/1
$self->{'expires'} || 0
$self->{'expires'}
Truth table0%0/2
A
A= $self->{'expires'}
Aresult
11
00
525 }
526 sub get_ctime { # Ïîëó÷åíèå ctime èç ñåññèè
527 0 ▶ my $self = shift;
Subroutine get_ctime: not called CC=3 SCAR=24.8
528 0 my $session = $self->{session};
529 0 ▶ return 0 unless defined $session;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session 0 0 0
530 0 ▶ return $session->param("ctime") || 0;
Condition0%0/2
$session->param('ctime') || 0
l!l
00
MC/DC0%0/1
$session->param('ctime') || 0
$session->param('ctime')
Truth table0%0/2
A
A= $session->param('ctime')
Aresult
11
00
531 }
532 sub get_atime { # Ïîëó÷åíèå atime èç ñåññèè
533 0 ▶ my $self = shift;
Subroutine get_atime: not called CC=3 SCAR=24.8
534 0 my $session = $self->{session};
535 0 ▶ return 0 unless defined $session;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session 0 0 0
536 0 ▶ return $session->param("atime") || 0;
Condition0%0/2
$session->param('atime') || 0
l!l
00
MC/DC0%0/1
$session->param('atime') || 0
$session->param('atime')
Truth table0%0/2
A
A= $session->param('atime')
Aresult
11
00
537 }
538 sub get_data { # Ïîëó÷åíèå data èç ñåññèè êàê õýø (ïîëíîñòüþ âñå äàííûå)
539 0 ▶ my $self = shift;
Subroutine get_data: not called CC=3 SCAR=24.8
540 0 my $session = $self->{session};
541 0 ▶ return undef unless defined $session;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session 0 0 0
542 0 ▶ return $session->param("data") || {};
Condition0%0/2
$session->param('data') || {}
l!l
00
MC/DC0%0/1
$session->param('data') || {}
$session->param('data')
Truth table0%0/2
A
A= $session->param('data')
Aresult
11
00
543 }
544 sub status { # Ïîëó÷åíèå/óñòàíîâêà ñòàòóñà
545 0 ▶ my $self = shift;
Subroutine status: not called CC=2 SCAR=17.9
546 0 my $ns = shift;
547 0 ▶ $self->{status} = fv2zero($ns) if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 0 0 0
548 0 return $self->{status};
549 }
550 sub reason { # Ïîëó÷åíèå/óñòàíîâêà ïðè÷èíû
551 0 ▶ my $self = shift;
Subroutine reason: not called CC=2 SCAR=17.9
552 0 my $nr = shift;
553 0 ▶ $self->{reason} = fv2null($nr) if defined $nr;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nr 0 0 0
554 0 return $self->{reason};
555 }
556 sub error { # Ïîëó÷åíèå/óñòàíîâêà îøèáêè
557 0 ▶ my $self = shift;
Subroutine error: not called CC=2 SCAR=17.9
558 0 my $ne = shift;
559 0 ▶ $self->{error} = fv2null($ne) if defined $ne;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ne 0 0 0
560 0 return $self->{error};
561 }
562 sub get { # Ïîëó÷åíèå óêàçàííîãî êëþ÷à èç ïîëüçîâàòåëüñêèõ äàííûõ ñåññèè
563 0 ▶ my $self = shift;
Subroutine get: not called CC=6 SCAR=37.4
564 0 ▶ my $key = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
565 0 my $session = $self->{session};
566 0 my $data = $self->get_data;
567 0 my $value;
568 0 ▶ if ($data && (ref($data) eq 'HASH') && !$session->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data eq 'HASH' and not $session->is_empty) { } 0 0 0
Condition0%0/6
$data and ref $data eq "HASH" and not $session->is_empty
!ll&&!rl&&r
000
$data and ref $data eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$data and ref $data eq "HASH" and not $session->is_empty
$data ref $data eq "HASH" not $session->is_empty
Truth table0%0/4
A and B and not(C)
A= $dataB= ref $data eq "HASH"C= not $session->is_empty
ABCresult
0XX0
10X0
1100
1111
569 0 $value = $data->{$key};
570 } else { # Áåð¸ì èç âðåìåííîé òàáëè÷êè
571 0 $value = $self->{predata}->{$key};
572 }
573 0 ▶ return defined($value) ? $value : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
574 }
575 sub set { # Çàïèñü óêàçàííîãî êëþ÷à â ïîëüçîâàòåëüñêèå äàííûå ñåññèè
576 0 ▶ my $self = shift;
Subroutine set: not called CC=5 SCAR=34.0
577 0 ▶ my $key = shift || return 0;
Condition0%0/2
shift() || (return 0)
l!l
00
MC/DC0%0/1
shift() || (return 0)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
578 0 my $value = shift;
579 0 my $session = $self->{session};
580 0 my $data = $self->get_data;
581 0 ▶ if ($data && (ref($data) eq 'HASH') && !$session->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data eq 'HASH' and not $session->is_empty) { } 0 0 0
Condition0%0/6
$data and ref $data eq "HASH" and not $session->is_empty
!ll&&!rl&&r
000
$data and ref $data eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
$data and ref $data eq "HASH" and not $session->is_empty
$data ref $data eq "HASH" not $session->is_empty
Truth table0%0/4
A and B and not(C)
A= $dataB= ref $data eq "HASH"C= not $session->is_empty
ABCresult
0XX0
10X0
1100
1111
582 0 $data->{$key} = $value;
583 0 $session->param("data", $data);
584 0 return 1;
585 } else { # Ñîõðàíÿåì âî âðåìåííóþ òàáëè÷êó
586 0 $data = $self->{predata};
587 0 $data->{$key} = $value;
588 0 return 1;
589 }
590 0 return 0;
591 }
592 sub delete { # Óäàëåíèå ñåññèè
593 0 ▶ my $self = shift;
Subroutine delete: not called CC=4 SCAR=30.0
594 0 my $session = $self->{session};
595 0 ▶ if ($self->status && $session && $session->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->status and $session and $session->id) 0 0 0
Condition0%0/6
$self->status and $session and $session->id
!ll&&!rl&&r
000
$self->status and $session
!ll&&!rl&&r
000
MC/DC0%0/3
$self->status and $session and $session->id
$self->status $session $session->id
Truth table0%0/4
A and B and C
A= $self->statusB= $sessionC= $session->id
ABCresult
0XX0
10X0
1100
1111
596 0 $session->delete;
597 }
598 0 $self->{session} = undef ;
599 0 $self->status(0);
600 0 $self->reason("UNAUTHORIZED");
601 0 $self->error(_translate("UNAUTHORIZED"));
602 0 return 1;
603 }
604 sub reason_translate { # Ïåðåâîä îïèñàíèÿ reason íà ðóññêèé ÿçûê ñ ñ ðàñøèôðîâêîé
605 0 ▶ my $self = shift;
Subroutine reason_translate: not called CC=2 SCAR=17.9
606 0 ▶ my $reason = shift || $self->reason();
Condition0%0/3
shift() || $self->reason
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->reason
shift() $self->reason
Truth table0%0/3
A || B
A= shift()B= $self->reason
ABresult
1X1
011
000
607 0 return _translate($reason);
608 }
609 sub toexpire { # Ïåðåâîä â expires
610 my $self = shift;
611 my $str = shift || 0;
612
613 return 0 unless defined $str;
614 return $1 if $str =~ m/^[-+]?(\d+)$/;
615
616 my %_map = (
617 s => 1,
618 m => 60,
619 h => 3600,
620 d => 86400,
621 w => 604800,
622 M => 2592000,
623 y => 31536000
624 );
625
626 my ($koef, $d) = $str =~ m/^([+-]?\d+)([smhdwMy])$/;
627 unless ( defined($koef) && defined($d) ) {
628 croak "toexpire(): couldn't parse '$str' into \$koef and \$d parts. Possible invalid syntax";
629 }
630 return $koef * $_map{ $d };
631 }
632 sub _translate { # Ïåðåâîä îïèñàíèÿ reason íà ðóññêèé ÿçûê ñ ñ ðàñøèôðîâêîé
633 my $reason = shift || 'DEFAULT';
634 my $transtable = STAT;
635
636 return $transtable->{DEFAULT} unless
637 grep {$_ eq 'DEFAULT'} keys %$transtable;
638
639 return $transtable->{$reason};
640 }
641 sub DESTROY {
642 my $self = shift;
643 my $session = $self->{session};
644 $session->flush() if $session;
645 undef $self;
646 }
647
648 1;
649
650 __END__
« MLite/Helper/Skel.pm ↑ Summary MLite/Util.pm »