/// <summary> /// Writes the attribute designator. /// </summary> /// <param name="writer">The writer.</param> /// <param name="data">The data.</param> protected virtual void WriteAttributeDesignator(XmlWriter writer, XacmlAttributeDesignator data) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (data == null) { throw new ArgumentNullException(nameof(data)); } Action <string, dynamic, bool> action = (designatorType, attributeDesignator, writeSubjectCategory) => { writer.WriteStartElement(XacmlConstants.Prefixes.Policy, designatorType, this.Version.NamespacePolicy); writer.WriteAttributeString(XacmlConstants.AttributeNames.AttributeId, attributeDesignator.AttributeId.OriginalString); writer.WriteAttributeString(XacmlConstants.AttributeNames.DataType, attributeDesignator.DataType.OriginalString); if (attributeDesignator.Issuer != null) { writer.WriteAttributeString(XacmlConstants.AttributeNames.Issuer, attributeDesignator.Issuer); } if (attributeDesignator.MustBePresent != null) { writer.WriteAttributeString(XacmlConstants.AttributeNames.MustBePresent, XmlConvert.ToString(attributeDesignator.MustBePresent)); } if (writeSubjectCategory && attributeDesignator.Category != null) { writer.WriteAttributeString(XacmlConstants.AttributeNames.SubjectCategory, attributeDesignator.Category.OriginalString); } writer.WriteEndElement(); }; var subjectAttributeDesignator = data as XacmlSubjectAttributeDesignator; if (subjectAttributeDesignator != null) { action(XacmlConstants.ElementNames.SubjectAttributeDesignator, subjectAttributeDesignator, true); } var resourceAttributeDesignator = data as XacmlResourceAttributeDesignator; if (resourceAttributeDesignator != null) { action(XacmlConstants.ElementNames.ResourceAttributeDesignator, resourceAttributeDesignator, false); } var actionAttributeDesignator = data as XacmlActionAttributeDesignator; if (actionAttributeDesignator != null) { action(XacmlConstants.ElementNames.ActionAttributeDesignator, actionAttributeDesignator, false); } }
private static void AssertAttributeDesignatorEqual(XacmlAttributeDesignator expected, XacmlAttributeDesignator actual) { Assert.NotNull(actual); Assert.NotNull(expected); Assert.Equal(expected.AttributeId.OriginalString, actual.AttributeId.OriginalString); Assert.Equal(expected.Category.OriginalString, actual.Category.OriginalString); Assert.Equal(expected.DataType.OriginalString, actual.DataType.OriginalString); Assert.Equal(expected.Issuer, actual.Issuer); Assert.Equal(expected.MustBePresent, actual.MustBePresent); }
private static void WriteAttributeDesignator(XmlWriter writer, XacmlAttributeDesignator xacmlAttributeDesignator) { Guard.ArgumentNotNull(writer, nameof(writer)); Guard.ArgumentNotNull(xacmlAttributeDesignator, nameof(xacmlAttributeDesignator)); writer.WriteStartElement(XacmlConstants.Prefixes.Xacml, XacmlConstants.ElementNames.AttributeDesignator, Xacml30Constants.NameSpaces.Policy); writer.WriteAttributeString(XacmlConstants.AttributeNames.AttributeId, xacmlAttributeDesignator.AttributeId.OriginalString); writer.WriteAttributeString(XacmlConstants.AttributeNames.Category, xacmlAttributeDesignator.Category.OriginalString); writer.WriteAttributeString(XacmlConstants.AttributeNames.DataType, xacmlAttributeDesignator.DataType.OriginalString); if (xacmlAttributeDesignator.MustBePresent != null) { writer.WriteAttributeString(XacmlConstants.AttributeNames.MustBePresent, xacmlAttributeDesignator.MustBePresent.ToString().ToLower()); } if (!string.IsNullOrEmpty(xacmlAttributeDesignator.Issuer)) { writer.WriteAttributeString(XacmlConstants.AttributeNames.Issuer, xacmlAttributeDesignator.Issuer); } writer.WriteEndElement(); }
/// <summary> /// Reads the match. /// </summary> /// <param name="reader">The reader.</param> /// <returns></returns> protected virtual XacmlMatch ReadMatch(XmlReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var gaMatchId = reader.GetAttribute("MatchId"); if (string.IsNullOrEmpty(gaMatchId)) { throw ThrowHelperXml(reader, "MatchId IsNullOrEmpty"); } Func <string, string, ReadElement <XacmlAttributeDesignator>, XacmlMatch> read = (matchType, designatorType, readFuncAttributeDesignator) => { XacmlMatch ret = null; if (!reader.IsStartElement(matchType, this.Version.NamespacePolicy)) { throw ThrowHelperXml(reader, string.Format("{0} NotStartElement", matchType)); } reader.ReadStartElement(matchType, this.Version.NamespacePolicy); var attributeValue = ReadAttributeValue(reader); IDictionary <Tuple <string, string>, Action> dicts = null; XacmlAttributeSelector sel = null; XacmlAttributeDesignator des = null; dicts = new Dictionary <Tuple <string, string>, Action>() { { new Tuple <string, string>(designatorType, this.Version.NamespacePolicy), () => des = readFuncAttributeDesignator(reader) }, { new Tuple <string, string>(XacmlConstants.ElementNames.AttributeSelector, this.Version.NamespacePolicy), () => sel = ReadAttributeSelector(reader) }, }; this.ReadChoice(reader, dicts, isRequired: true); reader.ReadEndElement(); switch (matchType) { case XacmlConstants.ElementNames.ActionMatch: ret = des != null ? new XacmlActionMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, (XacmlActionAttributeDesignator)des) : new XacmlActionMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, sel); break; case XacmlConstants.ElementNames.SubjectMatch: ret = des != null ? new XacmlSubjectMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, (XacmlSubjectAttributeDesignator)des) : new XacmlSubjectMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, sel); break; case XacmlConstants.ElementNames.ResourceMatch: ret = des != null ? new XacmlResourceMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, (XacmlResourceAttributeDesignator)des) : new XacmlResourceMatch(new Uri(gaMatchId, UriKind.RelativeOrAbsolute), attributeValue, sel); break; default: break; } return(ret); }; if (reader.LocalName == XacmlConstants.ElementNames.ActionMatch) { return(read(reader.LocalName, XacmlConstants.ElementNames.ActionAttributeDesignator, ReadAttributeDesignator)); } else if (reader.LocalName == XacmlConstants.ElementNames.SubjectMatch) { return(read(reader.LocalName, XacmlConstants.ElementNames.SubjectAttributeDesignator, ReadAttributeDesignator)); } else if (reader.LocalName == XacmlConstants.ElementNames.ResourceMatch) { return(read(reader.LocalName, XacmlConstants.ElementNames.ResourceAttributeDesignator, ReadAttributeDesignator)); } else { throw ThrowHelperXml(reader, string.Format("Incorrect start element. redaer.LocalName={0} ReadMatch", reader.LocalName)); } }