Skip to content

Commit

Permalink
Use ContextMapper to search for users
Browse files Browse the repository at this point in the history
  • Loading branch information
Fairuz Wan Ismail committed Dec 26, 2015
1 parent e94e179 commit 22a6ae8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/main/java/sample/controller/LdapUserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@ public List<User> getUserList() {

@RequestMapping("/test/userlookup")
@ResponseBody
public User getUserByString() {
public User getUserByString(@RequestParam(value="uid", defaultValue="test.001") String uid) {
log.info("Testing user lookup using String Dn");
return ldapUserService.findUserByString("uid=fairuz.ismail,ou=people");
String str = "uid=" + uid + ",ou=people";
return ldapUserService.findUserByString(str);
}

@RequestMapping("/test/userlookup2")
@ResponseBody
public User getUser() {
public User getUser(@RequestParam(value="uid", defaultValue="test.001") String uid) {
log.info("Testing user lookup");
User user = new User();
user.setUid("fairuz.ismail");
user.setUid(uid);
return ldapUserService.findUser(user);
}

Expand Down
16 changes: 15 additions & 1 deletion src/main/java/sample/domain/LdapUserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import org.springframework.ldap.NameAlreadyBoundException;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapNameBuilder;
import org.springframework.ldap.core.support.AbstractContextMapper;
import org.springframework.security.authentication.encoding.LdapShaPasswordEncoder;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -51,6 +53,17 @@ public User mapFromAttributes(Attributes attrs) throws NamingException {
return person;
}
}

// use this or above. This one should be better
private static class PersonContextMapper extends AbstractContextMapper<User> {
public User doMapFromContext(DirContextOperations context) {
User p = new User();
p.setFullName(context.getStringAttribute("cn"));
p.setLastName(context.getStringAttribute("sn"));
p.setUid(context.getStringAttribute("uid"));
return p;
}
}

private Attributes buildAttributes(User user) {
Attributes attrs = new BasicAttributes();
Expand Down Expand Up @@ -87,7 +100,8 @@ public User findUserByString(String dn) {
}

public User findUser(User user) {
return ldapTemplate.lookup(buildDn(user), new PersonAttributesMapper());
//return ldapTemplate.lookup(buildDn(user), new PersonAttributesMapper());
return ldapTemplate.lookup(buildDn(user), new PersonContextMapper());
}

public void create(User user) throws NameAlreadyBoundException {
Expand Down

0 comments on commit 22a6ae8

Please sign in to comment.