2016-04-20 07:09:23 +02:00
|
|
|
package Lemonldap::NG::Portal::UserDB::LDAP;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
2016-05-22 14:22:59 +02:00
|
|
|
use Lemonldap::NG::Portal::Main::Constants
|
|
|
|
qw(PE_OK PE_LDAPCONNECTFAILED PE_LDAPERROR PE_BADCREDENTIALS);
|
2016-04-20 07:09:23 +02:00
|
|
|
|
2017-01-15 14:18:01 +01:00
|
|
|
extends 'Lemonldap::NG::Portal::Lib::LDAP';
|
2016-04-20 07:09:23 +02:00
|
|
|
|
2016-04-28 22:07:35 +02:00
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
|
|
|
has ldapGroupAttributeNameSearch => (
|
|
|
|
is => 'rw',
|
|
|
|
builder => sub {
|
2016-05-07 13:27:08 +02:00
|
|
|
return $_[0]->{conf}->{ldapGroupAttributeNameSearch}
|
|
|
|
? [
|
2016-04-28 22:07:35 +02:00
|
|
|
split( /\s+/, $_[0]->{conf}->{ldapGroupAttributeNameSearch} ),
|
|
|
|
|
|
|
|
# Push group attribute value for recursive search
|
|
|
|
(
|
|
|
|
$_[0]->{conf}->{ldapGroupRecursive}
|
|
|
|
and $_[0]->{conf}->{ldapGroupAttributeNameGroup} ne "dn"
|
|
|
|
? $_[0]->{conf}->{ldapGroupAttributeNameGroup}
|
|
|
|
: ()
|
|
|
|
)
|
2016-05-07 13:27:08 +02:00
|
|
|
]
|
|
|
|
: [];
|
2016-04-28 22:07:35 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
has attrs => (
|
|
|
|
is => 'rw',
|
|
|
|
builder => sub {
|
|
|
|
return [
|
|
|
|
values %{ $_[0]->{conf}->{exportedVars} },
|
|
|
|
values %{ $_[0]->{conf}->{ldapExportedVars} }
|
|
|
|
];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-06-09 20:40:20 +02:00
|
|
|
# RUNNING METHODS
|
|
|
|
|
2016-04-28 22:07:35 +02:00
|
|
|
sub getUser {
|
|
|
|
my ( $self, $req ) = @_;
|
2016-05-11 13:42:37 +02:00
|
|
|
return PE_LDAPCONNECTFAILED unless $self->ldap and $self->bind();
|
2016-04-28 22:07:35 +02:00
|
|
|
my $mesg = $self->ldap->search(
|
|
|
|
base => $self->conf->{ldapBase},
|
|
|
|
scope => 'sub',
|
|
|
|
filter => $self->filter->($req),
|
|
|
|
defer => $self->conf->{ldapSearchDeref} || 'find',
|
|
|
|
attrs => $self->attrs,
|
|
|
|
);
|
|
|
|
if ( $mesg->code() != 0 ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error( 'LDAP Search error: ' . $mesg->error );
|
2016-04-28 22:07:35 +02:00
|
|
|
return PE_LDAPERROR;
|
|
|
|
}
|
|
|
|
if ( $mesg->count() > 1 ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error('More than one entry returned by LDAP directory');
|
2016-04-28 22:07:35 +02:00
|
|
|
return PE_BADCREDENTIALS;
|
|
|
|
}
|
2016-05-12 21:02:48 +02:00
|
|
|
unless ( $req->datas->{entry} = $mesg->entry(0) ) {
|
2016-04-28 22:07:35 +02:00
|
|
|
my $user = $req->{mail} || $req->{user};
|
2017-02-15 15:16:59 +01:00
|
|
|
$self->userLogger->warn("$user was not found in LDAP directory");
|
2016-04-28 22:07:35 +02:00
|
|
|
return PE_BADCREDENTIALS;
|
|
|
|
}
|
|
|
|
$req->datas->{dn} = $req->datas->{entry}->dn();
|
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Load all parameters included in exportedVars parameter.
|
|
|
|
# Multi-value parameters are loaded in a single string with
|
|
|
|
# a separator (param multiValuesSeparator)
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub setSessionInfo {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
$req->{sessionInfo}->{dn} = $req->datas->{dn};
|
|
|
|
|
|
|
|
my %vars = ( %{ $self->conf->{exportedVars} },
|
|
|
|
%{ $self->conf->{ldapExportedVars} } );
|
|
|
|
while ( my ( $k, $v ) = each %vars ) {
|
2017-03-02 07:35:15 +01:00
|
|
|
$req->sessionInfo->{$k} =
|
2016-04-28 22:07:35 +02:00
|
|
|
$self->{ldap}->getLdapValue( $req->datas->{entry}, $v ) || "";
|
|
|
|
}
|
|
|
|
|
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Load all groups in $groups.
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub setGroups {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my $groups = $req->{sessionInfo}->{groups};
|
|
|
|
my $hGroups = $req->{sessionInfo}->{hGroups};
|
|
|
|
|
|
|
|
if ( $self->conf->{ldapGroupBase} ) {
|
|
|
|
|
|
|
|
# Get value for group search
|
|
|
|
my $group_value = $self->ldap->getLdapValue( $req->datas->{entry},
|
|
|
|
$self->conf->{ldapGroupAttributeNameUser} );
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug( "Searching LDAP groups in "
|
2016-04-28 22:07:35 +02:00
|
|
|
. $self->{ldapGroupBase}
|
2017-02-15 07:41:50 +01:00
|
|
|
. " for $group_value" );
|
2016-04-28 22:07:35 +02:00
|
|
|
|
|
|
|
# Call searchGroups
|
|
|
|
my $ldapGroups = $self->ldap->searchGroups(
|
|
|
|
$self->conf->{ldapGroupBase},
|
|
|
|
$self->conf->{ldapGroupAttributeName},
|
|
|
|
$group_value, $self->ldapGroupAttributeNameSearch
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( keys %$ldapGroups ) {
|
|
|
|
my $groupName = $_;
|
|
|
|
$hGroups->{$groupName} = $ldapGroups->{$groupName};
|
|
|
|
my $groupValues = [];
|
|
|
|
foreach ( @{ $self->ldapGroupAttributeNameSearch } ) {
|
|
|
|
next if $_ =~ /^name$/;
|
|
|
|
my $firstValue = $ldapGroups->{$groupName}->{$_}->[0];
|
|
|
|
push @$groupValues, $firstValue;
|
|
|
|
}
|
|
|
|
$groups .=
|
|
|
|
$self->conf->{multiValuesSeparator} . join( '|', @$groupValues );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-03 13:17:15 +01:00
|
|
|
$req->{sessionInfo}->{groups} = $groups;
|
2016-04-28 22:07:35 +02:00
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-20 07:09:23 +02:00
|
|
|
1;
|