Refine NTFS group name shortening
This commit is contained in:
@@ -13,6 +13,9 @@ namespace C4IT_IAM_Engine
|
|||||||
{
|
{
|
||||||
public const int MaxAdGroupNameLength = 64;
|
public const int MaxAdGroupNameLength = 64;
|
||||||
public const int MaxAdGroupLoopDigits = 3;
|
public const int MaxAdGroupLoopDigits = 3;
|
||||||
|
private const int MinLeadingRelativePathSegmentLength = 3;
|
||||||
|
private const int MinSingleLeadingRelativePathSegmentLength = 2;
|
||||||
|
private const int MinLastRelativePathSegmentLength = 12;
|
||||||
|
|
||||||
public sealed class BoundedTemplateContext
|
public sealed class BoundedTemplateContext
|
||||||
{
|
{
|
||||||
@@ -75,13 +78,13 @@ namespace C4IT_IAM_Engine
|
|||||||
{
|
{
|
||||||
var effectiveSegments = (sanitizedSegments ?? Array.Empty<string>()).Where(i => i != null).ToArray();
|
var effectiveSegments = (sanitizedSegments ?? Array.Empty<string>()).Where(i => i != null).ToArray();
|
||||||
var effectiveFolderName = folderName ?? string.Empty;
|
var effectiveFolderName = folderName ?? string.Empty;
|
||||||
var availableLength = Math.Max(1, maxLength - GetLoopReservationLength(templateValue));
|
|
||||||
var originalValue = MaterializeTemplateValue(templateValue, allowRelativePath, defaultRelativePath, effectiveSegments, effectiveFolderName, replacementTags);
|
var originalValue = MaterializeTemplateValue(templateValue, allowRelativePath, defaultRelativePath, effectiveSegments, effectiveFolderName, replacementTags);
|
||||||
|
var measuredValue = MaterializeTemplateValueForLength(templateValue, allowRelativePath, defaultRelativePath, effectiveSegments, effectiveFolderName, replacementTags);
|
||||||
var usesRelativePath = allowRelativePath && Regex.IsMatch(templateValue ?? string.Empty, @"{{\s*RELATIVEPATH", RegexOptions.IgnoreCase);
|
var usesRelativePath = allowRelativePath && Regex.IsMatch(templateValue ?? string.Empty, @"{{\s*RELATIVEPATH", RegexOptions.IgnoreCase);
|
||||||
var usesName = Regex.IsMatch(templateValue ?? string.Empty, @"{{\s*NAME\s*}}", RegexOptions.IgnoreCase);
|
var usesName = Regex.IsMatch(templateValue ?? string.Empty, @"{{\s*NAME\s*}}", RegexOptions.IgnoreCase);
|
||||||
var strategy = string.Empty;
|
var strategy = string.Empty;
|
||||||
|
|
||||||
while (originalValue.Length > availableLength)
|
while (measuredValue.Length > maxLength)
|
||||||
{
|
{
|
||||||
var changed = false;
|
var changed = false;
|
||||||
|
|
||||||
@@ -103,18 +106,17 @@ namespace C4IT_IAM_Engine
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
originalValue = MaterializeTemplateValue(templateValue, allowRelativePath, defaultRelativePath, effectiveSegments, effectiveFolderName, replacementTags);
|
originalValue = MaterializeTemplateValue(templateValue, allowRelativePath, defaultRelativePath, effectiveSegments, effectiveFolderName, replacementTags);
|
||||||
|
measuredValue = MaterializeTemplateValueForLength(templateValue, allowRelativePath, defaultRelativePath, effectiveSegments, effectiveFolderName, replacementTags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var initialValue = MaterializeTemplateValue(templateValue, allowRelativePath, defaultRelativePath, sanitizedSegments, folderName, replacementTags);
|
||||||
var result = new BoundedTemplateContext
|
var result = new BoundedTemplateContext
|
||||||
{
|
{
|
||||||
SanitizedSegments = effectiveSegments,
|
SanitizedSegments = effectiveSegments,
|
||||||
FolderName = effectiveSegments.Length > 0 ? effectiveSegments[effectiveSegments.Length - 1] : effectiveFolderName,
|
FolderName = effectiveSegments.Length > 0 ? effectiveSegments[effectiveSegments.Length - 1] : effectiveFolderName,
|
||||||
OriginalValue = MaterializeTemplateValue(templateValue, allowRelativePath, defaultRelativePath, sanitizedSegments, folderName, replacementTags),
|
OriginalValue = initialValue,
|
||||||
FinalValue = originalValue,
|
FinalValue = originalValue,
|
||||||
WasShortened = !string.Equals(
|
WasShortened = !string.Equals(initialValue, originalValue, StringComparison.Ordinal),
|
||||||
MaterializeTemplateValue(templateValue, allowRelativePath, defaultRelativePath, sanitizedSegments, folderName, replacementTags),
|
|
||||||
originalValue,
|
|
||||||
StringComparison.Ordinal),
|
|
||||||
Strategy = strategy
|
Strategy = strategy
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -122,14 +124,14 @@ namespace C4IT_IAM_Engine
|
|||||||
{
|
{
|
||||||
cLogManager.DefaultLogger.LogEntry(
|
cLogManager.DefaultLogger.LogEntry(
|
||||||
LogLevels.Warning,
|
LogLevels.Warning,
|
||||||
$"AD-Gruppenname gekuerzt ({logContext}): '{result.OriginalValue}' ({result.OriginalValue.Length}) -> '{result.FinalValue}' ({result.FinalValue.Length}), Strategie: {result.Strategy}, Limit: {availableLength}.");
|
$"AD-Gruppenname gekuerzt ({logContext}): '{result.OriginalValue}' ({GetMeasuredTemplateLength(result.OriginalValue)}) -> '{result.FinalValue}' ({GetMeasuredTemplateLength(result.FinalValue)}), Strategie: {result.Strategy}, Limit: {maxLength}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.FinalValue.Length > availableLength)
|
if (measuredValue.Length > maxLength)
|
||||||
{
|
{
|
||||||
cLogManager.DefaultLogger.LogEntry(
|
cLogManager.DefaultLogger.LogEntry(
|
||||||
LogLevels.Warning,
|
LogLevels.Warning,
|
||||||
$"AD-Gruppenname ueberschreitet weiterhin das sichere Limit ({logContext}): '{result.FinalValue}' ({result.FinalValue.Length}), Limit: {availableLength}.");
|
$"AD-Gruppenname ueberschreitet weiterhin das sichere Limit ({logContext}): '{result.FinalValue}' ({measuredValue.Length}), Limit: {maxLength}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -176,6 +178,35 @@ namespace C4IT_IAM_Engine
|
|||||||
.ToUpper();
|
.ToUpper();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string MaterializeTemplateValueForLength(
|
||||||
|
string templateValue,
|
||||||
|
bool allowRelativePath,
|
||||||
|
string defaultRelativePath,
|
||||||
|
string[] sanitizedSegments,
|
||||||
|
string folderName,
|
||||||
|
IDictionary<string, string> replacementTags)
|
||||||
|
{
|
||||||
|
return NormalizeLoopPlaceholderLength(
|
||||||
|
MaterializeTemplateValue(templateValue, allowRelativePath, defaultRelativePath, sanitizedSegments, folderName, replacementTags));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string NormalizeLoopPlaceholderLength(string templateValue)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(templateValue))
|
||||||
|
return templateValue ?? string.Empty;
|
||||||
|
|
||||||
|
return Regex.Replace(
|
||||||
|
templateValue,
|
||||||
|
@"{{(?<prefix>[^}]*)(?<loop>LOOP)(?<postfix>[^{]*)}}",
|
||||||
|
match => match.Groups["prefix"].Value + new string('9', MaxAdGroupLoopDigits) + match.Groups["postfix"].Value,
|
||||||
|
RegexOptions.IgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int GetMeasuredTemplateLength(string templateValue)
|
||||||
|
{
|
||||||
|
return NormalizeLoopPlaceholderLength(templateValue).Length;
|
||||||
|
}
|
||||||
|
|
||||||
private static int GetLoopReservationLength(string templateValue)
|
private static int GetLoopReservationLength(string templateValue)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(templateValue))
|
if (string.IsNullOrWhiteSpace(templateValue))
|
||||||
@@ -196,8 +227,10 @@ namespace C4IT_IAM_Engine
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
var lastIndex = segments.Length - 1;
|
var lastIndex = segments.Length - 1;
|
||||||
|
if (segments.Length > 2)
|
||||||
|
{
|
||||||
var candidateIndex = -1;
|
var candidateIndex = -1;
|
||||||
var candidateLength = 1;
|
var candidateLength = MinLeadingRelativePathSegmentLength;
|
||||||
|
|
||||||
for (var i = 0; i < lastIndex; i++)
|
for (var i = 0; i < lastIndex; i++)
|
||||||
{
|
{
|
||||||
@@ -214,12 +247,39 @@ namespace C4IT_IAM_Engine
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (segments.Length > 1)
|
|
||||||
{
|
|
||||||
segments = segments.Skip(1).ToArray();
|
segments = segments.Skip(1).ToArray();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (segments.Length == 2)
|
||||||
|
{
|
||||||
|
if (segments[0].Length > MinSingleLeadingRelativePathSegmentLength)
|
||||||
|
{
|
||||||
|
segments[0] = segments[0].Substring(0, segments[0].Length - 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (segments[lastIndex].Length > MinLastRelativePathSegmentLength)
|
||||||
|
{
|
||||||
|
segments[lastIndex] = segments[lastIndex].Substring(0, segments[lastIndex].Length - 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (segments[0].Length > 1)
|
||||||
|
{
|
||||||
|
segments[0] = segments[0].Substring(0, segments[0].Length - 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (segments[lastIndex].Length > 1)
|
||||||
|
{
|
||||||
|
segments[lastIndex] = segments[lastIndex].Substring(0, segments[lastIndex].Length - 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (segments[lastIndex].Length > 1)
|
if (segments[lastIndex].Length > 1)
|
||||||
{
|
{
|
||||||
segments[lastIndex] = segments[lastIndex].Substring(0, segments[lastIndex].Length - 1);
|
segments[lastIndex] = segments[lastIndex].Substring(0, segments[lastIndex].Length - 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user