Enterprise user add search pattern
@@ -16,6 +16,8 @@ import requests
import logging
import platform
import datetime
+import fnmatch
+import re
from Cryptodome.PublicKey import RSA
from Cryptodome.Util.asn1 import DerSequence
@@ -79,7 +81,7 @@ enterprise_user_parser.add_argument('--add-role', dest='add_role', action='appen
enterprise_user_parser.add_argument('--remove-role', dest='remove_role', action='append', help='role name or role ID')
enterprise_user_parser.add_argument('--add-team', dest='add_team', action='append', help='team name or team UID')
enterprise_user_parser.add_argument('--remove-team', dest='remove_team', action='append', help='team name or team UID')
-enterprise_user_parser.add_argument('email', type=str, action='store', help='user email or user ID')
+enterprise_user_parser.add_argument('email', type=str, action='store', help='user email or user ID or user search pattern')
enterprise_user_parser.error = raise_parse_exception
enterprise_user_parser.exit = suppress_exit
@@ -428,6 +430,7 @@ class EnterpriseUserCommand(EnterpriseCommand):
def execute(self, params, **kwargs):
user = None
+ matched_users = []
email = kwargs['email']
if 'users' in params.enterprise:
for u in params.enterprise['users']:
@@ -435,6 +438,13 @@ class EnterpriseUserCommand(EnterpriseCommand):
user = u
break
+ if not user:
+ regex = re.compile(fnmatch.translate(email)).match
+ if 'users' in params.enterprise:
+ for u in params.enterprise['users']:
+ if regex(u['username']):
+ matched_users.append(u)
+
node_id = None
if kwargs.get('node'):
for node in params.enterprise['nodes']:
@@ -641,6 +651,21 @@ class EnterpriseUserCommand(EnterpriseCommand):
else:
is_verbose = kwargs.get('verbose') or False
+ self.display_user(params, user, is_verbose)
+ else:
+ if len(matched_users) > 0:
+ is_verbose = kwargs.get('verbose') or False
+ skip = True
+ for user in matched_users:
+ if skip:
+ skip = False
+ else:
+ print('\n')
+ self.display_user(params, user, is_verbose)
+ else:
+ print('No such user')
+
+ def display_user(self, params, user, is_verbose = False):
print('{0:>16s}: {1}'.format('User ID', user['enterprise_user_id']))
print('{0:>16s}: {1}'.format('Email', user['username']))
print('{0:>16s}: {1}'.format('Display Name', user['data'].get('displayname') or ''))
@@ -676,9 +701,6 @@ class EnterpriseUserCommand(EnterpriseCommand):
role_node = role_nodes[role_ids[i]]
print('{0:>16s}: {1:<22s} {2}'.format('Role' if i == 0 else '', role_node['data']['displayname'], role_node['role_id'] if is_verbose else ''))
- else:
- print('No such user')
-
class EnterpriseRoleCommand(EnterpriseCommand):
def get_parser(self):
