1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.ldaptive.app;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import org.bremersee.ldaptive.LdaptiveAttribute;
22 import org.bremersee.ldaptive.LdaptiveEntryMapper;
23 import org.ldaptive.AttributeModification;
24 import org.ldaptive.LdapEntry;
25 import org.ldaptive.dn.Dn;
26 import org.ldaptive.dn.NameValue;
27 import org.ldaptive.dn.RDn;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.stereotype.Component;
30 import org.springframework.util.StringUtils;
31
32
33
34
35
36
37 @Component
38 public class PersonMapper implements LdaptiveEntryMapper<Person> {
39
40 @Value("${spring.ldap.embedded.base-dn}")
41 private String baseDn;
42
43 private String getBaseDn() {
44 return "ou=people," + baseDn;
45 }
46
47 @Override
48 public String[] getObjectClasses() {
49 return new String[]{"top", "person", "organizationalPerson", "inetOrgPerson"};
50 }
51
52 @Override
53 public String mapDn(Person person) {
54 if (person == null || !StringUtils.hasText(person.getUid())) {
55 return null;
56 }
57 return Dn.builder()
58 .add(new RDn(new NameValue("uid", person.getUid())))
59 .add(new Dn(getBaseDn()))
60 .build()
61 .format();
62 }
63
64 @Override
65 public Person map(LdapEntry ldapEntry) {
66 if (ldapEntry == null) {
67 return null;
68 }
69 Person person = new Person();
70 map(ldapEntry, person);
71 return person;
72 }
73
74 @Override
75 public void map(LdapEntry ldapEntry, Person person) {
76 LdaptiveAttribute.define("cn").getValue(ldapEntry)
77 .ifPresent(person::setCn);
78 LdaptiveAttribute.define("uid").getValue(ldapEntry)
79 .ifPresent(person::setUid);
80 LdaptiveAttribute.define("sn").getValue(ldapEntry)
81 .ifPresent(person::setSn);
82 }
83
84 @Override
85 public AttributeModification[] mapAndComputeModifications(
86 Person source,
87 LdapEntry destination) {
88 List<AttributeModification> modifications = new ArrayList<>();
89 LdaptiveAttribute.define("cn").setValue(destination, source.getCn())
90 .ifPresent(modifications::add);
91 LdaptiveAttribute.define("uid").setValue(destination, source.getUid())
92 .ifPresent(modifications::add);
93 LdaptiveAttribute.define("sn").setValue(destination, source.getSn())
94 .ifPresent(modifications::add);
95 return modifications.toArray(new AttributeModification[0]);
96 }
97
98 }