From b7074a9314b32ef8c6c59d65ee7ee038cb3567af Mon Sep 17 00:00:00 2001 From: Amol Agarwal Date: Thu, 21 May 2020 22:22:49 -0700 Subject: [PATCH 1/3] Add Guest user lookup and remove incorrect if statement. --- ...rverActiveDirectoryAdministratorAdapter.cs | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Sql/Sql/ServerActiveDirectoryAdministrator/Services/AzureSqlServerActiveDirectoryAdministratorAdapter.cs b/src/Sql/Sql/ServerActiveDirectoryAdministrator/Services/AzureSqlServerActiveDirectoryAdministratorAdapter.cs index c0beba0e34e7..135b9a806429 100644 --- a/src/Sql/Sql/ServerActiveDirectoryAdministrator/Services/AzureSqlServerActiveDirectoryAdministratorAdapter.cs +++ b/src/Sql/Sql/ServerActiveDirectoryAdministrator/Services/AzureSqlServerActiveDirectoryAdministratorAdapter.cs @@ -224,12 +224,7 @@ protected ServerAzureADAdministrator GetActiveDirectoryInformation(string displa var applicationList = ActiveDirectoryClient.GetApplicationWithFilters(odataQueryFilter); - // No application was found - if (applicationList == null || applicationList.Count() == 0) - { - throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADObjectNotFound, displayName)); - } - else if (applicationList.Count() > 1) + if (applicationList.Count() > 1) { // More than one application was found. throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADApplicationMoreThanOneFound, displayName)); @@ -294,6 +289,20 @@ protected ServerAzureADAdministrator GetActiveDirectoryInformation(string displa userList = ActiveDirectoryClient.FilterUsers(filter).Where(gr => string.Equals(gr.UserPrincipalName, displayName, StringComparison.OrdinalIgnoreCase)); } + // No user was found. Check if the display name is a guest user. + if (userList == null || userList.Count() == 0) + { + // Check if the display name is the UPN + filter = new ADObjectFilterOptions() + { + Id = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null, + Mail = displayName, + Paging = true, + }; + + userList = ActiveDirectoryClient.FilterUsers(filter); + } + // No user was found if (userList == null || userList.Count() == 0) { From e364a7a06a4fb741b5e06cbce3937a32ba0e8132 Mon Sep 17 00:00:00 2001 From: Amol Agarwal Date: Thu, 21 May 2020 22:41:21 -0700 Subject: [PATCH 2/3] Add a few null checks for safely. --- .../AzureSqlServerActiveDirectoryAdministratorAdapter.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Sql/Sql/ServerActiveDirectoryAdministrator/Services/AzureSqlServerActiveDirectoryAdministratorAdapter.cs b/src/Sql/Sql/ServerActiveDirectoryAdministrator/Services/AzureSqlServerActiveDirectoryAdministratorAdapter.cs index 135b9a806429..ba2685860797 100644 --- a/src/Sql/Sql/ServerActiveDirectoryAdministrator/Services/AzureSqlServerActiveDirectoryAdministratorAdapter.cs +++ b/src/Sql/Sql/ServerActiveDirectoryAdministrator/Services/AzureSqlServerActiveDirectoryAdministratorAdapter.cs @@ -192,12 +192,12 @@ protected ServerAzureADAdministrator GetActiveDirectoryInformation(string displa // Get a list of groups from Azure Active Directory groupList = ActiveDirectoryClient.FilterGroups(filter).Where(gr => string.Equals(gr.DisplayName, displayName, StringComparison.OrdinalIgnoreCase)); - if (groupList.Count() > 1) + if (groupList != null && groupList.Count() > 1) { // More than one group was found with that display name. throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADGroupMoreThanOneFound, displayName)); } - else if (groupList.Count() == 1) + else if (groupList != null && groupList.Count() == 1) { // Only one group was found. Get the group display name and object id group = groupList.First(); @@ -224,12 +224,12 @@ protected ServerAzureADAdministrator GetActiveDirectoryInformation(string displa var applicationList = ActiveDirectoryClient.GetApplicationWithFilters(odataQueryFilter); - if (applicationList.Count() > 1) + if (applicationList != null && applicationList.Count() > 1) { // More than one application was found. throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADApplicationMoreThanOneFound, displayName)); } - else if (applicationList.Count() == 1) + else if (applicationList != null && applicationList.Count() == 1) { // Only one user was found. Get the user display name and object id PSADApplication app = applicationList.First(); From 05497eb6af53dea0a6cf3004c37f0540d4ebdf01 Mon Sep 17 00:00:00 2001 From: Amol Agarwal Date: Thu, 21 May 2020 22:46:20 -0700 Subject: [PATCH 3/3] update ChangeLog.md --- src/Sql/Sql/ChangeLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Sql/Sql/ChangeLog.md b/src/Sql/Sql/ChangeLog.md index 6436ac3f2bd3..c835e31750d3 100644 --- a/src/Sql/Sql/ChangeLog.md +++ b/src/Sql/Sql/ChangeLog.md @@ -20,6 +20,7 @@ ## Upcoming Release * Added UsePrivateLinkConnection to `New-AzSqlSyncGroup`, `Update-AzSqlSyncGroup`, `New-AzSqlSyncMember` and `Update-AzSqlSyncMember` * Added SyncMemberAzureDatabaseResourceId to `New-AzSqlSyncMember` and `Update-AzSqlSyncMember` +* Add Guest user lookup support to Set SQL Server Azure Active Directory Admin cmdlet ## Version 2.6.1 * Enhance performance of: