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; ...@@ -15,11 +15,13 @@ import edu.kit.scc.dto.GroupDTO;
public interface GroupDAO { public interface GroupDAO {
public List<GroupDTO> getAllGroups(); public List<GroupDTO> getAllGroups();
public List<GroupDTO> getGroupDetails(int gidNumber); public List<GroupDTO> getGroupDetails(String commonName);
public void insertGroup(GroupDTO groupDTO); public void insertGroup(GroupDTO groupDTO);
public void updateGroup(GroupDTO groupDTO); public void updateGroup(GroupDTO groupDTO);
public void deleteGroup(GroupDTO groupDTO); public void deleteGroup(GroupDTO groupDTO);
public void addMember(GroupDTO groupDTO, String memberUid);
} }
...@@ -8,10 +8,13 @@ ...@@ -8,10 +8,13 @@
*/ */
package edu.kit.scc.dto; package edu.kit.scc.dto;
import java.util.List;
public class GroupDTO { public class GroupDTO {
String commonName; String commonName;
int gidNumber; int gidNumber;
List<String> memberUids;
public String getCommonName() { public String getCommonName() {
return commonName; return commonName;
...@@ -29,9 +32,17 @@ public class GroupDTO { ...@@ -29,9 +32,17 @@ public class GroupDTO {
this.gidNumber = gidNumber; this.gidNumber = gidNumber;
} }
public List<String> getMemberUids() {
return memberUids;
}
public void setMemberUids(List<String> memberUids) {
this.memberUids = memberUids;
}
@Override @Override
public String toString() { public String toString() {
return "GroupDTO [" + (commonName != null ? "commonName=" + commonName + ", " : "") + "gidNumber=" + gidNumber return "GroupDTO [" + (commonName != null ? "commonName=" + commonName + ", " : "") + "gidNumber=" + gidNumber
+ "]"; + ", " + (memberUids != null ? "memberUids=" + memberUids : "") + "]";
} }
} }
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
*/ */
package edu.kit.scc.ldap; package edu.kit.scc.ldap;
import java.util.ArrayList;
import javax.naming.NamingException; import javax.naming.NamingException;
import javax.naming.directory.Attribute; import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes; import javax.naming.directory.Attributes;
...@@ -27,7 +29,12 @@ public class GroupAttributeMapper implements AttributesMapper<GroupDTO> { ...@@ -27,7 +29,12 @@ public class GroupAttributeMapper implements AttributesMapper<GroupDTO> {
Attribute gidNumber = attributes.get("gidNumber"); Attribute gidNumber = attributes.get("gidNumber");
if (gidNumber != null) if (gidNumber != null)
groupDTO.setGidNumber(Integer.valueOf((String) gidNumber.get())); 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; return groupDTO;
} }
......
...@@ -99,14 +99,31 @@ public class LdapClient { ...@@ -99,14 +99,31 @@ public class LdapClient {
List<UserDTO> userList = ldapUser.getUserDetails(uid); List<UserDTO> userList = ldapUser.getUserDetails(uid);
UserDTO user = null; UserDTO user = null;
if (!userList.isEmpty()) { if (userList != null && !userList.isEmpty()) {
user = userList.get(0); user = userList.get(0);
log.info(user.toString()); log.debug(user.toString());
} }
return user; 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. * Gets all users from the LDAP server.
* *
...@@ -115,7 +132,7 @@ public class LdapClient { ...@@ -115,7 +132,7 @@ public class LdapClient {
public List<UserDTO> getLdapUsers() { public List<UserDTO> getLdapUsers() {
List<UserDTO> userList = ldapUser.getAllUsers(); List<UserDTO> userList = ldapUser.getAllUsers();
for (int i = 0; i < userList.size(); i++) 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; return userList;
} }
...@@ -128,13 +145,13 @@ public class LdapClient { ...@@ -128,13 +145,13 @@ public class LdapClient {
public List<GroupDTO> getLdapGroups() { public List<GroupDTO> getLdapGroups() {
List<GroupDTO> groupList = ldapGroup.getAllGroups(); List<GroupDTO> groupList = ldapGroup.getAllGroups();
for (int i = 0; i < groupList.size(); i++) 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; return groupList;
} }
/** /**
* Creates a new LDAP user. * Creates a new LDAP POSIX user.
* *
* @param uid * @param uid
* the user's uid * the user's uid
...@@ -163,4 +180,95 @@ public class LdapClient { ...@@ -163,4 +180,95 @@ public class LdapClient {
user.setHomeDirectory(homeDirectory); user.setHomeDirectory(homeDirectory);
ldapUser.insertUser(user); 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; ...@@ -14,6 +14,8 @@ import javax.naming.InvalidNameException;
import javax.naming.directory.Attributes; import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes; import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.ldap.LdapName; import javax.naming.ldap.LdapName;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -48,12 +50,12 @@ public class LdapGroupDAO implements GroupDAO { ...@@ -48,12 +50,12 @@ public class LdapGroupDAO implements GroupDAO {
} }
@Override @Override
public List<GroupDTO> getGroupDetails(int gidNumber) { public List<GroupDTO> getGroupDetails(String commonName) {
AndFilter andFilter = new AndFilter(); 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()); log.debug("LDAP query {}", andFilter.encode());
return ldapTemplate.search(groupBase, andFilter.encode(), new GroupAttributeMapper()); return ldapTemplate.search("", andFilter.encode(), new GroupAttributeMapper());
} }
@Override @Override
...@@ -64,7 +66,7 @@ public class LdapGroupDAO implements GroupDAO { ...@@ -64,7 +66,7 @@ public class LdapGroupDAO implements GroupDAO {
Attributes posixGroupAttributes = new BasicAttributes(); Attributes posixGroupAttributes = new BasicAttributes();
posixGroupAttributes.put(posixGroupBasicAttribute); posixGroupAttributes.put(posixGroupBasicAttribute);
posixGroupAttributes.put("cn", groupDTO.getCommonName()); posixGroupAttributes.put("cn", groupDTO.getCommonName());
posixGroupAttributes.put("gidNumber", groupDTO.getGidNumber()); posixGroupAttributes.put("gidNumber", String.valueOf(groupDTO.getGidNumber()));
LdapName newGroupDN = LdapUtils.emptyLdapName(); LdapName newGroupDN = LdapUtils.emptyLdapName();
try { try {
...@@ -85,14 +87,14 @@ public class LdapGroupDAO implements GroupDAO { ...@@ -85,14 +87,14 @@ public class LdapGroupDAO implements GroupDAO {
Attributes posixGroupAttributes = new BasicAttributes(); Attributes posixGroupAttributes = new BasicAttributes();
posixGroupAttributes.put(posixGroupBasicAttribute); posixGroupAttributes.put(posixGroupBasicAttribute);
posixGroupAttributes.put("cn", groupDTO.getCommonName()); 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 { try {
newGroupDN = new LdapName(groupBase); groupDN = new LdapName(groupBase);
newGroupDN.add("cn=" + groupDTO.getCommonName()); groupDN.add("cn=" + groupDTO.getCommonName());
log.debug(newGroupDN.toString()); log.debug(groupDN.toString());
ldapTemplate.bind(newGroupDN, null, posixGroupAttributes); ldapTemplate.bind(groupDN, null, posixGroupAttributes);
} catch (InvalidNameException e) { } catch (InvalidNameException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -100,12 +102,27 @@ public class LdapGroupDAO implements GroupDAO { ...@@ -100,12 +102,27 @@ public class LdapGroupDAO implements GroupDAO {
@Override @Override
public void deleteGroup(GroupDTO groupDTO) { public void deleteGroup(GroupDTO groupDTO) {
LdapName newGroupDN = LdapUtils.emptyLdapName(); LdapName groupDN = LdapUtils.emptyLdapName();
try { try {
newGroupDN = new LdapName(groupBase); groupDN = new LdapName(groupBase);
newGroupDN.add("cn=" + groupDTO.getCommonName()); groupDN.add("cn=" + groupDTO.getCommonName());
log.debug(newGroupDN.toString()); log.debug(groupDN.toString());
ldapTemplate.unbind(newGroupDN); 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) { } catch (InvalidNameException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -79,7 +79,6 @@ public class LdapUserDAO implements UserDAO { ...@@ -79,7 +79,6 @@ public class LdapUserDAO implements UserDAO {
log.debug(newUserDN.toString()); log.debug(newUserDN.toString());
ldapTemplate.bind(newUserDN, null, personAttributes); ldapTemplate.bind(newUserDN, null, personAttributes);
} catch (InvalidNameException e) { } catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
...@@ -91,6 +90,7 @@ public class LdapUserDAO implements UserDAO { ...@@ -91,6 +90,7 @@ public class LdapUserDAO implements UserDAO {
personBasicAttribute.add("posixAccount"); personBasicAttribute.add("posixAccount");
Attributes personAttributes = new BasicAttributes(); Attributes personAttributes = new BasicAttributes();
personAttributes.put(personBasicAttribute);
personAttributes.put("cn", userDTO.getCommonName()); personAttributes.put("cn", userDTO.getCommonName());
personAttributes.put("sn", userDTO.getSurName()); personAttributes.put("sn", userDTO.getSurName());
personAttributes.put("description", userDTO.getDescription()); personAttributes.put("description", userDTO.getDescription());
...@@ -99,28 +99,26 @@ public class LdapUserDAO implements UserDAO { ...@@ -99,28 +99,26 @@ public class LdapUserDAO implements UserDAO {
personAttributes.put("gidNumber", String.valueOf(userDTO.getGidNumber())); personAttributes.put("gidNumber", String.valueOf(userDTO.getGidNumber()));
personAttributes.put("homeDirectory", userDTO.getHomeDirectory()); personAttributes.put("homeDirectory", userDTO.getHomeDirectory());
LdapName newUserDN = LdapUtils.emptyLdapName(); LdapName userDN = LdapUtils.emptyLdapName();
try { try {
newUserDN = new LdapName(userBase); userDN = new LdapName(userBase);
newUserDN.add("uid=" + userDTO.getUid()); userDN.add("uid=" + userDTO.getUid());
log.debug(newUserDN.toString()); log.debug(userDN.toString());
ldapTemplate.rebind(newUserDN, null, personAttributes); ldapTemplate.rebind(userDN, null, personAttributes);
} catch (InvalidNameException e) { } catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override @Override
public void deleteUser(UserDTO userDTO) { public void deleteUser(UserDTO userDTO) {
LdapName newUserDN = LdapUtils.emptyLdapName(); LdapName userDN = LdapUtils.emptyLdapName();
try { try {
newUserDN = new LdapName(userBase); userDN = new LdapName(userBase);
newUserDN.add("uid=" + userDTO.getUid()); userDN.add("uid=" + userDTO.getUid());
log.debug(newUserDN.toString()); log.debug(userDN.toString());
ldapTemplate.unbind(newUserDN); ldapTemplate.unbind(userDN);
} catch (InvalidNameException e) { } catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
......
...@@ -8,42 +8,107 @@ ...@@ -8,42 +8,107 @@
*/ */
package edu.kit.scc.test.ldap; 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.Test;
import org.junit.runner.RunWith; 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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import edu.kit.scc.Application; import edu.kit.scc.Application;
import edu.kit.scc.dto.GroupDTO;
import edu.kit.scc.dto.UserDTO;
import edu.kit.scc.ldap.LdapClient; import edu.kit.scc.ldap.LdapClient;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class) @SpringApplicationConfiguration(classes = Application.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class LdapClientTest { public class LdapClientTest {
private static final Logger log = LoggerFactory.getLogger(LdapClientTest.class);
@Autowired @Autowired
private LdapClient ldapClient; private LdapClient ldapClient;
@Test @Test
public void getLdapGroupsTest() { public void a_createLdapGroupTest() {
ldapClient.getLdapGroups(); ldapClient.createGroup("newGroup", 3333);
} }
@Test @Test
public void getLdapUsersTest() { public void b_createLdapUserTest() {
ldapClient.getLdapUsers(); 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 @Test
public void createLdapUserTest() { public void c_getLdapUserTest() {
String cn = "newPosixUser1"; UserDTO user = ldapClient.getLdapUser("newUser");
String sn = "newPosixUser1";
String description = "new posix user"; assertNotNull(user);
String homeDirectory = "/home/newPosixUser1";
String uid = "newPosixUser1"; 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 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");