Commit 57509300 authored by benjamin.ertl's avatar benjamin.ertl
Browse files

update ldap classes

parent d27b3f4c
......@@ -15,11 +15,13 @@ import edu.kit.scc.dto.GroupDTO;
public interface GroupDAO {
public List<GroupDTO> getAllGroups();
public List<GroupDTO> getGroupDetails(int gidNumber);
public List<GroupDTO> getGroupDetails(String commonName);
public void insertGroup(GroupDTO groupDTO);
public void updateGroup(GroupDTO groupDTO);
public void deleteGroup(GroupDTO groupDTO);
public void addMember(GroupDTO groupDTO, String memberUid);
}
......@@ -8,10 +8,13 @@
*/
package edu.kit.scc.dto;
import java.util.List;
public class GroupDTO {
String commonName;
int gidNumber;
List<String> memberUids;
public String getCommonName() {
return commonName;
......@@ -29,9 +32,17 @@ public class GroupDTO {
this.gidNumber = gidNumber;
}
public List<String> getMemberUids() {
return memberUids;
}
public void setMemberUids(List<String> memberUids) {
this.memberUids = memberUids;
}
@Override
public String toString() {
return "GroupDTO [" + (commonName != null ? "commonName=" + commonName + ", " : "") + "gidNumber=" + gidNumber
+ "]";
+ ", " + (memberUids != null ? "memberUids=" + memberUids : "") + "]";
}
}
......@@ -8,6 +8,8 @@
*/
package edu.kit.scc.ldap;
import java.util.ArrayList;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
......@@ -27,7 +29,12 @@ public class GroupAttributeMapper implements AttributesMapper<GroupDTO> {
Attribute gidNumber = attributes.get("gidNumber");
if (gidNumber != null)
groupDTO.setGidNumber(Integer.valueOf((String) gidNumber.get()));
Attribute memberUids = attributes.get("memberUid");
if (memberUids != null) {
groupDTO.setMemberUids(new ArrayList<String>());
for (int i = 0; i < memberUids.size(); i++)
groupDTO.getMemberUids().add((String) memberUids.get(i));
}
return groupDTO;
}
......
......@@ -99,14 +99,31 @@ public class LdapClient {
List<UserDTO> userList = ldapUser.getUserDetails(uid);
UserDTO user = null;
if (!userList.isEmpty()) {
if (userList != null && !userList.isEmpty()) {
user = userList.get(0);
log.info(user.toString());
log.debug(user.toString());
}
return user;
}
/**
* Gets the group specified from the LDAP server.
*
* @param cn
* the group's common name
* @return a {@link GroupDTO} with the LDAP group information
*/
public GroupDTO getLdapGroup(String cn) {
List<GroupDTO> groupList = ldapGroup.getGroupDetails(cn);
GroupDTO group = null;
if (groupList != null && !groupList.isEmpty()) {
group = groupList.get(0);
log.debug(group.toString());
}
return group;
}
/**
* Gets all users from the LDAP server.
*
......@@ -115,7 +132,7 @@ public class LdapClient {
public List<UserDTO> getLdapUsers() {
List<UserDTO> userList = ldapUser.getAllUsers();
for (int i = 0; i < userList.size(); i++)
log.info("User name {}", ((UserDTO) userList.get(i)).getCommonName());
log.debug("User {}", ((UserDTO) userList.get(i)).toString());
return userList;
}
......@@ -128,13 +145,13 @@ public class LdapClient {
public List<GroupDTO> getLdapGroups() {
List<GroupDTO> groupList = ldapGroup.getAllGroups();
for (int i = 0; i < groupList.size(); i++)
log.info("Gropu name {}", ((GroupDTO) groupList.get(i)).getCommonName());
log.debug("Group {}", ((GroupDTO) groupList.get(i)).toString());
return groupList;
}
/**
* Creates a new LDAP user.
* Creates a new LDAP POSIX user.
*
* @param uid
* the user's uid
......@@ -163,4 +180,95 @@ public class LdapClient {
user.setHomeDirectory(homeDirectory);
ldapUser.insertUser(user);
}
/**
* Updates a specific LDAP POSIX user.
*
* @param uid
* the user's uid
* @param cn
* the user's common name
* @param sn
* the user's sure name
* @param uidNumber
* the user's uid number
* @param gidNumber
* the user's gid number
* @param homeDirectory
* the user's home directory
* @param description
* the user's description
*/
public void updateUser(String uid, String cn, String sn, int uidNumber, int gidNumber, String homeDirectory,
String description) {
UserDTO user = new UserDTO();
user.setCommonName(cn);
user.setDescription(description);
user.setSurName(sn);
user.setUid(uid);
user.setGidNumber(gidNumber);
user.setUidNumber(uidNumber);
user.setHomeDirectory(homeDirectory);
ldapUser.updateUser(user);
}
/**
* Deletes a specific LDAP POSIX user.
*
* @param uid
* the user's uid
*/
public void deleteUser(String uid) {
UserDTO user = new UserDTO();
user.setUid(uid);
ldapUser.deleteUser(user);
}
/**
* Creates a new LDAP POSIX group.
*
* @param cn
* the group's common name
* @param gidNumber
* the group's gid number
*/
public void createGroup(String cn, int gidNumber) {
GroupDTO group = new GroupDTO();
group.setCommonName(cn);
group.setGidNumber(gidNumber);
ldapGroup.insertGroup(group);
}
/**
* Updates a specific LDAP POSIX group.
*
* @param cn
* the group's common name
* @param gidNumber
* the group's gid number
*/
public void updateGroup(String cn, int gidNumber) {
GroupDTO group = new GroupDTO();
group.setCommonName(cn);
group.setGidNumber(gidNumber);
ldapGroup.updateGroup(group);
}
/**
* Deletes a specific LDAP POSIX group.
*
* @param cn
* the group's common name
*/
public void deleteGroup(String cn) {
GroupDTO group = new GroupDTO();
group.setCommonName(cn);
ldapGroup.deleteGroup(group);
}
public void addGroupMember(String cn, String memberUid) {
GroupDTO group = new GroupDTO();
group.setCommonName(cn);
ldapGroup.addMember(group, memberUid);
}
}
......@@ -14,6 +14,8 @@ import javax.naming.InvalidNameException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.ldap.LdapName;
import org.slf4j.Logger;
......@@ -48,12 +50,12 @@ public class LdapGroupDAO implements GroupDAO {
}
@Override
public List<GroupDTO> getGroupDetails(int gidNumber) {
public List<GroupDTO> getGroupDetails(String commonName) {
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass", "posixGroup")).and(new EqualsFilter("gidNumber", gidNumber));
andFilter.and(new EqualsFilter("objectclass", "posixGroup")).and(new EqualsFilter("cn", commonName));
log.debug("LDAP query {}", andFilter.encode());
return ldapTemplate.search(groupBase, andFilter.encode(), new GroupAttributeMapper());
return ldapTemplate.search("", andFilter.encode(), new GroupAttributeMapper());
}
@Override
......@@ -64,7 +66,7 @@ public class LdapGroupDAO implements GroupDAO {
Attributes posixGroupAttributes = new BasicAttributes();
posixGroupAttributes.put(posixGroupBasicAttribute);
posixGroupAttributes.put("cn", groupDTO.getCommonName());
posixGroupAttributes.put("gidNumber", groupDTO.getGidNumber());
posixGroupAttributes.put("gidNumber", String.valueOf(groupDTO.getGidNumber()));
LdapName newGroupDN = LdapUtils.emptyLdapName();
try {
......@@ -85,14 +87,14 @@ public class LdapGroupDAO implements GroupDAO {
Attributes posixGroupAttributes = new BasicAttributes();
posixGroupAttributes.put(posixGroupBasicAttribute);
posixGroupAttributes.put("cn", groupDTO.getCommonName());
posixGroupAttributes.put("gidNumber", groupDTO.getGidNumber());
posixGroupAttributes.put("gidNumber", String.valueOf(groupDTO.getGidNumber()));
LdapName newGroupDN = LdapUtils.emptyLdapName();
LdapName groupDN = LdapUtils.emptyLdapName();
try {
newGroupDN = new LdapName(groupBase);
newGroupDN.add("cn=" + groupDTO.getCommonName());
log.debug(newGroupDN.toString());
ldapTemplate.bind(newGroupDN, null, posixGroupAttributes);
groupDN = new LdapName(groupBase);
groupDN.add("cn=" + groupDTO.getCommonName());
log.debug(groupDN.toString());
ldapTemplate.bind(groupDN, null, posixGroupAttributes);
} catch (InvalidNameException e) {
e.printStackTrace();
}
......@@ -100,12 +102,27 @@ public class LdapGroupDAO implements GroupDAO {
@Override
public void deleteGroup(GroupDTO groupDTO) {
LdapName newGroupDN = LdapUtils.emptyLdapName();
LdapName groupDN = LdapUtils.emptyLdapName();
try {
newGroupDN = new LdapName(groupBase);
newGroupDN.add("cn=" + groupDTO.getCommonName());
log.debug(newGroupDN.toString());
ldapTemplate.unbind(newGroupDN);
groupDN = new LdapName(groupBase);
groupDN.add("cn=" + groupDTO.getCommonName());
log.debug(groupDN.toString());
ldapTemplate.unbind(groupDN);
} catch (InvalidNameException e) {
e.printStackTrace();
}
}
@Override
public void addMember(GroupDTO groupDTO, String memberUid) {
ModificationItem[] modificationItems = new ModificationItem[] {
new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("memberUid", memberUid)) };
LdapName groupDN = LdapUtils.emptyLdapName();
try {
groupDN = new LdapName(groupBase);
groupDN.add("cn=" + groupDTO.getCommonName());
log.debug(groupDN.toString());
ldapTemplate.modifyAttributes(groupDN, modificationItems);
} catch (InvalidNameException e) {
e.printStackTrace();
}
......
......@@ -79,7 +79,6 @@ public class LdapUserDAO implements UserDAO {
log.debug(newUserDN.toString());
ldapTemplate.bind(newUserDN, null, personAttributes);
} catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
......@@ -91,6 +90,7 @@ public class LdapUserDAO implements UserDAO {
personBasicAttribute.add("posixAccount");
Attributes personAttributes = new BasicAttributes();
personAttributes.put(personBasicAttribute);
personAttributes.put("cn", userDTO.getCommonName());
personAttributes.put("sn", userDTO.getSurName());
personAttributes.put("description", userDTO.getDescription());
......@@ -99,28 +99,26 @@ public class LdapUserDAO implements UserDAO {
personAttributes.put("gidNumber", String.valueOf(userDTO.getGidNumber()));
personAttributes.put("homeDirectory", userDTO.getHomeDirectory());
LdapName newUserDN = LdapUtils.emptyLdapName();
LdapName userDN = LdapUtils.emptyLdapName();
try {
newUserDN = new LdapName(userBase);
newUserDN.add("uid=" + userDTO.getUid());
log.debug(newUserDN.toString());
ldapTemplate.rebind(newUserDN, null, personAttributes);
userDN = new LdapName(userBase);
userDN.add("uid=" + userDTO.getUid());
log.debug(userDN.toString());
ldapTemplate.rebind(userDN, null, personAttributes);
} catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void deleteUser(UserDTO userDTO) {
LdapName newUserDN = LdapUtils.emptyLdapName();
LdapName userDN = LdapUtils.emptyLdapName();
try {
newUserDN = new LdapName(userBase);
newUserDN.add("uid=" + userDTO.getUid());
log.debug(newUserDN.toString());
ldapTemplate.unbind(newUserDN);
userDN = new LdapName(userBase);
userDN.add("uid=" + userDTO.getUid());
log.debug(userDN.toString());
ldapTemplate.unbind(userDN);
} catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
......
......@@ -8,42 +8,107 @@
*/
package edu.kit.scc.test.ldap;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import edu.kit.scc.Application;
import edu.kit.scc.dto.GroupDTO;
import edu.kit.scc.dto.UserDTO;
import edu.kit.scc.ldap.LdapClient;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class LdapClientTest {
private static final Logger log = LoggerFactory.getLogger(LdapClientTest.class);
@Autowired
private LdapClient ldapClient;
@Test
public void getLdapGroupsTest() {
ldapClient.getLdapGroups();
public void a_createLdapGroupTest() {
ldapClient.createGroup("newGroup", 3333);
}
@Test
public void getLdapUsersTest() {
ldapClient.getLdapUsers();
public void b_createLdapUserTest() {
String cn = "newUser";
String sn = "newUser";
String description = "new posix user";
String homeDirectory = "/home/newUser";
String uid = "newUser";
int uidNumber = 6001;
int gidNumber = 3333;
ldapClient.createUser(uid, cn, sn, uidNumber, gidNumber, homeDirectory, description);
}
@Test
public void createLdapUserTest() {
String cn = "newPosixUser1";
String sn = "newPosixUser1";
String description = "new posix user";
String homeDirectory = "/home/newPosixUser1";
String uid = "newPosixUser1";
public void c_getLdapUserTest() {
UserDTO user = ldapClient.getLdapUser("newUser");
assertNotNull(user);
log.debug(user.toString());
}
@Test
public void d_updateUserTest() {
String cn = "newUser";
String sn = "newUser";
String description = "new posix user (update)";
String homeDirectory = "/home/newUser";
String uid = "newUser";
int uidNumber = 6001;
int gidNumber = 2222;
int gidNumber = 3333;
ldapClient.createUser(uid, cn, sn, uidNumber, gidNumber, homeDirectory, description);
ldapClient.updateUser(uid, cn, sn, uidNumber, gidNumber, homeDirectory, description);
}
@Test
public void e_addUserToGroupTest() {
ldapClient.addGroupMember("newGroup", "newUser");
}
@Test
public void f_getLdapGroupTest() {
GroupDTO group = ldapClient.getLdapGroup("newGroup");
assertNotNull(group);
log.debug(group.toString());
}
@Test
public void g_deleteLdapUserTest() {
ldapClient.deleteUser("newUser");
}
@Test
public void h_deleteLdapGroupTest() {
ldapClient.deleteGroup("newGroup");
}
@Test
public void getLdapGroupsTest() {
ldapClient.getLdapGroups();
}
@Test
public void getLdapUsersTest() {
ldapClient.getLdapUsers();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment