MicroStrategy ONE

Granting Privileges to a User

With user management, you can grant both users and groups specific MicroStrategy privileges.

The sample code shown below illustrates how to grant all of the MicroStrategy privilege to a user. However, you can add logic in the code to add only specific privileges.

/**

 * 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.WebPrivilegeCategories;

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

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

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

import com.microstrategy.webapi.EnumDSSXMLApplicationType;

import com.microstrategy.webapi.EnumDSSXMLObjectSubTypes;

import com.microstrategy.webapi.EnumDSSXMLSearchDomain;

 

public class GrantAllPrivlegesToUser {

    public static WebIServerSession sessionInfo;

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

        WebSearch search = source.getNewSearchObject();

        search.setNamePattern(userName);

        search.setAsync(false);

        search.types().add(EnumDSSXMLObjectSubTypes.DssXmlSubTypeUser);

        search.setDomain(EnumDSSXMLSearchDomain.DssXmlSearchDomainConfiguration);

        try {

            search.submit();

            WebFolder folder = search.getResults();

            if(folder.size()>0){

                WebUser user = (WebUser) folder.get(0);

                WebPrivilegeCategories cats = sessionInfo.getFactory().getObjectSource().getUserServicesSource().getPrivilegeCategories(user);

                //Loop though all categories and privileges and grant all privileges

                for (int i = 0; i < cats.size(); i++) {

                    WebPrivilegeCategory cat = cats.get(i);

                    String catName = cat.getName(); //Category Name

                    System.out.println("Privilege Category: " + catName);

                    for (int j = 0; j < cat.size(); j++) {

                        WebPrivilegeEntry privilegeEntry = cat.get(j);

                        privilegeEntry.grant(); //Grant privilege

                    }

                }

                source.save(user);

            }

        } 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;

    }

 

}