changeset 25:ec5b9cbb518c

modify group
author Dirk Olmes <dirk.olmes@codedo.de>
date Tue, 25 Oct 2016 16:18:30 +0000
parents f6e07a3d95a2
children 19ef091c0ef3
files jndi/src/test/java/de/codedo/jndi/Group.java jndi/src/test/java/de/codedo/jndi/GroupQuery.java jndi/src/test/java/de/codedo/jndi/JndiTestCase.java
diffstat 3 files changed, 110 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jndi/src/test/java/de/codedo/jndi/Group.java	Tue Oct 25 16:18:30 2016 +0000
@@ -0,0 +1,84 @@
+package de.codedo.jndi;
+
+import java.util.*;
+import javax.naming.*;
+import javax.naming.directory.*;
+
+class Group extends Object
+{
+	protected static final String GROUP_BASE = "ou=groups,dc=ldap,dc=example,dc=org";
+
+	private DirContext _context;
+	private Attribute _cn;
+	private Attribute _gidNumber;
+
+	Group(String name, int groupNumber)
+	{
+		super();
+		_cn = new BasicAttribute("cn", name);
+		_gidNumber = new BasicAttribute("gidNumber", String.valueOf(groupNumber));
+	}
+
+	Group(DirContext context, String name)
+	{
+		super();
+		_context = context;
+		_cn = new BasicAttribute("cn", name);
+	}
+
+	public void create(DirContext context) throws NamingException
+	{
+		Attribute objectClass = createObjectClass();
+
+		Attributes attributes = new BasicAttributes();
+		attributes.put(objectClass);
+		attributes.put(_cn);
+		attributes.put(_gidNumber);
+
+		String dn = buildDn();
+		context.createSubcontext(dn, attributes);
+	}
+
+	private Attribute createObjectClass()
+	{
+		Attribute objectClass = new BasicAttribute("objectClass"); 
+		objectClass.add("posixGroup");
+		objectClass.add("top");
+		return objectClass;		
+	}
+
+	public void addMember(String userUid) throws NamingException
+	{
+		if (getMemberUids().contains(userUid))
+		{
+			return;
+		}
+
+		Attribute attribute = new BasicAttribute("memberUid", userUid);
+		ModificationItem item = new ModificationItem(DirContext.ADD_ATTRIBUTE, attribute);
+
+		String dn = buildDn();
+		_context.modifyAttributes(dn, new ModificationItem[] { item });
+	}
+
+	public List<String> getMemberUids() throws NamingException
+	{
+		List<String> memberUids = new ArrayList<>();
+
+		String dn = buildDn();
+		Attributes attributes = _context.getAttributes(dn, new String[] { "memberUid" });
+		NamingEnumeration<? extends Attribute> enumeration = attributes.getAll();
+		while (enumeration.hasMore())
+		{
+			Attribute attribute = enumeration.next();
+			System.out.println(attribute);
+		}
+		
+		return memberUids;
+	}
+
+	private String buildDn() throws NamingException
+	{
+		return "cn=" + _cn.get() + "," + GROUP_BASE; 
+	}
+}
--- a/jndi/src/test/java/de/codedo/jndi/GroupQuery.java	Tue Oct 25 15:48:15 2016 +0000
+++ b/jndi/src/test/java/de/codedo/jndi/GroupQuery.java	Tue Oct 25 16:18:30 2016 +0000
@@ -5,8 +5,6 @@
 
 class GroupQuery extends Object
 {
-	private static final String GROUP_BASE = "ou=groups,dc=ldap,dc=example,dc=org";
-
 	private final DirContext _context;
 
 	GroupQuery(DirContext context)
@@ -19,7 +17,7 @@
 	{
 		int highest = 0;
 
-		NamingEnumeration<NameClassPair> groupEnum = _context.list(GROUP_BASE);
+		NamingEnumeration<NameClassPair> groupEnum = _context.list(Group.GROUP_BASE);
 		while (groupEnum.hasMore())
 		{
 			NameClassPair pair = groupEnum.next();
@@ -31,7 +29,7 @@
 
 	private int getGroupNumber(String groupCn) throws NamingException
 	{
-		String dn = groupCn + "," + GROUP_BASE;
+		String dn = groupCn + "," + Group.GROUP_BASE;
 		Attributes attributes = _context.getAttributes(dn);
 
 		Attribute gidNumber = attributes.get("gidNumber");
--- a/jndi/src/test/java/de/codedo/jndi/JndiTestCase.java	Tue Oct 25 15:48:15 2016 +0000
+++ b/jndi/src/test/java/de/codedo/jndi/JndiTestCase.java	Tue Oct 25 16:18:30 2016 +0000
@@ -80,6 +80,7 @@
 		return Integer.valueOf(value);
 	}
 
+	@Ignore
 	@Test
 	public void findHighestGroupNumber2() throws Exception
 	{
@@ -91,6 +92,29 @@
 		context.close();
 	}
 
+	@Ignore
+	@Test
+	public void createGroup() throws Exception
+	{
+		DirContext context = createContext();
+		int groupNumber = new GroupQuery(context).getHighestGroupNumber();
+		groupNumber++;
+
+		Group group = new Group("pid3", groupNumber);
+		group.create(context);
+
+		context.close();
+	}
+
+	@Test
+	public void addUserToGroup() throws Exception
+	{
+		DirContext context = createContext();
+		Group group = new Group(context, "pid3");
+		group.addMember("wwu");
+		context.close();
+	}
+
 	private DirContext createContext() throws Exception
 	{
 		Hashtable<String, Object> env = new Hashtable<>();