MicroStrategy ONE
Adding a User to a Group
Users are normally added to groups, frequently on functional lines in an organization. This simplifies user management by allowing access privileges and security filters to be applied to sets of users rather than to individual users.
The sample code below illustrates how to add a user to a group and save the group.
/**
* MicroStrategy SDK Sample
*
* Copyright © 2006 MicroStrategy Incorporated. All Rights Reserved.
*
* MICROSTRATEGY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
* SUITABILITY OF THIS SAMPLE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. MICROSTRATEGY SHALL NOT
* BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
* MODIFYING OR DISTRIBUTING THIS SAMPLE CODE OR ITS DERIVATIVES.
*
*
*/
package com.microstrategy.sdk.samples.usermanagement;
import com.microstrategy.web.objects.WebFolder;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectSource;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.web.objects.WebSearch;
import com.microstrategy.web.objects.admin.users.WebUser;
import com.microstrategy.web.objects.admin.users.WebUserGroup;
import com.microstrategy.webapi.EnumDSSXMLApplicationType;
import com.microstrategy.webapi.EnumDSSXMLObjectSubTypes;
import com.microstrategy.webapi.EnumDSSXMLSearchDomain;
public class AttachUserToGroup {
public static WebIServerSession sessionInfo;
public static final String groupName = "NewGroup";
public static final String userName = "New User";
/* The following information is required to login and manipulate the User management API */
/* iServerName is the IServer we are connecting to */
public static final String iServerName = "localhost";
/* projectName is the project name we are connecting to */
public static final String projectName = "";
/* loginName is the user name we use to login the project */
public static final String adminLoginName = "administrator";
/* loginPasswd is the password we use to login the project */
public static final String adminLoginPasswd = "";
public static void main(String[] args) {
sessionInfo = getServerSession(iServerName, projectName, adminLoginName, adminLoginPasswd);
WebObjectSource source =sessionInfo.getFactory().getObjectSource();
//Set up search objects
WebSearch groupSearch = source.getNewSearchObject();
WebSearch userSearch = source.getNewSearchObject();
userSearch.setNamePattern(userName);
userSearch.setAsync(false);
userSearch.types().add(EnumDSSXMLObjectSubTypes.DssXmlSubTypeUser);
userSearch.setDomain(EnumDSSXMLSearchDomain.DssXmlSearchDomainConfiguration);
groupSearch.setNamePattern(groupName);
groupSearch.setAsync(false);
groupSearch.types().add(EnumDSSXMLObjectSubTypes.DssXmlSubTypeUserGroup);
groupSearch.setDomain(EnumDSSXMLSearchDomain.DssXmlSearchDomainConfiguration);
try {
WebUserGroup group = (WebUserGroup)performSearch(groupSearch);
if(group!=null){
WebUser user = (WebUser) performSearch(userSearch);
//Add user to group
if(user!=null){
group.getMembers().add(user);
}
}
//Save the group object
source.save(group);
} catch (WebObjectsException e) {
e.printStackTrace();
}
}
public static WebIServerSession getServerSession(String serverName, String Project, String loginName, String password) {
WebIServerSession sessionInfo = null;
try {
WebObjectsFactory woFact = WebObjectsFactory.getInstance();
sessionInfo = woFact.getIServerSession();
sessionInfo.setServerName(serverName);
sessionInfo.setProjectName(Project);
sessionInfo.setLogin(loginName);
sessionInfo.setPassword(password);
sessionInfo.setApplicationType(EnumDSSXMLApplicationType.DssXmlApplicationCustomApp);
//Create a new session
sessionInfo.getSessionID();
} catch (WebObjectsException ex) {
System.out.println("Error creating a sesion");
}
return sessionInfo;
}
public static Object performSearch(WebSearch search){
try {
search.submit();
WebFolder folder = search.getResults();
if(folder.size()>0){
if(folder.size()==1){
return folder.get(0);
} else {
System.out.println("Search returns more than 1 object, returning first object");
return folder.get(0);
}
}
} catch (WebObjectsException ex) {
System.out.println("Error performing search: "+ex.getMessage());
}
return null;
}
}