Express (Distributed operating systems), v8.0 > Secure applications and their environment > Authenticate users > Select a registry or repository > Manage realms in a federated repository > Virtual member manager > Develop with virtual member manager > Integrate virtual member manager into the application > Sample code


Sample code for removing users from a group

Use the sample code snippet and data graphs to remove users from a group by using GroupMembershipControl.

The following sample code snippet shows how to remove a user from a group by using the update() method and GroupMembershipControl. For sample code to remove a user from a group by using the GroupMemberControl, see the topic Sample code for working with users, groups, group members, and group memberships.


Prerequisites

Ensure that we have read the information and completed the steps described in the topic, Program prerequisites.

Before you execute the following sample code, ensure that we have created users and groups, and assigned the users as members of groups. For sample code to do this, see the topic, Sample code for working with users, groups, group members, and group memberships.


Sample code

Add the following code snippet to the application code and replace the variables with the actual values to use.

/**
 *  rmMemberFromGroupUsingGroupMembershipControl
 *  Removes the user from the group using the group membership control
 *  @param memberDn uniqueName of the user
  *   @param groupDn uniqueame of the group  */
public static void rmMemberFromGroupUsingGroupMembershipControl(String memberDn, String groupDn)
{
    try
    {
        DataObject root = SDOHelper.createRootDataObject();
        DataObject entity = SDOHelper.createEntityDataObject(root, null, SchemaConstants.DO_GROUP);
        // Set the group uniqueName
        entity.createDataObject(SchemaConstants.DO_IDENTIFIER).set(SchemaConstants.PROP_UNIQUE_NAME,
                groupDn);
        DataObject member1 = SDOHelper.createDataObject(SchemaConstants.WIM_NS_URI,
                SchemaConstants.DO_ENTITY);
        // Set the member uniqueName
        member1.createDataObject(SchemaConstants.DO_IDENTIFIER).setString(SchemaConstants.PROP_UNIQUE_NAME,
                memberDn);
        // Get the member of the group that need to be unassigned
        entity.getList(SchemaConstants.DO_MEMBERS).add(member1);
        // Set the Group membership control
        DataObject grpMbrCtrl = SDOHelper.createControlDataObject(root, null,
                SchemaConstants.DO_GROUP_MEMBERSHIP_CONTROL);
        // Unassign mode to remove member from the group
grpMbrCtrl.setInt(SchemaConstants.PROP_MODIFY_MODE, SchemaConstants.VALUE_MODIFY_MODE_UNASSIGN);
        System.out.println("Input datagraph before removing user from group using Group Membership control"
                + printDO(root));
        // Update the group to remove its member
        root = service.update(root);
        System.out.println("Output datagraph after removing user from group using Group Membership control"
                + printDO(root));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}


Input and output data graphs

Input data graph for removing a member from a group by using GroupMembershipControl:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:Group">
<wim:identifier uniqueName="cn=group2,o=defaultWIMFileBasedRealm"/>
<wim:members>  
<wim:identifier uniqueName="uid=user2,o=defaultWIMFileBasedRealm"/>
</wim:members>
</wim:entities>
<wim:controls xsi:type="wim:GroupMembershipControl" modifyMode="3"/>
</wim:Root>
</sdo:datagraph> 

Output data graph after removing a member from a group by using GroupMembershipControl:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:Group">
<wim:identifier externalName="cn=group2,o=defaultWIMFileBasedRealm" repositoryId="InternalFileRepository"
          uniqueId="5187a438-cacc-4fe7-8aa7-c06aa76cec11" uniqueName="cn=group2,o=defaultWIMFileBasedRealm"/>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Parent topic: Sample code



+

Search Tips   |   Advanced Search