package services;
import dao.LDAPSupport;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import net.sf.ldaptemplate.AttributesMapper;
import net.sf.ldaptemplate.LdapTemplate;
import net.sf.ldaptemplate.support.DistinguishedName;
public class LDAPChecker implements LDAPSupport {
private LdapTemplate ldapTemplate;
@Override
public LdapTemplate getLdapTemplate() {
return ldapTemplate;
}
@Override
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public String checkAlreadyExists(String login) {
DistinguishedName webaccounts = new DistinguishedName();
webaccounts.add("ou","webaccounts");
List lst = getLdapTemplate().search(webaccounts, "cn=" + login, new AttributesMapper() {
@Override
public Object mapFromAttributes(Attributes attrs) throws NamingException {
return attrs.get("cn").get();
}
});
if(lst.size() > 0) {
return "Такой логин уже занят";
}
return null;
}
}