| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | } | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | }; | ||||||||||||||
| 56 | 0 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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'} || {}
MC/DC0%0/1
$res->{'info'} || {}
$res->{'info'}
Truth table0%0/2
A
A= $res->{'info'}
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 104 | 0 | $self->logger->error("UserDB REST error: $@"); | |||||||||||||
| 105 | 0 | return PE_ERROR; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | 0 | ▶ | unless ( $res->{result} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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; | |||||||||||||