/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.management.idp.ldap.lookup;

import io.gravitee.management.idp.api.identity.IdentityLookup;
import io.gravitee.management.idp.api.identity.User;
import io.gravitee.management.idp.ldap.lookup.spring.LdapIdentityLookupConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.WhitespaceWildcardsFilter;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import java.util.Collection;

/**
 * @author David BRASSELY (david at gravitee.io)
 * @author GraviteeSource Team
 */
@Import(LdapIdentityLookupConfiguration.class)
public class LdapIdentityLookup implements IdentityLookup<String> {

    @Autowired
    private LdapTemplate ldapTemplate;

    @Override
    public Collection<User> search(String query) {
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectclass", "person"));
        filter.and(new WhitespaceWildcardsFilter("cn", query));
        return ldapTemplate.search("", filter.encode(), new UserAttributesMapper());
    }

    @Override
    public User retrieve(String dn) {
        return ldapTemplate.lookup(dn, new UserAttributesMapper());
    }

    private class UserAttributesMapper implements AttributesMapper<User> {
        public User mapFromAttributes(Attributes attrs) throws NamingException {
            LdapUser user = new LdapUser(attributeValue(attrs, "uid"));
            user.setFirstname(attributeValue(attrs, "givenname"));
            user.setLastname(attributeValue(attrs, "cn"));
            user.setEmail(attributeValue(attrs, "mail"));
            return user;
        }
    }

    private String attributeValue(Attributes attrs, String attributeId) throws NamingException {
        Attribute attr = attrs.get(attributeId);
        if (attr != null) {
            return (String) attr.get();
        }

        return null;
    }
}

@@ -871,24 +871,81 @@
 figuration;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -1049,16 +1049,65 @@
 Import;%0A
+import org.springframework.core.env.Environment;%0A
 import o
@@ -1374,16 +1374,55 @@
 ilter;%0A%0A
+import javax.annotation.PostConstruct;%0A
 import j
@@ -1775,57 +1775,738 @@
     
-@Autowired%0A    private LdapTemplate ldapTemplate;
+private final Logger LOGGER = LoggerFactory.getLogger(LdapIdentityLookup.class);%0A%0A    @Autowired%0A    private LdapTemplate ldapTemplate;%0A%0A    @Autowired%0A    private Environment environment;%0A%0A    private String identifierAttribute = %22uid%22;%0A%0A    @PostConstruct%0A    public void initialize() %7B%0A        String searchFilter = environment.getProperty(%22user-search-filter%22);%0A        LOGGER.debug(%22Looking for a LDAP user's identifier using search filter %5B%7B%7D%5D%22, searchFilter);%0A%0A        if (searchFilter != null) %7B%0A            // Search filter can be uid=%7B0%7D or mail=%7B0%7D%0A            identifierAttribute = searchFilter.split(%22=%22)%5B0%5D;%0A        %7D%0A%0A        LOGGER.info(%22User identifier is based on the %5B%7B%7D%5D attribute%22, identifierAttribute);%0A    %7D
 %0A%0A  
@@ -3177,21 +3177,35 @@
 (attrs, 
-%22uid%22
+identifierAttribute
 ));%0A    

Use user's identifier from the "user-search-filter" configuration (#104)