User / group

Creating a user

In the following example a user is created. The code checks first if the user exists int he VDoc directory.

public void organization_createUser( IDirectoryModule directoryModule, IContext context, IOrganization organization ) throws DirectoryModuleException {
	// verifying if a user exists
	IUser user = directoryModule.getUserByLogin( "jbon" );
	if ( user == null ) {
		// creating the user linked to an organization
		user = directoryModule.createUser( context, "jbon", "pwd", organization );
		user.setFirstName( "Jean" );
		user.setLastName( "Bon" );
		user.setEmail( "jbon@charcuterie.com" );
	}
}

Creating a group

In the following example a group is created. The code uses the getGroup function to check if the group already exists before creating it.

public void organization_createGroup( IDirectoryModule directoryModule, IContext context, IOrganization organization ) throws DirectoryModuleException {
	// verifying if the group exists
	IGroup managers = directoryModule.getGroup( context, organization, "Managers" );
	if ( managers == null ) {
		// creating a group inside the organization
		managers = directoryModule.createGroup( context, organization, "Managers" );
	}
}

Adding a user or a group in a group

The following code shows the possibilities of association between users and groups. It shows also how to check the belonging of a user to a group or another group.

public void organization_addGroupAndMember( IDirectoryModule directoryModule, IContext context, IOrganization organization ) throws DirectoryModuleException {
	// recovering a group
	IGroup group = directoryModule.getGroup( context, organization, "Managers" );

	// recovering a user 
	IUser user = directoryModule.getUserByLogin( "jbon" );

	// checking the belonging of a user to a group 
	if ( !user.isMemberOf( group, false ) ) {
		// adding the user to the group's members 
		group.addMember( user );
	}

	// recovering a group in the directory 
	IGroup nextGroup = directoryModule.getGroup( context, organization, "Managers" );

	// checking the belonging to a group or another group 
	if ( !nextGroup.isChildOf( group, false ) ) {
		// adding the user to the group's child 
		group.addChild( nextGroup );
	}
}