MicroStrategy ONE

Creating a User

The user is the basic unit in the MicroStrategy security model. When you create a new user, you need to provide some or all of the following information:

  • Login information

    Login information such as the user name, login ID, password, and password expiration date. For an example of how to do this, see the sample code below.

  • Authentication

    Type of authentication to be used for this user. For an example of how to do this, see the sample code below

  • Project Access

    Projects for which this user has access privileges.

The sample code below illustrates how to create a new user, specify login information (including user name, login ID, password, and password expiration date), specify the authentication mode, and save the user.

/**

 * 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 java.util.Calendar;

import java.util.Date;

 

import com.microstrategy.web.beans.BeanFactory;

import com.microstrategy.web.beans.UserBean;

import com.microstrategy.web.beans.WebBeanException;

import com.microstrategy.web.objects.WebIServerSession;

import com.microstrategy.web.objects.WebObjectsException;

import com.microstrategy.web.objects.WebObjectsFactory;

import com.microstrategy.web.objects.admin.users.WebStandardLoginInfo;

import com.microstrategy.web.objects.admin.users.WebUser;

import com.microstrategy.webapi.EnumDSSXMLApplicationType;

 

public class CreateUser {

    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="New User Created Programattically";

 

    /*  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);

        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();

            WebStandardLoginInfo loginInfo = ua.getStandardLoginInfo();

            //Set basic user information

            ua.setLoginName(loginName);

            ua.setFullName(fullName);

            user.getObjectInfo().setDescription(description);

            loginInfo.setPassword(password);

            //Set other properties

            Calendar cal = Calendar.getInstance();

            cal.set(2012, 11, 21);

            Date d = cal.getTime();

            loginInfo.setPasswordExpirationDate(d); //Password expires on November 21, 2012

            loginInfo.setPasswordExpirationFrequency(90);//90 days to expire

            loginInfo.setPasswordExpiresAutomatically(true);//If set to false, password never expires

            loginInfo.setStandardAuthAllowed(true);//The user can log in using standard auth

            user.save();

        } catch (WebBeanException ex) {

            System.out.println("Error creating a user: " + ex.getMessage());

        }

    }

 

    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;

    }

 

}