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

KB486768: How to List Security Roles for Each User in a Specified User Group Using a Python Script in MicroStrategy Workstation


Rainiero Leon

Support Engineer • MicroStrategy


This article outlines a Python script to list the security roles for each user within a specified user group in a given environment, utilizing the MicroStrategy WorkStation. The script, which is configurable to work with either the name or ID of the user group, is set to operate with the 'System Administrators' group by default.

Symptom

In Strategy Workstation, administrators may encounter difficulties in manually listing the security roles for each user within a specific user group. This process can be tedious and prone to inaccuracies, necessitating an automated approach to enhance efficiency and accuracy.
 

Cause 

The necessity for this script arises from the administrative burden of manually listing the security roles for each user within a specific user group in the Strategy environment, a task that can be laborious and error-prone.
 

Solution

To automate the listing of security roles for each user in a specified user group, please follow these steps in the Strategy Workstation:
1. Connect to Your Environment: Initialize the connection to your Strategy environment.
2. Navigate to Administration: Access the administration panel to manage scripts and workflows.
3. Access Scripts Section: Go to the scripts section where you can create or modify scripts.
4. Select Your Environment: Choose the appropriate environment where the script will be executed.
5. Open Gallery Tab: In the right section of the interface, select the Gallery tab to view available workflows and scripts.
6. Expand Administration Workflows: Find and expand the Administration Workflows section to access various administrative scripts.
7. Launch 'List Security Roles per User' Script: Double-click on the 'list security roles per user' script to open it.
8. Execute the Script: Press the run button to execute the script, which will automatically list the security roles for each user in the specified user group.

ka0PW0000002OizYAE_0EMPW000006Yr86.jpeg

 
Here is the Python script in WorkStation that facilitates this operation:


"""List security roles for every user in a user group. It is possible to provide
either name or id of user group. Without any changes this script will be
executed for user group 'System Administrators'."""

from typing import List

from mstrio.connection import Connection, get_connection
from mstrio.users_and_groups import User, UserGroup

def list_security_roles_per_user(
    connection: "Connection",
    user_group_name: str = None,
    user_group_id: str = None,
    include_user_groups: bool = False
) -> List[dict]:
    """List security roles for every user in a user group.
    It is possible to provide either name or id of user group.

    Args:
        connection: Strategy connection object returned by
            `connection.Connection()`
        user_group_name (str): name of the user group
        user_group_id (str): id of the user group
        include_user_groups (bool): if True also user groups, which are inside
            provided user group, will be included in the result

    Returns:
        list of dicts where each of them is in given form:
        {
            'type' - type of object (it can be `user` or `user group`)
            'id' - id of object
            'name' - name of object
            'username' - username of user (for user group it is None)
            'security_roles' - list of security roles which object has
        }
    """

    user_group_ = UserGroup(connection=connection, name=user_group_name, id=user_group_id)
    all_security_roles = []
    for member in user_group_.list_members():
        if not member.get('fullName', None):
            if not include_user_groups:
                continue
            member_type = 'user_group'
            tmp_ug = UserGroup(connection=connection, id=member['id'])
            security_roles = tmp_ug.security_roles
        else:
            member_type = 'user'
            tmp_u = User(connection=connection, id=member['id'])
            security_roles = tmp_u.security_roles
        m = {
            'type': member_type,
            'id': member['id'],
            'name': member['name'],
            'username': member.get('username', None),
            'security_roles': security_roles
        }
        print('Security roles:', flush=True)
        for project_ in m['security_roles']:
            print("\tProject: " + project_['name'], flush=True)
            for sec_role in project_['securityRoles']:
                print("\t\t" + sec_role['name'], flush=True)
        all_security_roles.append(m)
    return all_security_roles

# connect to environment without providing user credentials
# variable `workstationData` is stored within Workstation
conn = get_connection(workstationData, 'Strategy Tutorial')

# list security roles per all users in user group 'System Administrators'
# change name of user group if needed
list_security_roles_per_user(conn, user_group_name='System Administrators')

Related articles 

Python within Workstation https://www2.Strategy.com/producthelp/Current/Workstation/en-us/Content/intro_scripts.htm 
Preview Python Scripts https://www2.Strategy.com/producthelp/Current/Workstation/en-us/Content/preview_scripts.htm
mstrio-py https://github.com/MicroStrategy/mstrio-py 
https://www2.Strategy.com/producthelp/Current/Workstation/en-us/Content/intro_scripts.htm
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. 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 7, 2024

Last Updated:

June 10, 2024