Esempio n. 1
0
    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        if (!context.ActionDescriptor.IsControllerAction() ||
            !context.ActionDescriptor.HasObjectResult())
        {
            await next();

            return;
        }

        if (!context.GetRequiredService <IOptions <AbpAspNetCoreMvcOptions> >().Value.AutoModelValidation)
        {
            await next();

            return;
        }

        if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault <DisableValidationAttribute>(context.ActionDescriptor.GetMethodInfo()) != null)
        {
            await next();

            return;
        }

        if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault <DisableValidationAttribute>(context.Controller.GetType()) != null)
        {
            await next();

            return;
        }

        if (context.ActionDescriptor.GetMethodInfo().DeclaringType != context.Controller.GetType())
        {
            var baseMethod = context.ActionDescriptor.GetMethodInfo();

            var overrideMethod = context.Controller.GetType().GetMethods().FirstOrDefault(x =>
                                                                                          x.DeclaringType == context.Controller.GetType() &&
                                                                                          x.Name == baseMethod.Name &&
                                                                                          x.ReturnType == baseMethod.ReturnType &&
                                                                                          x.GetParameters().Select(p => p.ToString()).SequenceEqual(baseMethod.GetParameters().Select(p => p.ToString())));

            if (overrideMethod != null)
            {
                if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault <DisableValidationAttribute>(overrideMethod) != null)
                {
                    await next();

                    return;
                }
            }
        }

        context.GetRequiredService <IModelStateValidator>().Validate(context.ModelState);
        await next();
    }
Esempio n. 2
0
    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        if (!context.ActionDescriptor.IsControllerAction())
        {
            await next();

            return;
        }

        var methodInfo     = context.ActionDescriptor.GetMethodInfo();
        var unitOfWorkAttr = UnitOfWorkHelper.GetUnitOfWorkAttributeOrNull(methodInfo);

        //context.HttpContext.Items["_ActionInfo"] = new ActionInfoInHttpContext
        //{
        //    IsObjectResult = context.ActionDescriptor.HasObjectResult()
        //};

        if (unitOfWorkAttr?.IsDisabled == true)
        {
            await next();

            return;
        }

        var options = CreateOptions(context, unitOfWorkAttr);

        var unitOfWorkManager = context.GetRequiredService <IUnitOfWorkManager>();

        //Trying to begin a reserved UOW by UnitOfWorkMiddleware
        if (unitOfWorkManager.TryBeginReserved(UnitOfWork.UnitOfWorkReservationName, options))
        {
            var result = await next();

            if (Succeed(result))
            {
                await SaveChangesAsync(context, unitOfWorkManager);
            }
            else
            {
                await RollbackAsync(context, unitOfWorkManager);
            }

            return;
        }

        using (var uow = unitOfWorkManager.Begin(options))
        {
            var result = await next();

            if (Succeed(result))
            {
                await uow.CompleteAsync(context.HttpContext.RequestAborted);
            }
            else
            {
                await uow.RollbackAsync(context.HttpContext.RequestAborted);
            }
        }
    }
Esempio n. 3
0
    /// <summary>
    /// 判断是否写日志
    /// </summary>
    /// <param name="context"></param>
    /// <param name="auditLog"></param>
    /// <param name="auditLogAction"></param>
    /// <returns></returns>
    private bool ShouldSaveAudit(ActionExecutingContext context, out AuditLogInfo auditLog, out AuditLogActionInfo auditLogAction)
    {
        auditLog       = null;
        auditLogAction = null;

        var options = context.GetRequiredService <IOptions <AuditingOptions> >().Value;

        if (!options.IsEnabled)
        {
            return(false);
        }

        if (!context.ActionDescriptor.IsControllerAction())
        {
            return(false);
        }

        var auditLogScope = context.GetService <IAuditingManager>()?.Current;

        if (auditLogScope == null)
        {
            return(false);
        }

        var auditingFactory = context.GetRequiredService <IAuditingFactory>();

        if (!auditingFactory.ShouldSaveAudit(context.ActionDescriptor.GetMethodInfo(), true))
        {
            return(false);
        }

        auditLog       = auditLogScope.Log;
        auditLogAction = auditingFactory.CreateAuditLogAction(
            auditLog,
            context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(),
            context.ActionDescriptor.AsControllerActionDescriptor().MethodInfo,
            context.ActionArguments
            );

        return(true);
    }
Esempio n. 4
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //TODO: Configuration to disable validation for controllers..?

            if (!context.ActionDescriptor.IsControllerAction() ||
                !context.ActionDescriptor.HasObjectResult())
            {
                await next();

                return;
            }

            context.GetRequiredService <IModelStateValidator>().Validate(context.ModelState);
            await next();
        }
Esempio n. 5
0
        private AbpUnitOfWorkOptions CreateOptions(ActionExecutingContext context, UnitOfWorkAttribute unitOfWorkAttribute)
        {
            var options = new AbpUnitOfWorkOptions();

            unitOfWorkAttribute?.SetOptions(options);

            if (unitOfWorkAttribute?.IsTransactional == null)
            {
                var abpUnitOfWorkDefaultOptions = context.GetRequiredService <IOptions <AbpUnitOfWorkDefaultOptions> >().Value;
                options.IsTransactional = abpUnitOfWorkDefaultOptions.CalculateIsTransactional(
                    autoValue: !string.Equals(context.HttpContext.Request.Method, HttpMethod.Get.Method, StringComparison.OrdinalIgnoreCase)
                    );
            }

            return(options);
        }
Esempio n. 6
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (!context.ActionDescriptor.IsControllerAction())
            {
                await next();

                return;
            }

            var methodInfo = context.ActionDescriptor.GetMethodInfo();

            using (AbpCrossCuttingConcerns.Applying(context.Controller, AbpCrossCuttingConcerns.FeatureChecking))
            {
                var methodInvocationFeatureCheckerService = context.GetRequiredService <IMethodInvocationFeatureCheckerService>();
                await methodInvocationFeatureCheckerService.CheckAsync(new MethodInvocationFeatureCheckerContext(methodInfo));

                await next();
            }
        }