Skip to content

Commit

Permalink
Add user to a group
Browse files Browse the repository at this point in the history
  • Loading branch information
Fairuz Wan Ismail committed Dec 26, 2015
1 parent 460d9b9 commit 1bdc267
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 167 deletions.
34 changes: 34 additions & 0 deletions src/main/java/sample/controller/LdapUserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.RestController;

import sample.domain.User;
import sample.service.LdapGroupService;
import sample.service.LdapUserService;

@RestController
Expand All @@ -23,6 +24,9 @@ public class LdapUserController {
@Autowired
public LdapUserService ldapUserService;

@Autowired
public LdapGroupService ldapGroupService;

@RequestMapping("/test/usernames")
@ResponseBody
public List<String> getUserNameList() {
Expand Down Expand Up @@ -110,4 +114,34 @@ public User updateUser(@RequestParam(value="lastname", defaultValue="test.001")
return user;
}

@RequestMapping("/test/addgroup")
@ResponseBody
public User addMemberToGroup(@RequestParam(value="group") String group,
@RequestParam(value="uid", defaultValue="test.001") String uid) {
log.info("Testing user add to group");

User user = null;
try {
User _user = new User(uid);
user = ldapUserService.findUser(_user);
} catch (NameNotFoundException e) {
log.error("User not found!!!");
log.error("ERROR", e);
} catch (Exception e) {
log.error("Something unknown happened >>> " + e.getClass().getName());
log.error("ERROR", e);
}

try {
ldapGroupService.addMemberToGroup(group, user);
} catch (NameNotFoundException e) {
log.error("Group not found!!!");
log.error("ERROR", e);
} catch (Exception e) {
log.error("Something unknown happened >>> " + e.getClass().getName());
log.error("ERROR", e);
}
return user;
}

}
7 changes: 7 additions & 0 deletions src/main/java/sample/domain/LdapGroupDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sample.domain;

public interface LdapGroupDao {

public void addMemberToGroup(String groupName, User user);

}
55 changes: 55 additions & 0 deletions src/main/java/sample/domain/LdapGroupDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package sample.domain;

import javax.naming.Name;
import javax.naming.ldap.LdapName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.BaseLdapNameAware;
import org.springframework.ldap.support.LdapNameBuilder;
import org.springframework.stereotype.Component;

@Component
public class LdapGroupDaoImpl implements LdapGroupDao, BaseLdapNameAware {

private final Logger log = LoggerFactory.getLogger(this.getClass());

@Autowired
@Qualifier(value = "ldapTemplate")
private LdapTemplate ldapTemplate;

private LdapName baseLdapPath;

@Override
public void setBaseLdapPath(LdapName baseLdapPath) {
this.baseLdapPath = baseLdapPath;
}

private Name buildGroupDn(String groupName) {
return LdapNameBuilder.newInstance()
.add("ou=groups")
.add("cn", groupName).build();
}

private Name buildUserDn(String uid) {
return LdapNameBuilder.newInstance(baseLdapPath)
.add("ou", "people")
.add("uid", uid)
.build();
}

public void addMemberToGroup(String groupName, User user) {
Name groupDn = buildGroupDn(groupName);
Name userDn = buildUserDn(user.getUid());

DirContextOperations ctx = ldapTemplate.lookupContext(groupDn);
ctx.addAttributeValue("uniqueMember", userDn);
ldapTemplate.modifyAttributes(ctx);
}


}
162 changes: 0 additions & 162 deletions src/main/java/sample/domain/LdapUser.java

This file was deleted.

6 changes: 1 addition & 5 deletions src/main/java/sample/domain/LdapUserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ldap.NameAlreadyBoundException;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.AttributesMapper;
Expand All @@ -23,11 +22,8 @@
import org.springframework.stereotype.Component;

@Component
public class LdapUserDaoImpl implements LdapUserDao<LdapUser> {
public class LdapUserDaoImpl implements LdapUserDao<User> {

@Value("${amba.ldap.contextSource.base:dc=amba,dc=online}")
private String BASE_DN;

@Autowired
@Qualifier(value = "ldapTemplate")
private LdapTemplate ldapTemplate;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/sample/service/LdapGroupService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sample.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import sample.domain.LdapGroupDaoImpl;
import sample.domain.User;

@Component
public class LdapGroupService {

@Autowired
private LdapGroupDaoImpl ldapGroupDao;

public void addMemberToGroup(String groupName, User user) {
ldapGroupDao.addMemberToGroup(groupName, user);
}

}

0 comments on commit 1bdc267

Please sign in to comment.