/// <inheritdoc/>
        public Task <bool> AuthorizeAsync(OperationContext context, CancellationToken cancellationToken)
        {
            Ensure.NotNull(context, nameof(context));
            var result = true;

            var returnType = typeof(bool);
            var methodName = ConventionBasedMethodNameFactory.GetFunctionMethodName(context, RestierPipelineState.Authorization, RestierOperationMethod.Execute);
            var method     = targetType.GetQualifiedMethod(methodName);

            if (method != null && method.IsFamily && method.ReturnType == returnType)
            {
                object target = null;
                if (!method.IsStatic)
                {
                    target = context.Api;
                    if (target == null || !targetType.IsInstanceOfType(target))
                    {
                        return(Task.FromResult(result));
                    }
                }

                var parameters = method.GetParameters();
                if (parameters.Length == 0)
                {
                    result = (bool)method.Invoke(target, null);
                }
            }

            return(Task.FromResult(result));
        }
        private Task InvokeProcessorMethodAsync(OperationContext context, RestierPipelineState pipelineState)
        {
            var parameters         = context.ParameterValues?.ToArray() ?? Array.Empty <object>();
            var expectedMethodName = ConventionBasedMethodNameFactory.GetFunctionMethodName(context, pipelineState, RestierOperationMethod.Execute);
            var expectedMethod     = targetApiType.GetQualifiedMethod(expectedMethodName);

            if (expectedMethod == null)
            {
                return(Task.CompletedTask);
            }

            if (!expectedMethod.IsFamily && !expectedMethod.IsFamilyOrAssembly)
            {
                Trace.WriteLine($"Restier Filter found '{expectedMethod}' but it is inaccessible due to its protection level. Your method will not be called until you change it to 'protected internal'.");
                return(Task.CompletedTask);
            }

            if (expectedMethod.ReturnType != typeof(void) && !typeof(Task).IsAssignableFrom(expectedMethod.ReturnType))
            {
                Trace.WriteLine($"Restier Filter found '{expectedMethod}' but it does not return void or a Task. Your method will not be called until you correct the return type.");
                return(Task.CompletedTask);
            }

            object target = null;

            if (!expectedMethod.IsStatic)
            {
                target = context.Api;
                if (!targetApiType.IsInstanceOfType(target))
                {
                    Trace.WriteLine("The Restier API is of the incorrect type.");
                    return(Task.CompletedTask);
                }
            }

            var methodParameters = expectedMethod.GetParameters();

            if (ParametersMatch(methodParameters, parameters))
            {
                var result = expectedMethod.Invoke(target, parameters);
                if (result is Task resultTask)
                {
                    return(resultTask);
                }
            }

            Trace.WriteLine($"Restier Authorizer found '{expectedMethod}', but it has an incorrect number of arguments or the types don't match. The number of arguments should be 1.");
            return(Task.CompletedTask);
        }
        /// <inheritdoc/>
        public Task <bool> AuthorizeAsync(OperationContext context, CancellationToken cancellationToken)
        {
            Ensure.NotNull(context, nameof(context));
            var result = true;

            var returnType = typeof(bool);
            var methodName = ConventionBasedMethodNameFactory.GetFunctionMethodName(context, RestierPipelineState.Authorization, RestierOperationMethod.Execute);
            var method     = targetApiType.GetQualifiedMethod(methodName);

            if (method == null)
            {
                return(Task.FromResult(result));
            }

            if (!method.IsFamily && !method.IsFamilyOrAssembly)
            {
                Trace.WriteLine($"Restier Authorizer found '{methodName}' but it is inaccessible due to its protection level. Your method will not be called until you change it to 'protected internal'.");
                return(Task.FromResult(result));
            }

            if (method.ReturnType != returnType)
            {
                Trace.WriteLine($"Restier Authorizer found '{methodName}' but it does not return a boolean value. Your method will not be called until you correct the return type.");
                return(Task.FromResult(result));
            }

            object target = null;

            if (!method.IsStatic)
            {
                target = context.Api;
                if (!targetApiType.IsInstanceOfType(target))
                {
                    Trace.WriteLine("The Restier API is of the incorrect type.");
                    return(Task.FromResult(result));
                }
            }

            var parameters = method.GetParameters();

            if (parameters.Length == 0)
            {
                result = (bool)method.Invoke(target, null);
            }

            Trace.WriteLine($"Restier Authorizer found '{methodName}', but it has an incorrect number of arguments. The number of arguments should be 0.");
            return(Task.FromResult(result));
        }
Exemplo n.º 4
0
        private Task InvokeProcessorMethodAsync(OperationContext context, RestierPipelineState pipelineState)
        {
            var methodName = ConventionBasedMethodNameFactory.GetFunctionMethodName(context, pipelineState, RestierOperationMethod.Execute);

            object[] parameters = null;
            if (context.ParameterValues != null)
            {
                parameters = context.ParameterValues.ToArray();
            }

            var method = targetType.GetQualifiedMethod(methodName);

            if (method != null && (method.ReturnType == typeof(void) || typeof(Task).IsAssignableFrom(method.ReturnType)))
            {
                object target = null;
                if (!method.IsStatic)
                {
                    target = context.Api;
                    if (target == null || !targetType.IsInstanceOfType(target))
                    {
                        return(Task.WhenAll());
                    }
                }

                var methodParameters = method.GetParameters();
                if (ParametersMatch(methodParameters, parameters))
                {
                    var result = method.Invoke(target, parameters);
                    if (result is Task resultTask)
                    {
                        return(resultTask);
                    }
                }
            }

            return(Task.WhenAll());
        }