# HG changeset patch # User Dirk Olmes # Date 1477412310 0 # Node ID ec5b9cbb518c32b1f66eaaa98f106cd998b001b4 # Parent f6e07a3d95a24d35c1faabb13ac5524aae12bcea modify group diff -r f6e07a3d95a2 -r ec5b9cbb518c jndi/src/test/java/de/codedo/jndi/Group.java --- /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 getMemberUids() throws NamingException + { + List memberUids = new ArrayList<>(); + + String dn = buildDn(); + Attributes attributes = _context.getAttributes(dn, new String[] { "memberUid" }); + NamingEnumeration 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; + } +} diff -r f6e07a3d95a2 -r ec5b9cbb518c jndi/src/test/java/de/codedo/jndi/GroupQuery.java --- 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 groupEnum = _context.list(GROUP_BASE); + NamingEnumeration 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"); diff -r f6e07a3d95a2 -r ec5b9cbb518c jndi/src/test/java/de/codedo/jndi/JndiTestCase.java --- 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 env = new Hashtable<>();