Esempio n. 1
0
        /// <summary>
        /// Evaluates the policy.
        /// </summary>
        /// <param name="context">The evaluation context instance.</param>
        /// <returns>The decission result for this policy.</returns>
        public Decision Evaluate(EvaluationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            context.Trace("Evaluating policy: {0}", _policy.Description);
            context.AddIndent();
            context.CurrentPolicy = this;
            try
            {
                // Evaluate the variables
                if (this._policy.SchemaVersion == XacmlVersion.Version20)
                {
                    if (_variables == null)
                    {
                        context.Trace("Evaluating variables...");
                        _variables = new Hashtable();

                        foreach (pol.VariableDefinitionElement variableDef in _policy.VariableDefinitions.Values)
                        {
                            VariableDefinition variable = new VariableDefinition(variableDef);
                            _variables.Add(variableDef.Id, variable);
                        }
                    }
                }

                // Matches the target.
                TargetEvaluationValue targetEvaluationValue = Match(context);

                // If the target matches.
                if (targetEvaluationValue == TargetEvaluationValue.Match)
                {
                    context.Trace("Rule combination algorithm: {0}", _policy.RuleCombiningAlgorithm);

                    // Evaluate all rules and apply rule combination
                    inf.IRuleCombiningAlgorithm rca = EvaluationEngine.CreateRuleCombiningAlgorithm(_policy.RuleCombiningAlgorithm);
                    _evaluationValue = rca.Evaluate(context, _rules);
                }
                else if (targetEvaluationValue == TargetEvaluationValue.NoMatch)
                {
                    _evaluationValue = Decision.NotApplicable;
                }
                else if (targetEvaluationValue == TargetEvaluationValue.Indeterminate)
                {
                    _evaluationValue = Decision.Indeterminate;
                }

                context.Trace("Policy: {0}", _evaluationValue);

                // Copy all the obligations.
                _obligations = new pol.ObligationCollection();
                if (_evaluationValue != Decision.Indeterminate &&
                    _evaluationValue != Decision.NotApplicable &&
                    _policy.Obligations != null && _policy.Obligations.Count != 0)
                {
                    foreach (pol.ObligationElement obl in _policy.Obligations)
                    {
                        if ((obl.FulfillOn == pol.Effect.Deny && _evaluationValue == Decision.Deny) ||
                            (obl.FulfillOn == pol.Effect.Permit && _evaluationValue == Decision.Permit))
                        {
                            context.Trace("Adding obligation: {0} ", obl.ObligationId);
                            _obligations.Add(obl);
                        }
                    }
                }

                return(_evaluationValue);
            }
            finally
            {
                context.RemoveIndent();
                context.CurrentPolicy = null;
            }
        }