2025-05-27 08:28:50 -04:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
2025-07-01 08:52:38 -04:00
|
|
|
|
using System.Text.Json;
|
2025-05-27 08:28:50 -04:00
|
|
|
|
using System.Text.RegularExpressions;
|
2025-04-23 10:44:43 -04:00
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-05-27 08:28:50 -04:00
|
|
|
|
if (string.IsNullOrEmpty(template))
|
2025-05-05 08:04:59 -04:00
|
|
|
|
{
|
2025-04-23 10:44:43 -04:00
|
|
|
|
return template;
|
2025-05-05 08:04:59 -04:00
|
|
|
|
}
|
2025-04-23 10:44:43 -04:00
|
|
|
|
var type = values.GetType();
|
|
|
|
|
|
return TokenRegex().Replace(template, match =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var propertyName = match.Groups[1].Value;
|
2025-07-01 08:52:38 -04:00
|
|
|
|
if (propertyName == "EventMessage")
|
|
|
|
|
|
{
|
|
|
|
|
|
return JsonSerializer.Serialize(values);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var property = type.GetProperty(propertyName);
|
|
|
|
|
|
return property?.GetValue(values)?.ToString() ?? match.Value;
|
|
|
|
|
|
}
|
2025-04-23 10:44:43 -04:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-05-05 08:04:59 -04:00
|
|
|
|
|
|
|
|
|
|
public static bool TemplateRequiresUser(string template)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(template))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return template.Contains("#UserName#", StringComparison.Ordinal)
|
|
|
|
|
|
|| template.Contains("#UserEmail#", 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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool TemplateRequiresOrganization(string template)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(template))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return template.Contains("#OrganizationName#", StringComparison.Ordinal);
|
|
|
|
|
|
}
|
2025-04-23 10:44:43 -04:00
|
|
|
|
}
|