Exemple #1
0
        private void ParseRequestOnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(true).Any(m => m.GetType() == typeof(IgnoreAuditAttribute)))
            {
                return;
            }

            if (filterContext.ActionDescriptor.GetCustomAttributes(true).Any(m => m.GetType() == typeof(IgnoreAuditAttribute)))
            {
                return;
            }

            if (!(filterContext.Result is ViewResultBase || filterContext.Result is JsonResult || filterContext.Result is RedirectResult || filterContext.Result is ContentResult))
            {
                return;
            }

            if (_sessionContext.UserSession == null)
            {
                return;
            }

            dynamic model = null;

            if ((filterContext.Result is ViewResultBase))
            {
                model = ((ViewResultBase)filterContext.Result).Model;
            }
            if ((filterContext.Result is JsonResult))
            {
                model = ((JsonResult)filterContext.Result).Data;
            }
            if ((filterContext.Result is RedirectResult))
            {
                model = ((RedirectResult)filterContext.Result);
            }
            if ((filterContext.Result is ContentResult))
            {
                model = ((ContentResult)filterContext.Result);
            }

            if (model == null)
            {
                return;
            }

            _activityType = Type.Retrieved;

            var customerId = 0;
            var customer   = filterContext.ActionDescriptor.GetParameters().FirstOrDefault(x => x.ParameterName.ToLower().Equals("customerid"));

            if (customer != null)
            {
                customerId = Convert.ToInt32(filterContext.Controller.ValueProvider.GetValue(customer.ParameterName).AttemptedValue);
            }

            HttpRequestBase request = filterContext.HttpContext.Request;

            if (_settings.AuditEnabled)
            {
                if (request.RequestType.ToUpper() == "GET")
                {
                    var logModel = _auditLogFilterHelper.GetActivityLogEditModel(request, model, _activityType.ToString());

                    if (logModel != null)
                    {
                        logModel.CustomerId = customerId;

                        var modelType = ReflectionHelper.GetModelType(model);

                        modelType = GetModelTypeForExceptions(model, modelType);

                        _auditLogFilterHelper.AsyncLogModel(modelType, logModel, model);
                    }
                    else
                    {
                        _logger.Info("Session is null while Saving Audit Data");
                    }
                }
                else if ((request.RequestType.ToUpper() == "POST" || request.RequestType.ToUpper() == "DELETE") &&
                         (filterContext.ActionDescriptor.ActionName.ToLower().Contains("remove") ||
                          filterContext.ActionDescriptor.ActionName.ToLower().Contains("delete")))
                {
                    //if (_activityLogDeleteService.IsDeleted(filterContext))
                    //{
                    //    var logModel = CreateModel(request);

                    //    logModel.CustomerId = customerId;

                    //    _activityLogService.LogDeleteActivity(logModel, _deletedEntity);
                    //}
                }
                else if (filterContext.ActionDescriptor.GetCustomAttributes(true).Any(m => m.GetType() == typeof(MustAuditAttribute)))
                {
                    var logModel = _auditLogFilterHelper.GetActivityLogEditModel(request, model, _activityType.ToString());

                    if (logModel != null)
                    {
                        logModel.CustomerId = customerId;

                        var modelType = ReflectionHelper.GetModelType(model);

                        _auditLogFilterHelper.AsyncLogModel(modelType, logModel, model);
                    }
                    else
                    {
                        _logger.Info("Session is null while Saving Audit Data");
                    }
                }
            }
        }
Exemple #2
0
        private void ParseRequestOnActionExecuted(HttpActionExecutedContext actionContext)
        {
            var request = HttpContext.Current.Request;

            if (actionContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerType.GetCustomAttributes(true).Any(m => m.GetType() == typeof(IgnoreAuditAttribute)))
            {
                return;
            }

            if (actionContext.ActionContext.ActionDescriptor.ReturnType.GetCustomAttributes(true).Any(m => m.GetType() == typeof(IgnoreAuditAttribute)))
            {
                return;
            }

            if (!_settings.AuditEnabled)
            {
                return;
            }

            if (_sessionContext.UserSession == null)
            {
                return;
            }

            var responseModel = actionContext.Response.Content as ObjectContent;

            if (responseModel == null)
            {
                return;
            }

            var model = responseModel.Value; //holding the returned value

            if (model == null)
            {
                return;
            }

            _activityType = Type.Retrieved;

            if (request.RequestType.ToUpper() == "GET")
            {
                var logModel = _auditLogFilterHelper.GetActivityLogEditModel(request, model, _activityType.ToString());

                var modelType = ReflectionHelper.GetModelType(model);
                if (logModel != null)
                {
                    _auditLogFilterHelper.AsyncLogModel(modelType, logModel, model, true);
                }
                else
                {
                    _logger.Info("Session is null while Saving Audit Data");
                }
            }
            //else if ((request.RequestType.ToUpper() == "POST" || request.RequestType.ToUpper() == "DELETE") &&
            //         (actionContext.ActionContext.ActionDescriptor.ActionName.ToLower() == "remove" ||
            //          actionContext.ActionContext.ActionDescriptor.ActionName.ToLower() == "delete"))
            //{

            //    if (!_activityLogDeleteService.IsDeleted(actionContext)) return;

            //    var logModel = new ActivityLogEditModel
            //    {
            //        Action = Type.Deleted.ToString(),
            //        UrlAccessed = request.FilePath ?? string.Empty,
            //        ModelFullName = _deletedEntity != null ? _deletedEntity.GetType().FullName : string.Empty,
            //        ModelName = _deletedEntity != null ? _deletedEntity.GetType().Name : string.Empty,
            //        RequestType = request.RequestType,
            //        Timestamp = DateTime.UtcNow,
            //        UserLogId =
            //            _sessionContext != null && _sessionContext.UserSession != null
            //                ? _sessionContext.UserSession.GetUserLog()
            //                : 0,
            //        AccessById = _sessionContext.UserSession.UserId
            //    };
            //    _activityLogService.LogDeleteActivity(logModel, _deletedEntity);
            //}
            else if (
                actionContext.ActionContext.ActionDescriptor.ReturnType.GetCustomAttributes(true)
                .Any(m => m.GetType() == typeof(MustAuditAttribute)))
            {
                var logModel = _auditLogFilterHelper.GetActivityLogEditModel(request, model, _activityType.ToString());

                var modelType = ReflectionHelper.GetModelType(model);

                if (logModel != null)
                {
                    _auditLogFilterHelper.AsyncLogModel(modelType, logModel, model, true);
                }
                else
                {
                    _logger.Info("Session is null while Saving Audit Data");
                }
            }
        }