<commit_before>package no.kantega.security.api.impl.ldap.profile;

import com.novell.ldap.*;
import com.novell.ldap.util.Base64;
import no.kantega.security.api.common.SystemException;
import no.kantega.security.api.identity.DefaultIdentity;
import no.kantega.security.api.identity.Identity;
import no.kantega.security.api.impl.ldap.LdapConfigurable;
import no.kantega.security.api.profile.DefaultProfile;
import no.kantega.security.api.profile.Profile;
import no.kantega.security.api.profile.ProfileComparator;
import no.kantega.security.api.profile.ProfileManager;
import no.kantega.security.api.search.DefaultProfileSearchResult;
import no.kantega.security.api.search.DefaultSearchResult;
import no.kantega.security.api.search.SearchResult;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: andersskar
 * Date: Jan 10, 2007
 * Time: 12:37:29 PM
 * To change this template use File | Settings | File Templates.
 */
public class LdapProfileManager extends LdapConfigurable implements ProfileManager {
    private String domain = "";

    public SearchResult<Profile> searchProfiles(String name) throws SystemException {
        DefaultProfileSearchResult searchResult = new DefaultProfileSearchResult();

        if (name == null) name = "";

        name = escapeChars(name).trim();

        if (name.length() < 3) {
            return searchResult;
        }

        LDAPConnection c = new LDAPConnection();

        try {
            c.connect(host, port);
            c.bind(LDAPConnection.LDAP_V3, adminUser, adminPassword.getBytes());

            String filter = "(&";
            if (objectClassUsers.length() > 0) {
                filter += "(objectclass=" + objectClassUsers + ")";
            }

            if (name.length() > 0) {
                String name1 = name;
                String name2 = "";
                if (name.indexOf(" ") != -1) {
                    name1 = name.substring(0, name.lastIndexOf(" ")).trim();
                    name2 = name.substring(name.lastIndexOf(" "), name.length()).trim();
                }

                name1 = escapeChars(name1);
                name2 = escapeChars(name2);

                if (name2.length() > 0) {
                    // Search for givenname = name1* and surname = name2*
                    filter += "(" + givenNameAttribute + "=" + name1 + "*)(" + surnameAttribute + "=" + name2 + "*)";
                } else {
                    // Search for givenname = name1* or surname = name1* or username = name1
                    filter += "(|(" + givenNameAttribute + "=" + name1 + "*)(" + surnameAttribute + "=" + name1 + "*)(" + usernameAttribute + "=" + name1 + "))";
                }
            }

            filter += ")";

            String[] searchAttributes;
            if (departmentAttribute.length() > 0) {
                searchAttributes = new String[] {usernameAttribute,  givenNameAttribute, surnameAttribute, emailAttribute, departmentAttribute};
            } else {
                searchAttributes = new String[] {usernameAttribute,  givenNameAttribute, surnameAttribute, emailAttribute};
            }
            LDAPSearchConstraints ldapConstraints = new LDAPSearchConstraints();
            ldapConstraints.setMaxResults(maxSearchResults);
            c.setConstraints(ldapConstraints);
            LDAPSearchResults results = c.search(searchBaseUsers, LDAPConnection.SCOPE_SUB, filter, searchAttributes, false);
            List<Profile> profiles = new ArrayList<Profile>();

            while (results.hasMore()) {
                try {
                    LDAPEntry entry = results.next();
                    Profile profile = getProfileFromLDAPEntry(entry, false);
                    if (profile != null) {
                        profiles.add(profile);
                    }
                } catch (LDAPReferralException l) {
                    // Ignore LDAPReferralException
                }
            }

            Collections.sort(profiles, new ProfileComparator());
            searchResult.setResults(profiles);

        } catch (Exception e) {
            throw new SystemException("Feil ved lesing av LDAP directory", e);
        } finally {
            try {
                c.disconnect();
            } catch (LDAPException e) {
                // Ingenting
            }
        }
        return searchResult;
    }


