/// <summary> /// Extracts the reference declarations referenced in the specified text /// </summary> /// <param name="value">The string to extract the attributes from</param> private void ExtractAttributeReferences(string value) { this.AttributeReferences.Clear(); Regex regex = new Regex(@"\[(?<preText>[^\]\[]*?)\{((?<referenceAttributeName>\w+)->)?(?<attributeName>\w+)(\:(?<modifier>([^\d]))?(?<count>\d?))?\}(?<postText>.*?)\]|\{((?<referenceAttributeName>\w+)->)?(?<attributeName>(sshma\:)?\w+)(\:(?<modifier>([^\d]))?(?<count>\d?))?\}", RegexOptions.ExplicitCapture); MatchCollection matches = regex.Matches(value); foreach (Match match in matches) { ReferencedAttribute reference = new ReferencedAttribute(); if (match.Groups["attributeName"] != null) { MASchema.ThrowOnMissingAttribute(match.Groups["attributeName"].Value); reference.AttributeName = match.Groups["attributeName"].Value; if (match.Groups["count"] != null && !string.IsNullOrWhiteSpace(match.Groups["count"].Value)) { if (MASchema.GetAttributeType(reference.AttributeName) != AttributeType.Reference) { throw new ArgumentException("A count value is only valid on a 'dn' or reference attribute"); } reference.Count = int.Parse(match.Groups["count"].Value); } if (match.Groups["modifier"] != null && !string.IsNullOrWhiteSpace(match.Groups["modifier"].Value)) { if (MASchema.GetAttributeType(reference.AttributeName) != AttributeType.Reference) { throw new ArgumentException("A modifier value is only valid on a 'dn' or reference attribute"); } reference.Modifier = match.Groups["modifier"].Value; } if (match.Groups["referenceAttributeName"] != null && !string.IsNullOrWhiteSpace(match.Groups["referenceAttributeName"].Value)) { throw new NotSupportedException("This MA does not support referenced attribute declarations"); } if (match.Groups["preText"] != null) { reference.PreReferenceString = match.Groups["preText"].Value; } if (match.Groups["postText"] != null) { reference.PostReferenceString = match.Groups["postText"].Value; } reference.Declaration = match.Value; this.AttributeReferences.Add(reference); } } }
/// <summary> /// Retrieves the reference value from the specified object /// </summary> /// <param name="csentry">The source object</param> /// <param name="reference">The ReferencedAttribute to obtain the value for</param> /// <param name="throwOnMissingAttribute">Sets a value indicating whether an exception should be thrown if an attribute is not present in the CSEntryChange, otherwise replaces the declaration with an empty string</param> /// <param name="obfuscatePasswordFields">A value indicating if passwords should be obfuscated in the resulting output. Typically used for logging purposes</param> /// <returns>The value of the specified reference on the source object</returns> private string GetSourceAttributeValue(CSEntryChange csentry, ReferencedAttribute reference, bool throwOnMissingAttribute, bool obfuscatePasswordFields) { string sourceAttributeValue = string.Empty; if (MASchema.GetAttributeType(reference.AttributeName) == AttributeType.Reference) { return(this.GetReferencedDNComponent(csentry, reference)); } if (csentry.AttributeChanges.Contains(reference.AttributeName)) { ValueChange valueChange = csentry.AttributeChanges[reference.AttributeName].ValueChanges.FirstOrDefault(t => t.ModificationType == ValueModificationType.Add); if (valueChange != null) { sourceAttributeValue = reference.PreReferenceString + valueChange.Value.ToString() + reference.PostReferenceString; } } else if (reference.AttributeName.StartsWith("sshma")) { switch (reference.AttributeName) { case "sshma:username": return(ManagementAgent.MAParameters.Username); case "sshma:password": if (obfuscatePasswordFields) { return("#hiddenpassword#"); } else { return(ManagementAgent.MAParameters.GetPassword()); } } } else { if (throwOnMissingAttribute) { throw new AttributeNotPresentException(reference.AttributeName); } } return(sourceAttributeValue); }
/// <summary> /// Gets an enumeration of expanded attribute value declarations for a multi-valued attribute /// </summary> /// <param name="csentry">The source object</param> /// <param name="multiValuedAttribute">The multi-valued attribute to expand</param> /// <param name="modificationType">Specifies which value modification types to expand during the enumeration</param> /// <returns>An enumeration of expanded declaration strings</returns> public IEnumerable <string> ExpandDeclarationWithMultiValued(CSEntryChange csentry, MASchemaAttribute multiValuedAttribute, ValueModificationType modificationType) { if (csentry == null) { yield return(this.DeclarationText); } string constructedValue = this.DeclarationText; foreach (ReferencedAttribute reference in this.AttributeReferences.Where(t => t.AttributeName != multiValuedAttribute.Name)) { if (MASchema.GetAttributeType(reference.AttributeName) == AttributeType.Reference) { constructedValue = constructedValue.Replace(reference.Declaration, this.GetReferencedDNComponent(csentry, reference)); } else { constructedValue = constructedValue.Replace(reference.Declaration, this.GetSourceAttributeValue(csentry, reference, false, false) ?? string.Empty); } } ReferencedAttribute mvattribute = this.AttributeReferences.FirstOrDefault(t => t.AttributeName == multiValuedAttribute.Name); if (mvattribute != null && csentry.AttributeChanges.Contains(multiValuedAttribute.Name)) { AttributeChange change = csentry.AttributeChanges[multiValuedAttribute.Name]; foreach (string value in this.GetSourceAttributeValues(change, mvattribute, modificationType)) { yield return(constructedValue.Replace(mvattribute.Declaration, value)); } } else { yield return(constructedValue); } yield break; }