Topic
  • No replies
lilawen
lilawen
2 Posts

Pinned topic Ram api to get roles and usergroups based on the community name and user id

‏2010-07-10T10:09:00Z |
I want to get the roles and usergroups which the user id belongs to in a community.
I do some researches and code as follows, but I am not sure whether it is correct or could anyone provides some more efficient RAM api to me.

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.ibm.ram.client.RAMCommunity;
import com.ibm.ram.client.RAMSession;
import com.ibm.ram.common.data.UserInformation;
import com.ibm.ram.internal.access.ws.RAM1;
import com.ibm.ram.internal.client.RAMClient;
import com.ibm.ram.internal.common.data.CommunitySO;
import com.ibm.ram.internal.common.data.RoleSO;
import com.ibm.ram.internal.common.data.UserGroupSO;

public class Test {

public void test( String communityName, String userId ) throws Exception {

List<RoleSO> roles = new ArrayList<RoleSO>();
List<UserGroupSO> groups = new ArrayList<UserGroupSO>();

// Get community based on the community name
RAMSession session = new RAMSession( "RAMURL", "ADMIN", "PASSWORD" );
RAMCommunity community = session.getCommunity( communityName );

// Connect ram server
RAM1 ram1 = new RAMClient(new URL( "RAMURL" ), "ADMIN", "PASSWORD", null).getRAM1Webservice();
int[] communityIdArray = { community.getId() };
CommunitySO[] communitySO = ram1.getCommunities( communityIdArray, true, true );
CommunitySO so = communitySO[0];

RoleSO[] rolesArray = so.getRoles();
UserGroupSO[] groupsArray = so.getUserGroups();

// Get roles and put into role list
for( RoleSO role : rolesArray ) {
String[] userIdArray = role.getUserIDs();
for( String id : userIdArray ) {
if( userId.equals( id ) ) {
roles.add( role );
}
}
}

// Get user groups and put into group list
for( UserGroupSO group : groupsArray ) {
UserInformation[] userArray = group.getUsers();
for( UserInformation user : userArray ) {
if( userId.equals( user.getUid() ) ) {
groups.add( group );
}
}
}

}

}