    public Profile getProfileForUser(Identity identity) throws SystemException {
        if (!identity.getDomain().equals(domain)) {
            return null;
        }

        Profile profile = null;

        LDAPConnection c = new LDAPConnection();

        try {
            c.connect(host, port);
            String filter;
            if (objectClassUsers.length() > 0) {
                filter = "(&(objectclass=" + objectClassUsers + ")(" + usernameAttribute + "=" + identity.getUserId() + "))";
            } else {
                filter = "(" + usernameAttribute + "=" + identity.getUserId() + ")";
            }

            c.bind(LDAPConnection.LDAP_V3, adminUser, adminPassword.getBytes());
            LDAPSearchResults results = c.search(searchBaseUsers, LDAPConnection.SCOPE_SUB, filter, new String[0], false);
            if (results.hasMore()) {
                try {
                    LDAPEntry entry = results.next();

                    // Hent alle attributtene
                    entry = c.read(entry.getDN());

                    profile = getProfileFromLDAPEntry(entry, true);

                    return profile;
                } catch (LDAPReferralException l) {
                    // Ignore LDAPReferralException
                }
<commit_msg>TKP-2228. La til escape av parantes når man slår opp en bruker i LDAP vha Identity<commit_after>package no.kantega.security.api.impl.ldap.profile;

import com.novell.ldap.*;
import com.novell.ldap.util.Base64;
import no.kantega.security.api.common.SystemException;
import no.kantega.security.api.identity.DefaultIdentity;
import no.kantega.security.api.identity.Identity;
import no.kantega.security.api.impl.ldap.LdapConfigurable;
import no.kantega.security.api.profile.DefaultProfile;
import no.kantega.security.api.profile.Profile;
import no.kantega.security.api.profile.ProfileComparator;
import no.kantega.security.api.profile.ProfileManager;
import no.kantega.security.api.search.DefaultProfileSearchResult;
import no.kantega.security.api.search.DefaultSearchResult;
import no.kantega.security.api.search.SearchResult;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: andersskar
 * Date: Jan 10, 2007
 * Time: 12:37:29 PM
 * To change this template use File | Settings | File Templates.
 */
public class LdapProfileManager extends LdapConfigurable implements ProfileManager {
    private String domain = "";

    public SearchResult<Profile> searchProfiles(String name) throws SystemException {
        DefaultProfileSearchResult searchResult = new DefaultProfileSearchResult();

        if (name == null) name = "";

        name = escapeChars(name).trim();

        if (name.length() < 3) {
            return searchResult;
        }

        LDAPConnection c = new LDAPConnection();

        try {
            c.connect(host, port);
            c.bind(LDAPConnection.LDAP_V3, adminUser, adminPassword.getBytes());

            String filter = "(&";
            if (objectClassUsers.length() > 0) {
                filter += "(objectclass=" + objectClassUsers + ")";
            }

            if (name.length() > 0) {
                String name1 = name;
                String name2 = "";
                if (name.indexOf(" ") != -1) {
                    name1 = name.substring(0, name.lastIndexOf(" ")).trim();
                    name2 = name.substring(name.lastIndexOf(" "), name.length()).trim();
                }

                name1 = escapeChars(name1);
                name2 = escapeChars(name2);

                if (name2.length() > 0) {
                    // Search for givenname = name1* and surname = name2*
                    filter += "(" + givenNameAttribute + "=" + name1 + "*)(" + surnameAttribute + "=" + name2 + "*)";
                } else {
                    // Search for givenname = name1* or surname = name1* or username = name1
                    filter += "(|(" + givenNameAttribute + "=" + name1 + "*)(" + surnameAttribute + "=" + name1 + "*)(" + usernameAttribute + "=" + name1 + "))";
                }
            }

            filter += ")";

            String[] searchAttributes;
            if (departmentAttribute.length() > 0) {
                searchAttributes = new String[] {usernameAttribute,  givenNameAttribute, surnameAttribute, emailAttribute, departmentAttribute};
            } else {
                searchAttributes = new String[] {usernameAttribute,  givenNameAttribute, surnameAttribute, emailAttribute};
            }
            LDAPSearchConstraints ldapConstraints = new LDAPSearchConstraints();
            ldapConstraints.setMaxResults(maxSearchResults);
            c.setConstraints(ldapConstraints);
            LDAPSearchResults results = c.search(searchBaseUsers, LDAPConnection.SCOPE_SUB, filter, searchAttributes, false);
            List<Profile> profiles = new ArrayList<Profile>();

            while (results.hasMore()) {
                try {
                    LDAPEntry entry = results.next();
                    Profile profile = getProfileFromLDAPEntry(entry, false);
                    if (profile != null) {
                        profiles.add(profile);
                    }
                } catch (LDAPReferralException l) {
                    // Ignore LDAPReferralException
                }
            }

            Collections.sort(profiles, new ProfileComparator());
            searchResult.setResults(profiles);

        } catch (Exception e) {
            throw new SystemException("Feil ved lesing av LDAP directory", e);
        } finally {
            try {
                c.disconnect();
            } catch (LDAPException e) {
                // Ingenting
            }
        }
        return searchResult;
    }


    public Profile getProfileForUser(Identity identity) throws SystemException {
        if (!identity.getDomain().equals(domain)) {
            return null;
        }

        Profile profile = null;

        LDAPConnection c = new LDAPConnection();

        final String userId = escapeChars( identity.getUserId() );

        try {
            c.connect(host, port);
            String filter;
            if (objectClassUsers.length() > 0) {
                filter = "(&(objectclass=" + objectClassUsers + ")(" + usernameAttribute + "=" + identity.getUserId() + "))";
            } else {
                filter = "(" + usernameAttribute + "=" + userId + ")";
            }

            c.bind(LDAPConnection.LDAP_V3, adminUser, adminPassword.getBytes());
            LDAPSearchResults results = c.search(searchBaseUsers, LDAPConnection.SCOPE_SUB, filter, new String[0], false);
            if (results.hasMore()) {
                try {
                    LDAPEntry entry = results.next();

                    // Hent alle attributtene
                    entry = c.read(entry.getDN());

                    profile = getProfileFromLDAPEntry(entry, true);

                    return profile;
                } catch (LDAPReferralException l) {
                    // Ignore LDAPReferralException
                }

{'fraction_non_alphanumeric': 0.11211351493387055, 'fraction_numerical': 0.008058158885871945, 'mean_word_length': 1.9488636363636365, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4744287', 'n_tokens_mistral': 3073, 'n_tokens_neox': 2889, 'n_words': 630}