blib/lib/Lemonldap/NG/Portal/UserDB/REST.pm

filter: Statementstmt 26.7% 19 / 71 Branchbran 4.5% 1 / 22 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 4 totaltotal 22.9% 25 / 109 CC 14 SCAR 31.9
CC
14
Cov
21%
getUser
42.0
findUser
42.0
__ANON__
6.0
CRAP 24.4
« blib/lib/Lemonldap/NG/Portal/Lib/Key.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/REST.pm »
1 package Lemonldap::NG::Portal::UserDB::REST;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 use Lemonldap::NG::Portal::Main::Constants qw(
7 PE_OK
8 PE_ERROR
9 PE_USERNOTFOUND
10 PE_BADCREDENTIALS
11 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 extends qw(
14 Lemonldap::NG::Portal::Main::UserDB
15 Lemonldap::NG::Portal::Lib::REST
16 );
17  
18 3 our $VERSION = '2.19.0';
19  
20 # INITIALIZATION
21  
22 has findUserDBUrl => (
23 is => 'ro',
24 lazy => 1,
25 default => sub {
26 0 ▶ $_[0]->conf->{restFindUserDBUrl} || $_[0]->conf->{restUserDBUrl};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conf->{"restFindUserDBUrl"} or $_[0]->conf->{"restUserDBUrl"} 0 0 0
27 }
28 3 );
29  
30 sub init {
31 2 ▶ my $self = shift;
Subroutine init: called CC=2 SCAR=8.4
Pod undocumented
32  
33 2 ▶ unless ( $self->conf->{restUserDBUrl} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"restUserDBUrl"}) 0 2 2
34 0 $self->logger->error('REST User data URL is not set');
35 0 return 0;
36 }
37  
38 2 return 1;
39 }
40  
41 # RUNNING METHODS
42  
43 sub getUser {
44 0 ▶ my ( $self, $req, %args ) = @_;
Subroutine getUser: not called CC=6 SCAR=37.4
Pod undocumented
45 0 my $res;
46 0 $res = eval {
47 $self->restCall(
48 $self->conf->{restUserDBUrl},
49 {
50 ( $args{useMail} ? 'mail' : 'user' ) => $req->user,
51 0 ▶ 'useMail' => ( $args{useMail} ? JSON::true : JSON::false ),
Branch: 2 branches
BranchTrueFalseTotal
$args{'useMail'} ? : 0 0 0
$args{'useMail'} ? : 0 0 0
52  
53 }
54 );
55 };
56 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
57 0 $self->logger->error("UserDB REST error: $@");
58 0 eval { $self->p->_authentication->setSecurity($req) };
59 0 return PE_ERROR;
60 }
61 0 ▶ unless ( $res->{result} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"result"}) 0 0 0
62 0 $self->userLogger->warn( 'User ' . $req->user . ' not found' );
63 0 eval { $self->p->_authentication->setSecurity($req) };
64 0 return PE_BADCREDENTIALS;
65 }
66 0 ▶ $req->data->{restUserDBInfo} = $res->{info} || {};
Condition0%0/2
$res->{'info'} || {}
l!l
00
MC/DC0%0/1
$res->{'info'} || {}
$res->{'info'}
Truth table0%0/2
A
A= $res->{'info'}
Aresult
11
00
67  
68 0 return PE_OK;
69 }
70  
71 sub findUser {
72 0 ▶ my ( $self, $req, %args ) = @_;
Subroutine findUser: not called CC=6 SCAR=37.4
Pod undocumented
73 my $plugin =
74 0 $self->p->loadedModules->{"Lemonldap::NG::Portal::Plugins::FindUser"};
75 0 my ( $searching, $excluding ) = $plugin->retreiveFindUserParams($req);
76 0 eval { $self->p->_authentication->setSecurity($req) };
77 0 ▶ return PE_OK unless scalar @$searching;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$searching 0 0 0
78  
79 0 my $res;
80 $searching = [
81 map {
82 0 { $_->{key} => $_->{value} }
83 0 } @$searching
84 ];
85 $excluding = [
86 map {
87 0 { $_->{key} => $_->{value} }
88 0 } @$excluding
89 ];
90 0 $res = eval {
91 0 ▶ $self->restCall(
Branch: 1 branch
BranchTrueFalseTotal
scalar @$excluding ? : 0 0 0
92 $self->findUserDBUrl,
93 {
94 searchingAttributes => to_json($searching),
95 (
96 scalar @$excluding
97 ? ( excludingAttributes => to_json($excluding) )
98 : ()
99 )
100 }
101 );
102 };
103 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
104 0 $self->logger->error("UserDB REST error: $@");
105 0 return PE_ERROR;
106 }
107 0 ▶ unless ( $res->{result} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"result"}) 0 0 0
108 0 $self->userLogger->info('FindUser: no user found from REST UserDB');
109 0 return PE_USERNOTFOUND;
110 }
111  
112 0 my $results = $res->{users};
113 0 $self->logger->debug(
114 'REST UserDB number of result(s): ' . scalar @$results );
115 0 ▶ if ( scalar @$results ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$results) 0 0 0
116 0 my $rank = int( rand( scalar @$results ) );
117 0 $self->logger->debug("REST UserDB random rank: $rank");
118 0 $self->userLogger->info(
119 "FindUser: REST UserDB returns $results->[$rank]");
120 0 $req->data->{findUser} = $results->[$rank];
121 0 $req->data->{findUserAll} = $results;
122 0 return PE_OK;
123 }
124  
125 0 return PE_USERNOTFOUND;
126 }
127  
128 sub setSessionInfo {
129 0 ▶ my ( $self, $req ) = @_;
Subroutine setSessionInfo: not called CC=2 SCAR=17.9
Pod undocumented
130 $req->sessionInfo->{$_} = $req->data->{restUserDBInfo}->{$_}
131 0 foreach ( keys %{ $req->data->{restUserDBInfo} } );
132  
133 0 return PE_OK;
134 }
135  
136 3 1;
« blib/lib/Lemonldap/NG/Portal/Lib/Key.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/REST.pm »