mirror of
https://github.com/bitwarden/server.git
synced 2026-01-31 22:23:18 +08:00
* Add new ExtendedCache to add caching to template parameters * Added Cache constants for building consistent keys/name, clarified that we are using defaults including TTL, removed as much fusion cache references as possible
75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace Bit.Core.AdminConsole.Utilities;
|
|
|
|
public static partial class IntegrationTemplateProcessor
|
|
{
|
|
[GeneratedRegex(@"#(\w+)#")]
|
|
private static partial Regex TokenRegex();
|
|
|
|
public static string ReplaceTokens(string template, object values)
|
|
{
|
|
if (string.IsNullOrEmpty(template))
|
|
{
|
|
return template;
|
|
}
|
|
var type = values.GetType();
|
|
return TokenRegex().Replace(template, match =>
|
|
{
|
|
var propertyName = match.Groups[1].Value;
|
|
var property = type.GetProperty(propertyName);
|
|
|
|
if (property == null)
|
|
{
|
|
return match.Value; // Return unknown keys as keys - i.e. #Key#
|
|
}
|
|
|
|
return property.GetValue(values)?.ToString() ?? string.Empty;
|
|
});
|
|
}
|
|
|
|
public static bool TemplateRequiresUser(string template)
|
|
{
|
|
if (string.IsNullOrEmpty(template))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return template.Contains("#UserName#", StringComparison.Ordinal)
|
|
|| template.Contains("#UserEmail#", StringComparison.Ordinal)
|
|
|| template.Contains("#UserType#", StringComparison.Ordinal);
|
|
}
|
|
|
|
public static bool TemplateRequiresActingUser(string template)
|
|
{
|
|
if (string.IsNullOrEmpty(template))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return template.Contains("#ActingUserName#", StringComparison.Ordinal)
|
|
|| template.Contains("#ActingUserEmail#", StringComparison.Ordinal)
|
|
|| template.Contains("#ActingUserType#", StringComparison.Ordinal);
|
|
}
|
|
|
|
public static bool TemplateRequiresGroup(string template)
|
|
{
|
|
if (string.IsNullOrEmpty(template))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return template.Contains("#GroupName#", StringComparison.Ordinal);
|
|
}
|
|
|
|
public static bool TemplateRequiresOrganization(string template)
|
|
{
|
|
if (string.IsNullOrEmpty(template))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return template.Contains("#OrganizationName#", StringComparison.Ordinal);
|
|
}
|
|
}
|