Skip to content

Commit

Permalink
Use DirContextAdapter to update users
Browse files Browse the repository at this point in the history
  • Loading branch information
Fairuz Wan Ismail committed Dec 26, 2015
1 parent 3ac2dcd commit 4e71829
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/main/java/sample/controller/LdapUserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,26 @@ public User deleteUser(@RequestParam(value="uid", defaultValue="test.001") Strin
return user;
}

@RequestMapping("/test/userupdate")
@ResponseBody
public User updateUser(@RequestParam(value="lastname", defaultValue="test.001") String lastname,
@RequestParam(value="uid", defaultValue="test.001") String uid) {
log.info("Testing user update");

User user = null;
try {
User _user = new User(uid);
user = ldapUserService.findUser(_user);
user.setLastName(lastname);

ldapUserService.update(user);
} catch (NameNotFoundException e) {
log.error("User not found!!!");
} catch (Exception e) {
log.error("Something unknown happened >>> " + e.getClass().getName());
log.error("ERROR", e);
}
return user;
}

}
6 changes: 4 additions & 2 deletions src/main/java/sample/domain/LdapUserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public interface LdapUserDao <T>{

public List<User> getAllPersons();

public User findUserByString(String dn);
public User findUserByString(String dn) throws NameNotFoundException;

public User findUser(User user);
public User findUser(User user) throws NameNotFoundException;

public void create(User user) throws NameAlreadyBoundException;

public void delete(User user) throws NameNotFoundException;

public void update(User user) throws NameNotFoundException;

}
14 changes: 12 additions & 2 deletions src/main/java/sample/domain/LdapUserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public List<User> getAllPersons() {
.where("objectclass").is("person"), new PersonAttributesMapper());
}

public User findUserByString(String dn) {
public User findUserByString(String dn) throws NameNotFoundException {
return ldapTemplate.lookup(dn, new PersonAttributesMapper());
}

public User findUser(User user) {
public User findUser(User user) throws NameNotFoundException {
//return ldapTemplate.lookup(buildDn(user), new PersonAttributesMapper());
return ldapTemplate.lookup(buildDn(user), new PersonContextMapper());
}
Expand All @@ -123,5 +123,15 @@ public void delete(User user) throws NameNotFoundException {
ldapTemplate.unbind(dn);
}

public void update(User user) throws NameNotFoundException {
Name dn = buildDn(user);
DirContextOperations context = ldapTemplate.lookupContext(dn);

context.setAttributeValue("cn", user.getFullName());
context.setAttributeValue("sn", user.getLastName());

ldapTemplate.modifyAttributes(context);
}


}
8 changes: 6 additions & 2 deletions src/main/java/sample/service/LdapUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public List<User> getAllPersons() {
return ldapUserDao.getAllPersons();
}

public User findUserByString(String dn) {
public User findUserByString(String dn) throws NameNotFoundException {
return ldapUserDao.findUserByString(dn);
}

public User findUser(User user) {
public User findUser(User user) throws NameNotFoundException {
return ldapUserDao.findUser(user);
}

Expand All @@ -39,4 +39,8 @@ public void createUser(User user) throws NameAlreadyBoundException {
public void delete(User user) throws NameNotFoundException {
ldapUserDao.delete(user);
}

public void update(User user) throws NameNotFoundException {
ldapUserDao.update(user);
}
}

0 comments on commit 4e71829

Please sign in to comment.