Ejemplo n.º 1
0
        /// <summary>
        /// Writes the condition.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="apply">The apply.</param>
        protected virtual void WriteCondition(XmlWriter writer, XacmlExpression data)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            writer.WriteStartElement(XacmlConstants.Prefixes.Policy, XacmlConstants.ElementNames.Condition, this.Version.NamespacePolicy);
            this.WriteApplyType(writer, data.Property as XacmlApply); // TODO:
            writer.WriteEndElement();
        }
        /// <summary>
        /// Reads the rule.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        /// <exception cref="System.Xml.XmlException">
        /// Rule NotStartElement
        /// or
        /// RuleId IsNullOrEmpty
        /// or
        /// Effect IsNullEmpty
        /// </exception>
        /// <exception cref="XacmlSerializationException">Wrong XacmlEffectType value</exception>
        protected virtual XacmlRule ReadRule(XmlReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (!reader.IsStartElement(XacmlConstants.ElementNames.Rule, this.Version.NamespacePolicy))
            {
                throw ThrowHelperXml(reader, "Rule NotStartElement");
            }

            var gaRuleId = reader.GetAttribute("RuleId");

            if (string.IsNullOrEmpty(gaRuleId))
            {
                throw ThrowHelperXml(reader, "RuleId IsNullOrEmpty");
            }

            var gaEffect = reader.GetAttribute("Effect");

            if (string.IsNullOrEmpty(gaEffect))
            {
                throw ThrowHelperXml(reader, "Effect IsNullEmpty");
            }

            XacmlEffectType effectType = XacmlEffectType.Deny;

            if (string.Equals(gaEffect, "Deny", StringComparison.OrdinalIgnoreCase))
            {
                effectType = XacmlEffectType.Deny;
            }
            else if (string.Equals(gaEffect, "Permit", StringComparison.OrdinalIgnoreCase))
            {
                effectType = XacmlEffectType.Permit;
            }
            else
            {
                throw ThrowHelperXml(reader, "Wrong XacmlEffectType value");
            }

            reader.ReadStartElement(XacmlConstants.ElementNames.Rule, this.Version.NamespacePolicy);

            string description = null;

            if (reader.IsStartElement(XacmlConstants.ElementNames.Description, this.Version.NamespacePolicy))
            {
                description = reader.ReadElementContentAsString(XacmlConstants.ElementNames.Description, this.Version.NamespacePolicy);
            }

            XacmlTarget target = null;

            if (reader.IsStartElement(XacmlConstants.ElementNames.Target, this.Version.NamespacePolicy))
            {
                target = this.ReadTarget(reader);
            }

            XacmlExpression condition = null;

            if (reader.IsStartElement(XacmlConstants.ElementNames.Condition, this.Version.NamespacePolicy))
            {
                condition = this.ReadCondition(reader);
            }

            reader.ReadEndElement();
            return(new XacmlRule(gaRuleId, effectType)
            {
                Description = description, Target = target, Condition = condition
            });
        }