From fb21216b5aab0db1811fe2d16efa23b4741cf93b Mon Sep 17 00:00:00 2001 From: Beisi Zhou Date: Wed, 1 Feb 2023 10:18:06 +0800 Subject: [PATCH] Fixed query issue when objectId in assignment is empty for Get-DenyAssignment --- .../Models/ActiveDirectoryClient.cs | 2 +- src/Resources/Resources/ChangeLog.md | 1 + .../AuthorizationClientExtensions.cs | 21 ++++++++++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Resources/Resources/ActiveDirectory/Models/ActiveDirectoryClient.cs b/src/Resources/Resources/ActiveDirectory/Models/ActiveDirectoryClient.cs index c16fba68f13a..78f459c7a3fb 100644 --- a/src/Resources/Resources/ActiveDirectory/Models/ActiveDirectoryClient.cs +++ b/src/Resources/Resources/ActiveDirectory/Models/ActiveDirectoryClient.cs @@ -212,7 +212,7 @@ public IEnumerable FilterUsers(ADObjectFilterOptions options, int firs /// public PSADObject GetObjectByObjectId(string objectId) { - return GraphClient.DirectoryObjects.GetDirectoryObject(objectId)?.ToPSADObject(); + return string.IsNullOrEmpty(objectId) ? null : GraphClient.DirectoryObjects.GetDirectoryObject(objectId)?.ToPSADObject(); } /// diff --git a/src/Resources/Resources/ChangeLog.md b/src/Resources/Resources/ChangeLog.md index da394d9a2902..896b226d2d02 100644 --- a/src/Resources/Resources/ChangeLog.md +++ b/src/Resources/Resources/ChangeLog.md @@ -19,6 +19,7 @@ --> ## Upcoming Release +* Fixed query issue when objectId in assignment is empty for `Get-DenyAssignment` * Fixed an issue where running deployment cmdlets with `-WhatIf` throws exception when formatting results with nested array changes ## Version 6.5.1 diff --git a/src/Resources/Resources/Models.Authorization/AuthorizationClientExtensions.cs b/src/Resources/Resources/Models.Authorization/AuthorizationClientExtensions.cs index b82d176f5e2f..277193a29c68 100644 --- a/src/Resources/Resources/Models.Authorization/AuthorizationClientExtensions.cs +++ b/src/Resources/Resources/Models.Authorization/AuthorizationClientExtensions.cs @@ -182,7 +182,7 @@ public static IEnumerable ToPSRoleAssignments(this IEnumerable List adObjects = null; try { - adObjects = objectIds.Count > 1 ? activeDirectoryClient.GetObjectsByObjectIds(objectIds) : new List() { activeDirectoryClient.GetObjectByObjectId(objectIds.FirstOrDefault()) }; + adObjects = GetAdObjectsByObjectIds(objectIds, activeDirectoryClient); } catch (Common.MSGraph.Version1_0.DirectoryObjects.Models.OdataErrorException) { @@ -222,6 +222,21 @@ public static IEnumerable ToPSRoleAssignments(this IEnumerable return psAssignments; } + private static List GetAdObjectsByObjectIds(List objectIds, ActiveDirectoryClient activeDirectoryClient) + { + if (null == objectIds || 0 == objectIds.Count()) + { + return new List(); + } + else if (1 == objectIds.Count()) + { + return new List() { activeDirectoryClient.GetObjectByObjectId(objectIds.FirstOrDefault()) }; + }else + { + return activeDirectoryClient.GetObjectsByObjectIds(objectIds); + } + } + private static IEnumerable ToPSPrincipals(this IEnumerable principals, IEnumerable adObjects) { var psPrincipals = new List(); @@ -265,7 +280,7 @@ public static PSDenyAssignment ToPSDenyAssignment(this DenyAssignment assignment try { - adObjects = objectIds.Count() <= 1 ? new List() { activeDirectoryClient.GetObjectByObjectId(objectIds.FirstOrDefault()) } : activeDirectoryClient.GetObjectsByObjectIds(objectIds); + adObjects = GetAdObjectsByObjectIds(objectIds, activeDirectoryClient); } catch (Common.MSGraph.Version1_0.DirectoryObjects.Models.OdataErrorException) { @@ -306,7 +321,7 @@ public static IEnumerable ToPSDenyAssignments(this IEnumerable List adObjects = null; try { - adObjects = objectIds.Count() <= 1 ? new List() { activeDirectoryClient.GetObjectByObjectId(objectIds.FirstOrDefault()) } : activeDirectoryClient.GetObjectsByObjectIds(objectIds); + adObjects = GetAdObjectsByObjectIds(objectIds, activeDirectoryClient); } catch (Common.MSGraph.Version1_0.DirectoryObjects.Models.OdataErrorException) {