private void CreateLogs(string actionCode, int[] ids, int?parentEntityId) { var backendLog = _serviceProvider.GetRequiredService <IBackendActionLogRepository>(); BackendActionContext.SetCurrent(actionCode, ids.Select(n => n.ToString()), parentEntityId); var logs = BackendActionLog.CreateLogs(BackendActionContext.Current, backendLog); backendLog.Save(logs); BackendActionContext.ResetCurrent(); }
private static void CreateLogs(string actionCode, int[] ids, int?parentEntityId) { var backendLog = DependencyResolver.Current.GetService <IBackendActionLogRepository>(); BackendActionContext.SetCurrent(actionCode, ids.Select(n => n.ToString()), parentEntityId); var logs = BackendActionLog.CreateLogs(BackendActionContext.Current, backendLog); backendLog.Save(logs); BackendActionContext.ResetCurrent(); }
public override async Task OnActionExecutionAsync(ActionExecutingContext filterContext, ActionExecutionDelegate next) { IServiceProvider serviceProvider = filterContext.HttpContext.RequestServices; ControllerContext controllerContext = ((Controller)filterContext.Controller).ControllerContext; IValueProvider valueProvider = await CompositeValueProvider.CreateAsync(controllerContext); var stringEntityIDs = new List <string>(); if (int.TryParse(valueProvider.GetValue(_entitySingleIdParamName).FirstValue, out int entityId)) { stringEntityIDs.Add(entityId.ToString()); } else { if (filterContext.ActionArguments.ContainsKey(_entityMultiIdParamName)) { if (filterContext.ActionArguments[_entityMultiIdParamName] is SelectedItemsViewModel selModel) { stringEntityIDs.AddRange(selModel.Ids.Select(id => id.ToString())); } } } int?parentEntityId = null; if (int.TryParse(valueProvider.GetValue(_parentEntityIdParamName).FirstValue, out int parentId)) { parentEntityId = parentId; } string actionCode = _actionCode; if (actionCode == null) { var getActionCode = serviceProvider.GetRequiredService <Func <string, IActionCode> >(); var command = (string)filterContext.RouteData.Values["command"]; actionCode = getActionCode(command).ActionCode; } BackendActionContext.SetCurrent(actionCode, stringEntityIDs, parentEntityId); try { await next.Invoke(); } finally { BackendActionContext.ResetCurrent(); } }
public HttpContextBase PostAction(XmlDbUpdateRecordedAction recordedAction, string backendUrl, int userId, bool useGuidSubstitution) { Ensure.NotNull(QPConnectionScope.Current.DbConnection, "QPConnection scope should be initialized to use fake mvc context"); var urlParts = recordedAction.BackendAction.ControllerActionUrl.Split(@"/".ToCharArray()).Where(n => !string.IsNullOrEmpty(n) && n != "~").ToArray(); var controllerName = urlParts[0]; var controllerAction = urlParts[1]; var requestContext = new RequestContext( XmlDbUpdateHttpContextHelpers.BuildHttpContextBase(recordedAction, backendUrl, userId, useGuidSubstitution), XmlDbUpdateHttpContextHelpers.GetRouteData(recordedAction, controllerName, controllerAction) ); BackendActionContext.ResetCurrent(); XmlDbUpdateHttpContextHelpers.BuildController(requestContext, controllerName, CultureHelpers.GetCultureByLcid(recordedAction.Lcid)).Execute(requestContext); return(requestContext.HttpContext); }
public HttpContext PostAction(XmlDbUpdateRecordedAction recordedAction, string backendUrl, int userId, bool useGuidSubstitution, IServiceProvider provider = null) { Ensure.NotNull(QPConnectionScope.Current.DbConnection, "QPConnection scope should be initialized to use fake mvc context"); var urlParts = recordedAction.BackendAction.ControllerActionUrl.Split(@"/".ToCharArray()) .Where(n => !string.IsNullOrEmpty(n) && n != "~").ToArray(); var controller = urlParts[0]; var action = urlParts[1]; BackendActionContext.ResetCurrent(); var cultureInfo = CultureHelpers.GetCultureByLcid(recordedAction.Lcid); CultureInfo.CurrentCulture = cultureInfo; var httpContext = XmlDbUpdateHttpContextHelpers.BuildHttpContext(recordedAction, backendUrl, userId, useGuidSubstitution); var routeData = XmlDbUpdateHttpContextHelpers.GetRouteData(recordedAction, controller, action); httpContext.Features[typeof(IRoutingFeature)] = new RoutingFeature { RouteData = routeData }; var routeContext = new RouteContext(httpContext) { RouteData = routeData }; var serviceProvider = provider ?? new HttpContextAccessor().HttpContext.RequestServices; httpContext.RequestServices = serviceProvider; var m = typeof(DynamicRouteValueTransformer).Assembly.GetType("Microsoft.AspNetCore.Mvc.Routing.MvcRouteHandler"); var handler = (IRouter)serviceProvider.GetRequiredService(m); QPContext.CurrentUserId = userId; Task.Run(async() => { await handler.RouteAsync(routeContext); await routeContext.Handler(routeContext.HttpContext); }).Wait(); QPContext.CurrentUserId = 0; return(httpContext); }
public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); var stringEntityIDs = new List <string>(); var vpr = filterContext.Controller.ValueProvider.GetValue(_entitySingleIdParamName); if (vpr != null) { stringEntityIDs.Add(vpr.AttemptedValue); } else { if (filterContext.ActionParameters.ContainsKey(_entityMultiIdParamName)) { var ids = filterContext.ActionParameters[_entityMultiIdParamName] as IEnumerable <int>; var strIds = filterContext.ActionParameters[_entityMultiIdParamName] as IEnumerable <string>; if (ids != null) { stringEntityIDs.AddRange(ids.Select(id => id.ToString())); } else if (strIds != null) { stringEntityIDs.AddRange(strIds); } } } int?parentEntityId = null; vpr = filterContext.Controller.ValueProvider.GetValue(_parentEntityIdParamName); if (!string.IsNullOrEmpty(vpr?.AttemptedValue)) { if (int.TryParse(vpr.AttemptedValue, out var peid)) { parentEntityId = peid; } } BackendActionContext.SetCurrent(_actionCode, stringEntityIDs, parentEntityId); }
public ActionResult UploadChunk(int?chunk, int?chunks, string name, string destinationUrl) { if (name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) { var errorMsg = $"File to upload: \"{name}\" has invalid characters"; Logger.Log.Warn(errorMsg); return(Json(new { message = errorMsg, isError = true })); } destinationUrl = HttpUtility.UrlDecode(destinationUrl); if (string.IsNullOrEmpty(destinationUrl)) { throw new ArgumentException("Folder Path is empty"); } if (!Directory.Exists(destinationUrl)) { Directory.CreateDirectory(destinationUrl); } chunk = chunk ?? 0; chunks = chunks ?? 1; PathSecurityResult securityResult; var fileUpload = Request.Files[0]; var tempPath = Path.Combine(QPConfiguration.TempDirectory, name); var destPath = Path.Combine(destinationUrl, name); if (chunk == 0 && chunks == 1) { securityResult = PathInfo.CheckSecurity(destinationUrl); if (!securityResult.Result) { var errorMsg = string.Format(PlUploadStrings.ServerError, name, destinationUrl, $"Access to the folder (ID = {securityResult.FolderId}) denied"); Logger.Log.Warn(errorMsg); return(Json(new { message = errorMsg, isError = true })); } try { using (var fs = new FileStream(destPath, FileMode.Create)) { var buffer = new byte[fileUpload.InputStream.Length]; fileUpload.InputStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, buffer.Length); } } catch (Exception ex) { var errorMsg = string.Format(PlUploadStrings.ServerError, name, destinationUrl, ex.Message); Logger.Log.Error(errorMsg, ex); return(Json(new { message = errorMsg, isError = true })); } } else { try { using (var fs = new FileStream(tempPath, chunk == 0 ? FileMode.Create : FileMode.Append)) { var buffer = new byte[fileUpload.InputStream.Length]; fileUpload.InputStream.Read(buffer, 0, buffer.Length); fs.Write(buffer, 0, buffer.Length); } } catch (Exception ex) { var errorMsg = string.Format(PlUploadStrings.ServerError, name, tempPath, ex.Message); Logger.Log.Error(errorMsg, ex); return(Json(new { message = errorMsg, isError = true })); } try { var isTheLastChunk = chunk.Value == chunks.Value - 1; if (isTheLastChunk) { securityResult = PathInfo.CheckSecurity(destinationUrl); var actionCode = securityResult.IsSite ? ActionCode.UploadSiteFile : ActionCode.UploadContentFile; if (!securityResult.Result) { var errorMsg = string.Format(PlUploadStrings.ServerError, name, destinationUrl, $"Access to the folder (ID = {securityResult.FolderId}) denied"); Logger.Log.Warn(errorMsg); return(Json(new { message = errorMsg, isError = true })); } if (FileIO.Exists(destPath)) { FileIO.SetAttributes(destPath, FileAttributes.Normal); FileIO.Delete(destPath); } FileIO.Move(tempPath, destPath); BackendActionContext.SetCurrent(actionCode, new[] { name }, securityResult.FolderId); var logs = BackendActionLog.CreateLogs(BackendActionContext.Current, _logger); _logger.Save(logs); BackendActionContext.ResetCurrent(); } } catch (Exception ex) { var errorMsg = string.Format(PlUploadStrings.ServerError, name, destinationUrl, ex.Message); Logger.Log.Error(errorMsg, ex); return(Json(new { message = errorMsg, isError = true })); } return(Json(new { message = $"chunk#{chunk.Value}, of file{name} uploaded", isError = false })); } return(Json(new { message = $"file{name} uploaded", isError = false })); }
public override void OnActionExecuted(ActionExecutedContext filterContext) { BackendActionContext.ResetCurrent(); base.OnActionExecuted(filterContext); }