From 54117246968b989de4b393cdf600ed73522de2cc Mon Sep 17 00:00:00 2001 From: Ross Scroggs Date: Sat, 15 Jan 2022 09:24:08 -0800 Subject: [PATCH] Improve gam user show vaultholds (#1472) * Improve gam user show vaultholds * Handle user in / * Handle user in / more efficiently * Display total holds, don't set rc, allow uid --- src/gam/gapi/directory/orgunits.py | 39 +++++++++++++++------------ src/gam/gapi/vault.py | 43 ++++++++++++++++++------------ 2 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/gam/gapi/directory/orgunits.py b/src/gam/gapi/directory/orgunits.py index fd64c6f7..1eb44b7a 100644 --- a/src/gam/gapi/directory/orgunits.py +++ b/src/gam/gapi/directory/orgunits.py @@ -9,25 +9,30 @@ from gam.gapi import directory as gapi_directory from gam.gapi import errors as gapi_errors -def _getAllParentOrgUnitIdsForUser(user, cd=None): +def _getAllParentOrgUnitsForUser(user, cd=None): if not cd: cd = gapi_directory.build() - parent_path = gapi.call( - cd.users(), - 'get', - userKey=user, - fields='orgUnitPath', - projection='basic').get('orgUnitPath') - orgunit_ids = [] - all_parent_paths = ['/'] - path_split = parent_path.split('/') - for i in range(len(path_split)): - a_parent = '/'.join(path_split[:i]) - if a_parent and a_parent not in all_parent_paths: - all_parent_paths.append(a_parent) - if parent_path not in all_parent_paths: - all_parent_paths.append(parent_path) - return [getOrgUnitId(a_parent, cd)[1] for a_parent in all_parent_paths] + parent_path = gapi.call(cd.users(), + 'get', + userKey=user, + fields='orgUnitPath', + projection='basic')['orgUnitPath'] + if parent_path == '/': + orgUnitPath, orgUnitId = getOrgUnitId('/', cd) + return {orgUnitId: orgUnitPath} + parent_path = encodeOrgUnitPath(makeOrgUnitPathRelative(parent_path)) + orgUnits = {} + while True: + result = gapi.call(cd.orgunits(), + 'get', + customerId=GC_Values[GC_CUSTOMER_ID], + orgUnitPath=parent_path, + fields='orgUnitId,orgUnitPath,parentOrgUnitId') + orgUnits[result['orgUnitId']] = result['orgUnitPath'] + if 'parentOrgUnitId' not in result: + break + parent_path = result['parentOrgUnitId'] + return orgUnits def create(): diff --git a/src/gam/gapi/vault.py b/src/gam/gapi/vault.py index 686ecaeb..e2ac331e 100644 --- a/src/gam/gapi/vault.py +++ b/src/gam/gapi/vault.py @@ -12,6 +12,7 @@ from gam import display from gam import fileutils from gam import gapi from gam.gapi import storage as gapi_storage +from gam.gapi import directory as gapi_directory from gam.gapi.directory import orgunits as gapi_directory_orgunits from gam import utils @@ -668,26 +669,34 @@ def updateHold(): def showHoldsForUsers(users): + cd = gapi_directory.build() v = buildGAPIObject() + matterIds = _getAllMatterIds(v) + matterHolds = {} + for matterId in matterIds: + matterHolds[matterId] = gapi.get_all_pages(v.matters().holds(), + 'list', + 'holds', + fields='holds(holdId,name,accounts(accountId,email),orgUnit),nextPageToken', + matterId=matterId) + totalHolds = 0 for user in users: user = user.lower() - org_ids = gapi_directory_orgunits._getAllParentOrgUnitIdsForUser(user) - matterIds = _getAllMatterIds(v) - for matterId in matterIds: - holds = gapi.get_all_pages(v.matters().holds(), - 'list', - 'holds', - fields='holds(holdId,name,accounts,orgUnit),nextPageToken', - matterId=matterId) - for hold in holds: - if 'orgUnit' in hold: - if hold['orgUnit'].get('orgUnitId') in org_ids: - print(f'FOUND: User\'s OrgUnit is on hold in matterId {matterId} and holdId {hold["holdId"]} named "{hold["name"]}"') - else: - for account in hold.get('accounts', []): - if user == account.get('email', '').lower(): - print(f'FOUND: User account is on hold in matterId {matterId} and holdId {hold["holdId"]} named "{hold["name"]}"') - break + orgUnits = gapi_directory_orgunits._getAllParentOrgUnitsForUser(user, cd) + for matterId in matterIds: + for hold in matterHolds[matterId]: + if 'orgUnit' in hold: + orgUnitId = hold['orgUnit'].get('orgUnitId') + if orgUnitId in orgUnits: + print(f'FOUND: OrgUnit {orgUnits[orgUnitId]} for user {user} is on hold in matterId {matterId} and holdId {hold["holdId"]} named "{hold["name"]}"') + totalHolds += 1 + else: + for account in hold.get('accounts', []): + if (user == account.get('email', '').lower()) or (user == account.get('accountId', '')): + print(f'FOUND: User account {user} is on hold in matterId {matterId} and holdId {hold["holdId"]} named "{hold["name"]}"') + totalHolds += 1 + break + sys.stdout.write(f'Total Holds: {totalHolds}\n') def updateMatter(action=None):