package com.primeton.poctag.service.ldap.impl;
import com.primeton.poctag.service.ldap.Person;
import com.primeton.poctag.service.ldap.PersonRepo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ldap.core.LdapTemplate;
import javax.naming.InvalidNameException;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.ldap.query.LdapQueryBuilder.query;
/**
*
*
* Created by zhenqin.
* User: zhenqin
* Date: 2021/9/14
* Time: 16:41
* Vendor: yiidata.com
*
*
*
* @author zhenqin
*/
public class PersonRepoImpl implements PersonRepo {
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
@Override
public List getAllPersonNames() {
/*
return ldapTemplate.search(
query().where("uid").is("zhenqin"),
new AttributesMapper() {
public String mapFromAttributes(Attributes attrs)
throws NamingException {
return attrs.get("uid").get().toString();
}
});
*/
final List people = ldapTemplate.find(query().where("uid").isPresent(), Person.class);
System.out.println(people);
return people.stream().map(Person::getUid).collect(Collectors.toList());
}
public static void main(String[] args) throws InvalidNameException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-ldap-test.xml");
final PersonRepoImpl personRepo = context.getBean(PersonRepoImpl.class);
System.out.println(personRepo.getAllPersonNames());
/*
Person vo = new Person();
vo.setDn(new LdapName("uid=admin"));
vo.setCn("admin");
vo.setSn("admin");
vo.setUid("admin");
vo.setPassword("123456");
personRepo.ldapTemplate.create(vo);
*/
}
}