mirror of
https://github.com/bitwarden/server.git
synced 2024-11-29 13:25:17 +01:00
Resolved generic type issues when getting claim value
This commit is contained in:
parent
6fa8e5afeb
commit
e5357d9aa3
@ -100,6 +100,67 @@ public static class LicenseExtensions
|
|||||||
return default;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -248,25 +248,27 @@ public class OrganizationLicense : ILicense
|
|||||||
|
|
||||||
var errorMessages = new StringBuilder();
|
var errorMessages = new StringBuilder();
|
||||||
|
|
||||||
var enabled = Token.ToClaimsPrincipal().GetValue<bool>(nameof(Enabled));
|
var claimsPrincipal = Token.ToClaimsPrincipal();
|
||||||
|
|
||||||
|
var enabled = claimsPrincipal.GetValue<bool>(nameof(Enabled));
|
||||||
if (!enabled)
|
if (!enabled)
|
||||||
{
|
{
|
||||||
errorMessages.AppendLine("Your cloud-hosted organization is currently disabled.");
|
errorMessages.AppendLine("Your cloud-hosted organization is currently disabled.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var installationId = Token.ToClaimsPrincipal().GetValue<Guid>(nameof(InstallationId));
|
var installationId = claimsPrincipal.GetValue<Guid>(nameof(InstallationId));
|
||||||
if (installationId != globalSettings.Installation.Id)
|
if (installationId != globalSettings.Installation.Id)
|
||||||
{
|
{
|
||||||
errorMessages.AppendLine("The installation ID does not match the current installation.");
|
errorMessages.AppendLine("The installation ID does not match the current installation.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var selfHost = Token.ToClaimsPrincipal().GetValue<bool>(nameof(SelfHost));
|
var selfHost = claimsPrincipal.GetValue<bool>(nameof(SelfHost));
|
||||||
if (!selfHost)
|
if (!selfHost)
|
||||||
{
|
{
|
||||||
errorMessages.AppendLine("The license does not allow for on-premise hosting of organizations.");
|
errorMessages.AppendLine("The license does not allow for on-premise hosting of organizations.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var licenseType = Token.ToClaimsPrincipal().GetValue<LicenseType>(nameof(LicenseType));
|
var licenseType = claimsPrincipal.GetValue<LicenseType>(nameof(LicenseType));
|
||||||
if (licenseType != Enums.LicenseType.Organization)
|
if (licenseType != Enums.LicenseType.Organization)
|
||||||
{
|
{
|
||||||
errorMessages.AppendLine("Premium licenses cannot be applied to an organization. " +
|
errorMessages.AppendLine("Premium licenses cannot be applied to an organization. " +
|
||||||
|
Loading…
Reference in New Issue
Block a user