/// <summary> /// Gets the specific DN component referenced in the ReferencedAttribute object /// </summary> /// <param name="dn">The DN string of an object</param> /// <param name="attribute">The ReferencedAttribute containing the DN and extraction parameters</param> /// <returns>A string containing the components of the DN referenced in the ReferenceAttribute object</returns> private string GetReferencedDNComponent(string dn, ReferencedAttribute attribute) { string valueToInsert = string.Empty; if (attribute.Count == 0) { valueToInsert = dn; } else { string[] split = DetachedUtils.SplitDN(dn); if (attribute.Count - 1 > split.Length) { throw new ArgumentException("The referenced attribute specifies a DN component that doesnt exist: " + attribute.Declaration); } string rdn = split[attribute.Count - 1]; if (attribute.Modifier == "+") { valueToInsert = rdn; } else if (attribute.Modifier == "$") { int indexOfEquals = rdn.IndexOf("="); valueToInsert = rdn.Remove(0, indexOfEquals + 1); } } return(valueToInsert); }
/// <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; }
/// <summary> /// Gets the specific DN component referenced in the ReferencedAttribute object /// </summary> /// <param name="csentry">The source object</param> /// <param name="attribute">The ReferencedAttribute containing the DN and extraction parameters</param> /// <returns>A string containing the components of the DN referenced in the ReferenceAttribute object</returns> private string GetReferencedDNComponent(CSEntryChange csentry, ReferencedAttribute attribute) { return(this.GetReferencedDNComponent(csentry.DN, attribute)); }
/// <summary> /// Gets the specific DN component referenced in the ReferencedAttribute object /// </summary> /// <param name="csentry">The source object</param> /// <param name="attribute">The ReferencedAttribute containing the DN and extraction parameters</param> /// <returns>A string containing the components of the DN referenced in the ReferenceAttribute object</returns> private string GetReferencedDNComponent(CSEntry csentry, ReferencedAttribute attribute) { return(this.GetReferencedDNComponent(csentry.DN.ToString(), attribute)); }
/// <summary> /// Retrieves the source attribute values from the specified attribute change /// </summary> /// <param name="attributeChange">The attribute change to enumerate the values from</param> /// <param name="reference">The declaration of the referenced attribute</param> /// <param name="modificationType">The value modification type to enumerate</param> /// <returns>An enumeration of expanded string values</returns> private IEnumerable <string> GetSourceAttributeValues(AttributeChange attributeChange, ReferencedAttribute reference, ValueModificationType modificationType) { IEnumerable <ValueChange> valueChanges = attributeChange.ValueChanges.Where(t => t.ModificationType == (modificationType == ValueModificationType.Unconfigured ? ValueModificationType.Add : modificationType)); foreach (ValueChange valueChange in valueChanges) { if (attributeChange.DataType == AttributeType.Reference) { yield return(reference.PreReferenceString + this.GetReferencedDNComponent(valueChange.Value.ToString(), reference) + reference.PostReferenceString); } else { yield return(reference.PreReferenceString + valueChange.Value.ToString() + reference.PostReferenceString); } } yield break; }