Show TOC

Assigning Users to GroupsLocate this document in the navigation structure

Prerequisites

You have imported the required interfaces and gotten the required factories.

More information: User Management Functions for Groups .

Context

This procedure describes how to assign a user to a group programmatically. Use this procedure if you cannot use the existing user administration tools to perform group assignment and need to customize your tools to assign groups programmatically.

Procedure

  1. Use the IUser and IGroup interfaces to get the user and group objects.
  2. Check if the user is already directly assigned a member of the group or not.

    This is to avoid AttributeValueAlreadyExistsException . You cannot avoid this risk completely. There is the remote possibility that another application makes the same group assignment in the time between this check and the actual group assignment.

  3. Use the getMutableGroup() method to get a modifiable version of the group object.
  4. Use the addMember() method to assign the user to the group.
  5. Commit your changes.

Example

This example gets the user demo_user and the group demo_group. It then checks if demo_user is already directly assigned to demo_group. If not the method assigns the user to the group.

            public static void modifyGroup_setAssignment(String groupUniqueName, String logonID){
        try {

        // Get the user "demo_user" and the group "demo_group".
                                        
        

        


        // Check if the user is already assigned directly to the group. 
                                
        

                                        
                //Get a modifiable group object from the UME. 
                                        
                

                                        
                // Make the user a direct member of the group.
                                        
                


                /*
                 * Write the changes to the database.  
                 * Note: If the user was already added to this group on another cluster node
                 * while between the check in the 'if' statement and now,  an
                 * AttributeValueAlreadyExistsException occurs.
                 */
                                        
                

                

        }
        } catch (NoSuchUserException nsuex) {
                // TODO: Handle NoSuchUserException.
        } catch (NoSuchUserAccountException nsuaex) {
                // TODO: Handle NoSuchUserAccountException.
        } catch (NoSuchGroupException nsgex) {
                // TODO: Handle NoSuchGroupException.
        } catch (AttributeValueAlreadyExistsException avaeex) {
                // TODO: Handle AttributeValueAlreadyExistsException.
        } catch (UMException umex) {
        // TODO: Handle UMException.
        } catch (UMRuntimeException umrex) {
                // TODO: Handle UMRuntimeException.
        }

}