EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

KB42627: Creating a User by Importing from LDAP or Configuring Windows Authentication and Adding to Groups Using the MicroStrategy Web SDK 9.4.x


Community Admin

• Strategy


The following code sample shows how to create a user by importing from LDAP or configuring Windows authentication and adding this user to a group:

The following code sample shows how to create a user by importing from LDAP or configuring Windows authentication and adding this user to a group:
 


import com.Strategy.web.beans.BeanFactory;
import com.Strategy.web.beans.UserBean;
import com.Strategy.web.beans.WebBeanException;
import com.Strategy.web.objects.WebFolder;
import com.Strategy.web.objects.WebIServerSession;
import com.Strategy.web.objects.WebObjectSource;
import com.Strategy.web.objects.WebObjectsException;
import com.Strategy.web.objects.WebObjectsFactory;
import com.Strategy.web.objects.WebSearch;
import com.Strategy.web.objects.admin.users.WebNTLoginInfo;
import com.Strategy.web.objects.admin.users.WebUser;
import com.Strategy.web.objects.admin.users.WebUserGroup;
import com.Strategy.webapi.EnumDSSXMLApplicationType;
import com.Strategy.webapi.EnumDSSXMLObjectSubTypes;
import com.Strategy.webapi.EnumDSSXMLSearchDomain;
 
public class CreateNTUserAndGroup {
    public static WebIServerSession sessionInfo;
    public static final String loginName = "newUser";
    public static final String password= "";
    public static final String fullName= "New User";
    public static final String description="User created programmatically";
    public static final String groupName = "Strategy Architect";
 
    /*  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 = "Strategy Tutorial";
    /* 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) throws WebObjectsException {
     
     sessionInfo = getServerSession(iServerName, projectName, adminLoginName, adminLoginPasswd);
        WebObjectSource  source =sessionInfo.getFactory().getObjectSource();
        UserBean user = null;
        try {
            //Instantiate a new user
            user = (UserBean)BeanFactory.getInstance().newBean("UserBean");
            user.setSessionInfo(sessionInfo);
            user.InitAsNew();
            //Fetch properties for the user
            WebUser ua = (WebUser) user.getUserEntityObject();
            WebNTLoginInfo NTloginInfo = ua.getNTLoginInfo();
            //Set basic user information
            ua.setLoginName(loginName);
            ua.setFullName(fullName);
            user.getObjectInfo().setDescription(description);
            
            //Link windows user
            NTloginInfo.setWindowsAccountName(loginName);
            
            
            //Or Get LDAP Info
            WebLDAPLoginINfo LDAPloginInfo = ua.getLDAPLoginInfo();
            
            //SetLDAP DN value
            LDAPloginInfo.setLDAPDN("user LDAP DN");
            //Save user
            user.save();
            System.out.println("User: " + loginName + " created successfully\n"); 
              
            //Find user group to attach to user
            WebSearch groupSearch = source.getNewSearchObject();
            groupSearch.setNamePattern(groupName);
            groupSearch.setAsync(false);
            groupSearch.types().add(EnumDSSXMLObjectSubTypes.DssXmlSubTypeUserGroup);
            groupSearch.setDomain(EnumDSSXMLSearchDomain.DssXmlSearchDomainConfiguration);
           
            WebUserGroup group = (WebUserGroup)performSearch(groupSearch);
            if(group!=null){
             group.getMembers().add(ua);
            }
            //Save the group object
            source.save(group);
            System.out.println("User: " + loginName + " added to group " + groupName + " successfully\n"); 
            
            sessionInfo.closeSession();
            
            System.out.println("Task(s) execution completed successfully");
            
        } catch (WebObjectsException ex) {
            System.out.println("Error performing search: "+ex.getMessage());
            sessionInfo.closeSession();
        }catch (WebBeanException ex) {
            System.out.println("Error creating a user: " + ex.getMessage());
            sessionInfo.closeSession();
        }
    }
 
    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 group search: "+ex.getMessage());
        }
        return null;
    }
 
}

  
PREREQUISITES:
For the sample to work, the Strategy Web JAR files must be accessible by the Java Runtime
Environment. The Strategy Web JAR files can be found under:

  • Strategy Web Universal (JSP): {web_root}/WEB-INF/lib directory.
  • Strategy Web (.NET): Program Files\Common Files\Strategy directory.

Note:
 
More elaborate programs will require additional library files and are out of the scope of this document.
 
ADDITIONAL INFORMATION:
The Strategy SDK allows you to customize the standard Strategy Web interface, and extend and integrate the Strategy business intelligence functionality into other applications. However, before changing the way Strategy Web products look or behave, it is helpful to understand how the application is built. For more information regarding the Strategy Web architecture or the process of customizing Strategy Web, please refer to Strategy Developer Zone (https://resource.microstrategy.com/msdz/).
 
To access the Strategy Developer Zone, you must have access to the Strategy Knowledge Base, you must have purchased the Strategy SDK, and you must be current on your Strategy maintenance agreement. If you are a US-based business and believe that you satisfy all three of these conditions but you do not have access to the Strategy Developer Zone, please contact Strategy Technical Support at support@microstrategy.com or at (703) 848-8700. If you are an international business, please contact Strategy Technical Support at the appropriate email address or phone number found at http://www.microstrategy.com/Support/ContactUs.asp.
 
CUSTOMIZATION WARNING:
This customization is provided as a convenience to Strategy users and is only directly applicable to the version stated. While this code may apply to other releases directly, Strategy Technical Support makes no guarantees that the code provided will apply to any future or previous builds. In the event of a code change in future builds, Strategy Technical Support makes no guarantee that an updated version of this particular customization will be provided. In the event of a code change in future builds, Strategy may not be able to provide additional code on this matter even though this customization is provided at this time for this specific build. For enhancements to this customization or to incorporate similar functionality into other versions, contact your Account Executive to inquire about Strategy Consulting assistance.


Comment

0 comments

Details

Knowledge Article

Published:

June 22, 2017

Last Updated:

June 22, 2017