/// <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, ValueWrapperResource <TValue> resource)
        {
            if (resource != null)
            {
                throw new InvalidOperationException("Unexpected resource passed to JsonPathConstantRequirement evaluation");
            }

            // Call base and replace the resource with the dummy resource wrapping around the const value
            return(base.Evaluate(context, this.valueWrapperResource));
        }
Beispiel #2
0
        public void ResourceJObjectBehavior()
        {
            var resource = new ValueWrapperResource <int>(5);

            var jObj1 = resource.GetResourceAsJObject();
            var jObj2 = resource.GetResourceAsJObject();

            // Make sure we don't do JObject conversion twice
            Assert.Same(jObj1, jObj2);
            Assert.Equal(5, resource.Value);

            // Make sure data is correct
            var convertedBack = jObj1.ToObject <ValueWrapperResource <int> >();

            Assert.Equal(5, convertedBack.Value);
        }
        /// <summary>
        /// Initializes a new instance of JsonPathRequirement which uses Json Path to check requirement satisfaction.
        /// 1. The Direction is always set to ContextToResource.
        /// 2. The resource type is always ConstantWrapperResource<TConst>
        /// 3. Passed in resource object will be ignored during evaluation and replaced by a on the fly instance of ConstantWrapperResource<TConst>
        /// </summary>
        /// <param name="operatorType">Requirement operator type</param>
        /// <param name="dataPath">jsonPath to context object</param>
        /// <param name="value">const to compare with</param>
        public JsonPathConstantRequirement(OperatorType operatorType, string dataPath, TValue value)
            : base(
                operatorType,
                dataPath,
                ValueWrapperResource <TValue> .ValueJsonPath,
                Direction.ContextToResource)
        {
            // We don't allow null as the value, so boxing it to compare.
            if (value as object == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            this.ConstValue           = value;
            this.valueWrapperResource = new ValueWrapperResource <TValue>(value);
        }