MicroStrategy / mstrio-py

Python integration for MicroStrategy
Apache License 2.0
90 stars 60 forks source link

Extract a list of Active users and their email addresses #89

Closed EugenDra closed 2 years ago

EugenDra commented 2 years ago

Hello,

Is it possible with mstrio to extract a list of all active users and their addresses from my Environment and export to an CSV or Excel file?

Many thanks!

Dholmblad commented 2 years ago

Hi @EugenDra Can you clarify what you mean by 'active user'? Does this mean users who do not have the 'disabled' property set?

EugenDra commented 2 years ago

Hi Darren, Yes

Dholmblad commented 2 years ago

@EugenDra Although the search engine does have parameters(EnumDSSXMLQuickSearchEnabledStatus) to search for:ALL, ENABLED_ONLY, for user objects. After reviewing the REST API implementation, this flag is only used on GET /collaboration/recipients which will return both users & usergroups. And not the GET /users API which is used by mstrio-py for UserManager functionality.

We will need to add support for filtering on enabled users for GET /users API.

Currently the only way to achieve this workflow would be to filter out the disabled users after retrival.

urszulajaczewska commented 2 years ago

Hi @EugenDra, we'll investigate GET /collaboration/recipients endpoint and update you with more info when we have it.

urszulajaczewska commented 2 years ago

Hi @EugenDra, we've looked into this and below you can find code snippet which will help you export the list of active users with addresses to CSV. We will work on delivering enhancement regarding this issue for the third quarter of this year.

import csv

from mstrio.users_and_groups import list_users
from mstrio.connection import Connection

file_name = 'user_emails.csv'
url = ''
login = ''
password = ''

conn = Connection(url, login, password)

# name of attributes on user object to save to csv, items can be removed or more added
attributes = ['username', 'full_name', 'default_email_address', 'enabled']

enabled_users = [user for user in list_users(conn) if user.enabled]

with open(file_name, 'w') as f:
    writer = csv.writer(f)
    writer.writerow(attributes)

    rows = (
        [getattr(user, col, '') for col in attributes]
        for user in enabled_users
    )
    writer.writerows(rows)