2025-05-27 08:28:50 -04:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
|
|
|
|
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-09-10 14:17:45 -04:00
|
|
|
|
var property = type.GetProperty(propertyName);
|
|
|
|
|
|
|
|
|
|
|
|
if (property == null)
|
2025-07-01 08:52:38 -04:00
|
|
|
|
{
|
2025-09-10 14:17:45 -04:00
|
|
|
|
return match.Value; // Return unknown keys as keys - i.e. #Key#
|
2025-07-01 08:52:38 -04:00
|
|
|
|
}
|
2025-09-10 14:17:45 -04:00
|
|
|
|
|
2025-11-24 10:30:45 -05:00
|
|
|
|
return property.GetValue(values)?.ToString() ?? string.Empty;
|
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)
|
2025-11-24 10:30:45 -05:00
|
|
|
|
|| template.Contains("#UserEmail#", StringComparison.Ordinal)
|
|
|
|
|
|
|| template.Contains("#UserType#", StringComparison.Ordinal);
|
2025-05-05 08:04:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool TemplateRequiresActingUser(string template)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(template))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return template.Contains("#ActingUserName#", StringComparison.Ordinal)
|
2025-11-24 10:30:45 -05:00
|
|
|
|
|| 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);
|
2025-05-05 08:04:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|