From e5357d9aa376132c965502ce628f4f3631aee687 Mon Sep 17 00:00:00 2001 From: Conner Turnbull Date: Mon, 4 Nov 2024 15:40:21 -0500 Subject: [PATCH] Resolved generic type issues when getting claim value --- .../Licenses/Extensions/LicenseExtensions.cs | 63 ++++++++++++++++++- .../Models/Business/OrganizationLicense.cs | 10 +-- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/Core/Billing/Licenses/Extensions/LicenseExtensions.cs b/src/Core/Billing/Licenses/Extensions/LicenseExtensions.cs index 592df4e9d..b517a6ad8 100644 --- a/src/Core/Billing/Licenses/Extensions/LicenseExtensions.cs +++ b/src/Core/Billing/Licenses/Extensions/LicenseExtensions.cs @@ -100,6 +100,67 @@ public static class LicenseExtensions return default; } - return (T)Convert.ChangeType(claim.Value, typeof(T)); + // Handle Guid + if (typeof(T) == typeof(Guid)) + { + return Guid.TryParse(claim.Value, out var guid) + ? (T)(object)guid + : default; + } + + // Handle DateTime + if (typeof(T) == typeof(DateTime)) + { + return DateTime.TryParse(claim.Value, out var dateTime) + ? (T)(object)dateTime + : default; + } + + // Handle TimeSpan + if (typeof(T) == typeof(TimeSpan)) + { + return TimeSpan.TryParse(claim.Value, out var timeSpan) + ? (T)(object)timeSpan + : default; + } + + // Check for Nullable Types + var underlyingType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); + + // Handle Enums + if (underlyingType.IsEnum) + { + if (Enum.TryParse(underlyingType, claim.Value, true, out var enumValue)) + { + return (T)enumValue; // Cast back to T + } + + return default; // Return default value for non-nullable enums or null for nullable enums + } + + // Handle other Nullable Types (e.g., int?, bool?) + if (underlyingType == typeof(int)) + { + return int.TryParse(claim.Value, out var intValue) + ? (T)(object)intValue + : default; + } + + if (underlyingType == typeof(bool)) + { + return bool.TryParse(claim.Value, out var boolValue) + ? (T)(object)boolValue + : default; + } + + if (underlyingType == typeof(double)) + { + return double.TryParse(claim.Value, out var doubleValue) + ? (T)(object)doubleValue + : default; + } + + // Fallback to Convert.ChangeType for other types including strings + return (T)Convert.ChangeType(claim.Value, underlyingType); } } diff --git a/src/Core/Models/Business/OrganizationLicense.cs b/src/Core/Models/Business/OrganizationLicense.cs index 42b4f8feb..36a275fbc 100644 --- a/src/Core/Models/Business/OrganizationLicense.cs +++ b/src/Core/Models/Business/OrganizationLicense.cs @@ -248,25 +248,27 @@ public class OrganizationLicense : ILicense var errorMessages = new StringBuilder(); - var enabled = Token.ToClaimsPrincipal().GetValue(nameof(Enabled)); + var claimsPrincipal = Token.ToClaimsPrincipal(); + + var enabled = claimsPrincipal.GetValue(nameof(Enabled)); if (!enabled) { errorMessages.AppendLine("Your cloud-hosted organization is currently disabled."); } - var installationId = Token.ToClaimsPrincipal().GetValue(nameof(InstallationId)); + var installationId = claimsPrincipal.GetValue(nameof(InstallationId)); if (installationId != globalSettings.Installation.Id) { errorMessages.AppendLine("The installation ID does not match the current installation."); } - var selfHost = Token.ToClaimsPrincipal().GetValue(nameof(SelfHost)); + var selfHost = claimsPrincipal.GetValue(nameof(SelfHost)); if (!selfHost) { errorMessages.AppendLine("The license does not allow for on-premise hosting of organizations."); } - var licenseType = Token.ToClaimsPrincipal().GetValue(nameof(LicenseType)); + var licenseType = claimsPrincipal.GetValue(nameof(LicenseType)); if (licenseType != Enums.LicenseType.Organization) { errorMessages.AppendLine("Premium licenses cannot be applied to an organization. " +