def delete_subscriptions_of_departed_users(connection: "Connection") -> None: """Delete all subscription in all projects which owners are departed users. Args: Args: connection: MicroStrategy connection object returned by `connection.Connection()` """ # get all projects that the authenticated user has access to response = get_projects(connection, whitelist=[('ERR014', 403)]) projects_ = response.json() if response.ok else [] # get all disabled users all_users = list_users(connection=connection) disabled_users = [u for u in all_users if not u.enabled] for project_ in projects_: project_id = project_['id'] sub_manager = SubscriptionManager(connection=connection, project_id=project_id) for user_ in disabled_users: subs = sub_manager.list_subscriptions(owner={'id': user_.id}) msg = f"subscriptions of user with ID: {user_.id}" msg += f" in project {project_['name']} with ID: {project_['id']}" # call of the function below returns True if all passed # subscriptions were deleted if sub_manager.delete(subscriptions=subs, force=True): print("All " + msg + " were deleted.", flush=True) else: print("Not all " + msg + " were deleted or there was no subscriptions.", flush=True)
def list_active_user_privileges(connection: "Connection") -> List[dict]: """List user privileges for all active users. Args: connection: MicroStrategy connection object returned by `connection.Connection()` Returns: list of dicts where each of them is in given form: { 'id' - id of user 'name' - name of user 'username' - username of user 'privileges' - list of privileges of user } """ all_users = list_users(connection=connection) active_users = [u for u in all_users if u.enabled] privileges_list = [] for usr in active_users: p = { 'id': usr.id, 'name': usr.name, 'username': usr.username, 'privileges': usr.privileges } print(f"{p['name']} ({p['username']}) ", flush=True) for prvlg in p['privileges']: print("\t" + prvlg['privilege']['name'], flush=True) privileges_list.append(p) return privileges_list
def delete_addresses_from_departed_users( connection: "Connection") -> List[dict]: """Remove each address from every departed user (those which are disabled). For each successfully removed address a message will be printed. Args: connection: MicroStrategy connection object returned by `connection.Connection()` """ # get all users that are disabled all_users = list_users(connection=connection) users_ = [u for u in all_users if not u.enabled] removed_addresses = [] for user_ in users_: # remove all email addresses from the given user if user_.addresses: for address in user_.addresses: user_.remove_address(id=address['id']) removed_addresses.append({ 'user_id': user_.id, 'username': user_.username, 'address': address }) return removed_addresses
def add_email_to_new_users(connection: "Connection", domain="microstrategy.com") -> List["User"]: """Add email address with a form `{username}@microstrategy.com` to every user which is enabled but doesn't have an email address. For each successfully added email address a message will be printed. Args: connection: MicroStrategy connection object returned by `connection.Connection()` domain: name of the domain in the email address (it should be provided without '@' symbol). Default value is "microstrategy.com". Returns: list of users to which email addresses where added """ # get all users that are enabled all_users = list_users(connection=connection) users_ = [u for u in all_users if u.enabled] modified_users_ = [] for user_ in users_: # add email address only for those users which don't have one if not user_.addresses: email_address = user_.username + '@' + domain user_.add_address(name=user_.username, address=email_address) modified_users_.append(user_) return modified_users_
def recipients(self): response = library.get_document(connection=self.connection, id=self.id).json()['recipients'] if response: self._recipients = list_users(connection=self.connection, id=[r['id'] for r in response]) else: self._recipients = [] return self._recipients
newly_created_users = create_users_from_csv(connection=conn, csv_file="path/to/file.csv") # Or you can do it manually with open("path/to/file.csv", "r") as f: users = csv.DictReader(f) for user in users: User.create(connection=conn, username=user['username'], full_name=user['full_name']) # create a single user and get users which name begins with "John" and have # additional filter for initials User.create(connection=conn, username="******", full_name="John Smith") my_users = list_users(connection=conn, name_begins="John", initials="JS") # get all user groups (you can also add additional filters as for users) and # create a new one user_groups_list = list_user_groups(connection=conn) UserGroup.create(connection=conn, name="Special Users") # get user, user group and add this user to this user group user_ = User(connection=conn, name="John Smith") user_group_ = UserGroup(connection=conn, name="Special Users") user_group_.add_users(users=[user_.id]) # set custom permissions of the user for given objects user_.set_custom_permissions(to_objects=['55AA293811EAE2F2EC7D0080EF25A592'], object_type=3, execute='grant',
project_name='MicroStrategy Library', login_mode=1) # list all dossiers and documents within a project to which we have connection dossiers = list_dossiers(connection=conn) docs = list_documents(connection=conn) # get document and dossier from by name or id and publish them to a library of # an authenticated user doc = Document(connection=conn, name='Some Simple Document') doss = Dossier(connection=conn, name='Some Simple Dossier') doc.publish() doss.publish() # list all users and get 2 of them users = list_users(connection=conn) user_1 = User(connection=conn, id='1234234534564567567867897890ABCD') user_2 = User(connection=conn, id='9876876576546543543243213210DCBA') # share one dossier/document to a given user(s) by passing user object(s) # or id(s) doss.publish(recipients=user_1) doss.publish(recipients=[ '1234234534564567567867897890ABCD', '9876876576546543543243213210DCBA' ]) # analogously we can take away dossier(s)/document(s) from the library of the # given user(s) doss.unpublish(recipients=[user_1, user_2]) doss.unpublish(recipients='1234234534564567567867897890ABCD') # list all user groups and get one of them
new_sec_fil2 = SecurityFilter.create(conn, qualification2, name2, folder_id) # alter security filter - its name, description, qualification and folder new_name = "Year > 2020" new_description = "Year in this security filter was altered from 2015 to 2020" # copy old qualification and change year new_qualification = Qualification.from_dict( new_sec_fil.qualification.to_dict()) new_qualification.tree.parameters[0].constant['value'] = "2020.0" new_folder_id = "E287FE0E11E5B55F03C70080EF555BA1" sec_fil.alter(qualification=new_qualification, name=new_name, description=new_description) # prepare users and user groups for applying and revoking security filter mstr_user = list_users(conn, name_begins='MSTR U')[0] system_monitors = list_user_groups(conn, name_begins='System Moni')[0] # apply user(s) and/or user group(s) to security filter sec_fil.apply([mstr_user, system_monitors.id]) # revoke user(s) and/or user group(s) from security filter sec_fil.revoke([mstr_user.id, system_monitors]) sec_fil.revoke(sec_fil.members) # revoke all members of security filter # apply security filter directly to user or user group mstr_user.apply_security_filter(sec_fil) # revoke security filter directly from user or user group system_monitors.apply_security_filter(sec_fil.id)