import com.hyperion.css.CSSException; import com.hyperion.css.common.CSSNativeGroupIF; import com.hyperion.css.common.CSSNativeUserIF; /* * This sample demonstrates the following: * 1. How to create a group. * 2. How to add groups to a group. * 3. How to add users to a group. * 4. How to list all users ina group. * 5. How to list all sub groups of a group. * 6. How to retrieve a group by identity. * 7. How to delete a group. * * The initialization code is shared by all samples * and is in Sample.java */ public class GroupFunctionsSample extends Sample { public GroupFunctionsSample() throws Exception { super(); } // Creates n groups with specified identifer as name. public CSSNativeGroupIF[] getGroups(int n, String identifier) throws CSSException{ CSSNativeGroupIF[] groups = new CSSNativeGroupIF[n]; for(int i=0; i< n; i++){ groups[i] = cssDMAPI.getNativeGroupInstance(context, principal); groups[i].setName("testgroup-" + identifier + i); groups[i].setDescription("Created for test " + identifier +i); cssDMAPI.addNativeGroup(context, principal, groups[i]); } return groups; } // Creates a group instance with specified identifer as name. public CSSNativeGroupIF getGroupInstance(String identifier) throws CSSException { CSSNativeGroupIF group = cssDMAPI.getNativeGroupInstance(context,principal); group.setName("testgroup-" + identifier + "0"); group.setDescription("Created for test " + identifier + "0"); return group; } /* * Creates n users in the native directory * and returns their identities in a String[] */ public CSSNativeUserIF[] getUsers(int numUsers) throws CSSException { CSSNativeUserIF []users = new CSSNativeUserIF[numUsers]; for (int i=0; i < numUsers; i++){ users[i] = cssDMAPI.getNativeUserInstance(context, principal); users[i].setLoginName("testuser" + i); users[i].setDescription("Created for test"); users[i].setFirstName("First" + i); users[i].setLastName("Last" + i); users[i].setPassword("testuser" + i); cssDMAPI.addNativeUser(context, principal, users[i]); } return users; } /* Adds groups to the parent group */ private void addGroupsToGroup(CSSNativeGroupIF parent, int n) throws CSSException { int nChildren = n; CSSNativeGroupIF []children = getGroups(nChildren, "tchildren"); String []gids = new String[nChildren]; for(int i =0; i< nChildren ;i++){ gids[i] = children[i].getIdentity(); } parent.setGroupList(gids, true); } /* adds users to parent group */ private void addUsersToGroup(CSSNativeGroupIF parent, int n) throws CSSException { int nUsers = n; CSSNativeUserIF []users = getUsers(nUsers); String []uids = new String[nUsers]; for(int i =0; i< nUsers ;i++){ uids[i] = users[i].getIdentity(); } parent.setUserList(uids, true); } public void modifyGroup(CSSNativeGroupIF g) throws CSSException { System.out.println("Modifying group description ..."); g.setDescription("Modified Description"); cssDMAPI.updateNativeGroup(context, principal,g); } /* Read a group into memory by identity */ public CSSNativeGroupIF getGroupByIdentity(String identity) throws CSSException { return cssDMAPI.getNativeGroupByIdentity(context, principal, identity); } //Delete group public void deleteGroup(String identity) throws CSSException { cssDMAPI.deleteNativeGroups(context, principal, new String[] { identity } ); } //Delete user public void deleteUser(String identity) throws CSSException { cssDMAPI.deleteNativeUsers(context, principal, new String[] { identity } ); } /* Create group with specified number of group and user members */ public CSSNativeGroupIF createGroup (int numSubGroups, int numUsers) throws CSSException { CSSNativeGroupIF parent = getGroupInstance("ParentGroup"); addGroupsToGroup(parent, numSubGroups); addUsersToGroup(parent, numUsers); cssDMAPI.addNativeGroup(context, principal, parent); return parent; } /* Returns identities of child groups */ public String[] getChildGroups(CSSNativeGroupIF group) throws CSSException { boolean returnIndirectGroups = true; return group.getGroupList(principal, returnIndirectGroups); } /* Returns identities of all users of group */ public String[] getAllUsers(CSSNativeGroupIF group) throws CSSException { boolean returnAllUsers = true; return group.getUsersList(context, principal, returnAllUsers); } public void cleanup(String []ids) { cleanupGroups(ids); } /*clean up group members after the test case */ public void cleanupGroups(String []ids) { // delete the test groups created if (ids != null && ids.length != 0) { for (int i = 0; i < ids.length; i++) { try { deleteGroup(ids[i]); } catch (CSSException ce) { ce.printStackTrace(); } } } } /*clean up user members after the test case */ public void cleanupUsers(String []ids) { // delete the test users created if (ids != null && ids.length != 0) { for (int i = 0; i < ids.length; i++) { try { deleteUser(ids[i]); } catch (CSSException ce) { ce.printStackTrace(); } } } } public static void main(String[] args) { GroupFunctionsSample gst = null; CSSNativeGroupIF g = null; String []childGroups = null; String []childUsers = null; try { gst = new GroupFunctionsSample(); gst.printMsg(System.out, "Main methods:"); /* Create group with 3 sub groups and 2 users */ g = gst.createGroup(3,2); gst.dumpGroups(System.out, "Created Group ", new CSSNativeGroupIF[]{g}); /* list all sub(child) groups of group g */ childGroups = gst.getChildGroups(g); gst.dumpGroups(System.out, "Child groups are ", childGroups); /*list all users of group g */ childUsers = gst.getAllUsers(g); gst.dumpUsers(System.out, "Child users are ", childUsers); /*modify group */ gst.printMsg(System.out, "Modifying the group ..."); gst.modifyGroup(g); /* Read group into memory from LDAP by identity */ CSSNativeGroupIF modifiedGroup = gst.getGroupByIdentity(g.getIdentity()); gst.dumpGroups(System.out, "Modified Group ", new CSSNativeGroupIF[]{modifiedGroup}); } catch (Throwable e) { e.printStackTrace(); System.out.println("Could not run sample because of error " + e.getClass().getName() + ": " + e.getMessage()); } finally { /* delete group */ if (g != null) { try { gst.deleteGroup(g.getIdentity()); gst.printMsg(System.out, "Deleted Group."); gst.cleanupGroups(childGroups); gst.printMsg(System.out, "Deleted Group members."); gst.cleanupUsers(childUsers); gst.printMsg(System.out, "Deleted User members."); } catch (CSSException csse) {} } if (gst != null) gst.shutdown(); } } }