Exemplo n.º 1
0
        /// <summary>
        // Evaluate current requirement against given user and typed resource.
        /// </summary>
        /// <param name="context">authorization data context</param>
        /// <param name="resource">resource object</param>
        /// <returns>true if allowed</returns>
        protected override bool Evaluate(AuthZyinContext <TData> context, TResource resource)
        {
            if (context?.Data == null)
            {
                throw new ArgumentNullException("JsonPath requirement needs the data to be set in context");
            }

            if (resource == null)
            {
                throw new ArgumentNullException("JsonPath based requirement needs the resource object");
            }

            var dataJObject  = context.GetDataAsJObject();
            var resourceJObj = resource.GetResourceAsJObject();

            if (dataJObject == null || resourceJObj == null)
            {
                return(false);
            }

            // Create evluation context and delegate real evlauation to the evaluators
            var evaluatorContext = new EvaluatorContext(
                dataJObject,
                this.DataJPath,
                resourceJObj,
                this.ResourceJPath,
                this.Direction);

            return(EvaluatorMap[this.Operator].Evaluate(evaluatorContext));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Evaluates the prameters based on the context/resource as well as the operator
        /// </summary>
        /// <param name="context">evaluation context</param>
        /// <returns>true if success</returns>
        public override bool Evaluate(EvaluatorContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var left  = context.LeftJObject.SelectTokens(context.LeftJPath);
            var right = context.RightJObject.SelectToken(context.RightJPath);

            if (left == null || left.Count() == 0 || right == null)
            {
                return(false);
            }

            // Right must be a JValue
            if (!(right is JValue rightValue))
            {
                return(false);
            }

            // Left must be an array of JValue with the same type as right
            if (!left.All(x => x is JValue && x.Type == right.Type))
            {
                return(false);
            }

            return(left.Any(x => (x as JValue).CompareTo(rightValue) == 0));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Evaluates the prameters based on the context/resource as well as the operator
        /// </summary>
        /// <param name="context">evaluation context</param>
        /// <returns>true if success</returns>
        public sealed override bool Evaluate(EvaluatorContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var left  = context.LeftJObject.SelectToken(context.LeftJPath);
            var right = context.RightJObject.SelectToken(context.RightJPath);

            if (left == null || right == null)
            {
                return(false);
            }

            if (!(left is JValue leftValue) || !(right is JValue rightValue))
            {
                // The left token or right token is not a primitive value
                return(false);
            }

            return(leftValue.Type == rightValue.Type && this.EvaluateValues(leftValue, rightValue));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Evaluates the prameters based on the context/resource as well as the operator
 /// </summary>
 /// <param name="context">evaluation context</param>
 /// <returns>true if success</returns>
 public abstract bool Evaluate(EvaluatorContext context);