<commit_before># --
# Kernel/System/CustomerUser/LDAP.pm - some customer user functions in LDAP
# Copyright (C) 2002 <NAME> <<EMAIL>>
# Copyright (C) 2002-2003 <NAME> <<EMAIL>>
# --
# $Id: LDAP.pm,v 1.12 2003-05-20 20:08:59 martin Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --

package Kernel::System::CustomerUser::LDAP;

use strict;
use Net::LDAP;

use vars qw(@ISA $VERSION);
$VERSION = '$Revision: 1.12 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;

# --
sub new {
    my $Type = shift;
    my %Param = @_;

    # allocate new hash for object
    my $Self = {};
    bless ($Self, $Type);
    # --
    # check needed objects
    # --
    foreach (qw(DBObject ConfigObject LogObject PreferencesObject)) {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }
    # --
    # max shown user a search list
    # --
    $Self->{UserSearchListLimit} = 200;
    # --
    # get ldap preferences
    # --
    $Self->{Host} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'Host'}
     || die "Need CustomerUser->Params->Host in Kernel/Config.pm";
    $Self->{BaseDN} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'BaseDN'}
     || die "Need CustomerUser->Params->BaseDN in Kernel/Config.pm";
    $Self->{SScope} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'SSCOPE'}
     || die "Need CustomerUser->Params->SSCOPE in Kernel/Config.pm";
    $Self->{SearchUserDN} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'UserDN'} || '';
    $Self->{SearchUserPw} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'UserPW'} || '';

    $Self->{CustomerKey} = $Self->{ConfigObject}->Get('CustomerUser')->{'CustomerKey'}
     || die "Need CustomerUser->CustomerKey in Kernel/Config.pm";
    $Self->{CustomerID} = $Self->{ConfigObject}->Get('CustomerUser')->{'CustomerID'}
     || die "Need CustomerUser->CustomerID in Kernel/Config.pm";

    # --
    # ldap connect and bind (maybe with SearchUserDN and SearchUserPw)
    # --
    $Self->{LDAP} = Net::LDAP->new($Self->{Host}) or die "$@";
    if (!$Self->{LDAP}->bind(dn => $Self->{SearchUserDN}, password => $Self->{SearchUserPw})) {
        $Self->{LogObject}->Log(
          Priority => 'error',
          Message => "First bind failed!",
        );
        return;
    }
    return $Self;
}
# --
sub CustomerName {
    my $Self = shift;
    my %Param = @_;
    my $Name = '';
    # --
    # check needed stuff
    # --
    if (!$Param{UserLogin}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need UserLogin!");
        return;
    }
    # --
    # build filter
    # --
    my $Filter = "($Self->{CustomerKey}=$Param{UserLogin})";
    # --
    # perform user search
    # --
    my $Result = $Self->{LDAP}->search(
        base => $Self->{BaseDN},
        scope => $Self->{SScope},
        filter => $Filter, 
        sizelimit => $Self->{UserSearchListLimit},
    );
    foreach my $entry ($Result->all_entries) {
        foreach (@{$Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserNameFields}}) {
            if (!$Name) {
                $Name = $entry->get_value($_);
            }
            else {
                $Name .= ' '.$entry->get_value($_);
            }
        }
    }
    return $Name;
}   
# --
sub CustomerSearch {
    my $Self = shift;
    my %Param = @_;
    # --
    # check needed stuff
    # --
    if (!$Param{Search} && !$Param{UserLogin} && !$Param{PostMasterSearch}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need Search, UserLogin or PostMasterSearch!");
        return;
    }
    # --
    # build filter
    # --
    my $Filter = '';
    if ($Param{Search}) {
        if ($Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserSearchFields}) {
            $Filter = '(|';
            foreach (@{$Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserSearchFields}}) {
                $Filter.= "($_=$Param{Search})";
            }
            $Filter .= ')';
        }
        else {
            $Filter = "($Self->{CustomerKey}=$Param{Search})";
        }
    }
    elsif ($Param{PostMasterSearch}) {
        if ($Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserPostMasterSearchFields}) {
            $Filter = '(|';
            foreach (@{$Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserPostMasterSearchFields}}) {
                $Filter.= "($_=$Param{PostMasterSearch})";
            }
            $Filter .= ')';
        }
    }
    elsif ($Param{UserLogin}) {
        $Filter = "($Self->{CustomerKey}=$Param{UserLogin})";
    }
    # --
    # perform user search
    # --
    my $Result = $Self->{LDAP}->search(
        base => $Self->{BaseDN},
        scope => $Self->{SScope},
        filter => $Filter, 
        sizelimit => $Self->{UserSearchListLimit},
    );
    # --
    # log ldap errors 
    # --
    if ($Result->code()) {
        $Self->{LogObject}->Log(Priority => 'error', Message => $Result->error());
    }
    my %Users = ();
    foreach my $entry ($Result->all_entries) {
        my $CustomerString = '';
        foreach (@{$Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserListFields}}) {
            $CustomerString .= $entry->get_value($_).' ';
        }
        $CustomerString =~ s/^(.*\s)(.+?\@.+?\..+?)(\s|)$/"$1" <$2>/;
        $Users{$entry->get_value($Self->{CustomerKey})} = $CustomerString;
    }
    return %Users;
}   
# --
sub CustomerUserList {
    my $Self = shift;
    my %Param = @_;
    my $Valid = defined $Param{Valid} ? $Param{Valid} : 1;
    # --
    # perform user search
    # --
    my $Result = $Self->{LDAP}->search (<commit_msg>fixed bug 178 - Authenticated customer LDAP requests dont work<commit_after># --
# Kernel/System/CustomerUser/LDAP.pm - some customer user functions in LDAP
# Copyright (C) 2002 <NAME> <<EMAIL>>
# Copyright (C) 2002-2003 <NAME> <<EMAIL>>
# --
# $Id: LDAP.pm,v 1.13 2003-07-04 11:12:10 martin Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --

package Kernel::System::CustomerUser::LDAP;

use strict;
use Net::LDAP;

use vars qw(@ISA $VERSION);
$VERSION = '$Revision: 1.13 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;

# --
sub new {
    my $Type = shift;
    my %Param = @_;

    # allocate new hash for object
    my $Self = {};
    bless ($Self, $Type);
    # --
    # check needed objects
    # --
    foreach (qw(DBObject ConfigObject LogObject PreferencesObject)) {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }
    # --
    # max shown user a search list
    # --
    $Self->{UserSearchListLimit} = 200;
    # --
    # get ldap preferences
    # --
    $Self->{Host} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'Host'}
     || die "Need CustomerUser->Params->Host in Kernel/Config.pm";
    $Self->{BaseDN} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'BaseDN'}
     || die "Need CustomerUser->Params->BaseDN in Kernel/Config.pm";
    $Self->{SScope} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'SSCOPE'}
     || die "Need CustomerUser->Params->SSCOPE in Kernel/Config.pm";
    $Self->{SearchUserDN} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'UserDN'} || '';
    $Self->{SearchUserPw} = $Self->{ConfigObject}->Get('CustomerUser')->{'Params'}->{'UserPw'} || '';

    $Self->{CustomerKey} = $Self->{ConfigObject}->Get('CustomerUser')->{'CustomerKey'}
     || die "Need CustomerUser->CustomerKey in Kernel/Config.pm";
    $Self->{CustomerID} = $Self->{ConfigObject}->Get('CustomerUser')->{'CustomerID'}
     || die "Need CustomerUser->CustomerID in Kernel/Config.pm";

    # --
    # ldap connect and bind (maybe with SearchUserDN and SearchUserPw)
    # --
    $Self->{LDAP} = Net::LDAP->new($Self->{Host}) or die "$@";
    if (!$Self->{LDAP}->bind(dn => $Self->{SearchUserDN}, password => $Self->{SearchUserPw})) {
        $Self->{LogObject}->Log(
          Priority => 'error',
          Message => "First bind failed!",
        );
        return;
    }
    return $Self;
}
# --
sub CustomerName {
    my $Self = shift;
    my %Param = @_;
    my $Name = '';
    # --
    # check needed stuff
    # --
    if (!$Param{UserLogin}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need UserLogin!");
        return;
    }
    # --
    # build filter
    # --
    my $Filter = "($Self->{CustomerKey}=$Param{UserLogin})";
    # --
    # perform user search
    # --
    my $Result = $Self->{LDAP}->search(
        base => $Self->{BaseDN},
        scope => $Self->{SScope},
        filter => $Filter, 
        sizelimit => $Self->{UserSearchListLimit},
    );
    foreach my $entry ($Result->all_entries) {
        foreach (@{$Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserNameFields}}) {
            if (!$Name) {
                $Name = $entry->get_value($_);
            }
            else {
                $Name .= ' '.$entry->get_value($_);
            }
        }
    }
    return $Name;
}   
# --
sub CustomerSearch {
    my $Self = shift;
    my %Param = @_;
    # --
    # check needed stuff
    # --
    if (!$Param{Search} && !$Param{UserLogin} && !$Param{PostMasterSearch}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need Search, UserLogin or PostMasterSearch!");
        return;
    }
    # --
    # build filter
    # --
    my $Filter = '';
    if ($Param{Search}) {
        if ($Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserSearchFields}) {
            $Filter = '(|';
            foreach (@{$Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserSearchFields}}) {
                $Filter.= "($_=$Param{Search})";
            }
            $Filter .= ')';
        }
        else {
            $Filter = "($Self->{CustomerKey}=$Param{Search})";
        }
    }
    elsif ($Param{PostMasterSearch}) {
        if ($Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserPostMasterSearchFields}) {
            $Filter = '(|';
            foreach (@{$Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserPostMasterSearchFields}}) {
                $Filter.= "($_=$Param{PostMasterSearch})";
            }
            $Filter .= ')';
        }
    }
    elsif ($Param{UserLogin}) {
        $Filter = "($Self->{CustomerKey}=$Param{UserLogin})";
    }
    # --
    # perform user search
    # --
    my $Result = $Self->{LDAP}->search(
        base => $Self->{BaseDN},
        scope => $Self->{SScope},
        filter => $Filter, 
        sizelimit => $Self->{UserSearchListLimit},
    );
    # --
    # log ldap errors 
    # --
    if ($Result->code()) {
        $Self->{LogObject}->Log(Priority => 'error', Message => $Result->error());
    }
    my %Users = ();
    foreach my $entry ($Result->all_entries) {
        my $CustomerString = '';
        foreach (@{$Self->{ConfigObject}->Get('CustomerUser')->{CustomerUserListFields}}) {
            my $Value = $entry->get_value($_);
            if ($Value) {
                $CustomerString .= $Value.' ';
            }
        }
        $CustomerString =~ s/^(.*\s)(.+?\@.+?\..+?)(\s|)$/"$1" <$2>/;
        $Users{$entry->get_value($Self->{CustomerKey})} = $CustomerString;
    }
    return %Users;
}   
# --
sub CustomerUserList {
    my $Self = shift;
    my %Param = @_;
    my $Valid = defined $Param{Valid} ? $Param{Valid} : 1;
    # --
    # perform user search
    # --
    my $Result = $Self->{LDAP}->search (
{'fraction_non_alphanumeric': 0.209009009009009, 'fraction_numerical': 0.0069498069498069494, 'mean_word_length': 2.3913296479487927, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 253, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '503081', 'n_tokens_mistral': 3847, 'n_tokens_neox': 3748, 'n_words': 701}