From 110194c3a5fe1e889b1a3f9ebcc21e5f52dde456 Mon Sep 17 00:00:00 2001 From: Yvan Duhamel Date: Tue, 7 May 2024 10:02:36 +0200 Subject: [PATCH] Add helper methods to get/delete a tenant in the configuration --- CHANGELOG.md | 1 + .../EntraIDProviderConfiguration.cs | 37 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f38b850..7e69bf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased * Fix an NullReferenceException in a very rare scenario where ClaimsPrincipal.Identity is null +* Add helper methods to get/delete a tenant in the configuration ## EntraCP v25.0.20240503.33 enhancements & bug-fixes - Published in May 3, 2024 diff --git a/Yvand.EntraCP/Yvand.EntraClaimsProvider/Configuration/EntraIDProviderConfiguration.cs b/Yvand.EntraCP/Yvand.EntraClaimsProvider/Configuration/EntraIDProviderConfiguration.cs index 7537328..006fd3a 100644 --- a/Yvand.EntraCP/Yvand.EntraClaimsProvider/Configuration/EntraIDProviderConfiguration.cs +++ b/Yvand.EntraCP/Yvand.EntraClaimsProvider/Configuration/EntraIDProviderConfiguration.cs @@ -404,7 +404,7 @@ public bool UpdateTenantCredentials(string tenantName, string newClientSecret, s throw new ArgumentNullException(nameof(newClientSecret)); } - EntraIDTenant tenant = this.EntraIDTenants.FirstOrDefault(x => x.Name.Equals(tenantName, StringComparison.InvariantCultureIgnoreCase)); + EntraIDTenant tenant = GetTenantConfiguration(tenantName); if (tenant == null) { return false; } string clientId = String.IsNullOrWhiteSpace(newClientId) ? tenant.ClientId : newClientId; return tenant.SetCredentials(clientId, newClientSecret); @@ -426,13 +426,46 @@ public bool UpdateTenantCredentials(string tenantName, string newClientCertifica throw new ArgumentNullException(nameof(tenantName)); } - EntraIDTenant tenant = this.EntraIDTenants.FirstOrDefault(x => x.Name.Equals(tenantName, StringComparison.InvariantCultureIgnoreCase)); + EntraIDTenant tenant = GetTenantConfiguration(tenantName); if (tenant == null) { return false; } string clientId = String.IsNullOrWhiteSpace(newClientId) ? tenant.ClientId : newClientId; return tenant.SetCredentials(clientId, newClientCertificatePfxFilePath, newClientCertificatePfxPassword); } + /// + /// Gets the tenant configuration + /// + /// Name of the tenant + /// + /// + public EntraIDTenant GetTenantConfiguration(string tenantName) + { + if (String.IsNullOrWhiteSpace(tenantName)) + { + throw new ArgumentNullException(nameof(tenantName)); + } + return this.EntraIDTenants.FirstOrDefault(x => x.Name.Equals(tenantName, StringComparison.OrdinalIgnoreCase)); + } + + /// + /// Deletes the tenant configuration + /// + /// Name of the tenant + /// + /// + public bool DeleteTenantConfiguration(string tenantName) + { + if (String.IsNullOrWhiteSpace(tenantName)) + { + throw new ArgumentNullException(nameof(tenantName)); + } + + EntraIDTenant tenant = GetTenantConfiguration(tenantName); + if (tenant == null) { return false; } + return this.EntraIDTenants.Remove(tenant); + } + /// /// If it is valid, commits the current settings to the SharePoint settings database ///