Ejemplo n.º 1
0
        public ActionResult Report(int id)
        {
            var timer = new Stopwatch();

            timer.Start();

            var process = new Process {
                Name = "Report"
            };

            var part = _orchardServices.ContentManager.Get(id).As <PipelineConfigurationPart>();

            if (part == null)
            {
                process.Name = "Not Found";
            }
            else
            {
                var user = _orchardServices.WorkContext.CurrentUser == null ? "Anonymous" : _orchardServices.WorkContext.CurrentUser.UserName ?? "Anonymous";

                if (_orchardServices.Authorizer.Authorize(Permissions.ViewContent, part))
                {
                    process = _processService.Resolve(part);

                    var parameters = Common.GetParameters(Request, _secureFileService, _orchardServices);
                    if (part.NeedsInputFile && Convert.ToInt32(parameters[Common.InputFileIdName]) == 0)
                    {
                        _orchardServices.Notifier.Add(NotifyType.Error, T("This transformalize expects a file."));
                        process.Name = "File Not Found";
                    }

                    process.Load(part.Configuration, parameters);
                    process.Buffer   = false; // no buffering for reports
                    process.ReadOnly = true;  // force reporting to omit system fields

                    // secure actions
                    var actions = process.Actions.Where(a => !a.Before && !a.After && !a.Description.StartsWith("Batch", StringComparison.OrdinalIgnoreCase));
                    foreach (var action in actions)
                    {
                        var p = _orchardServices.ContentManager.Get(action.Id);
                        if (!_orchardServices.Authorizer.Authorize(Permissions.ViewContent, p))
                        {
                            action.Description = "BatchUnauthorized";
                        }
                    }

                    var output = process.Output();

                    if (_reportOutputs.Contains(output.Provider))
                    {
                        Common.TranslatePageParametersToEntities(process, parameters, "page");

                        // change process for export and batch purposes
                        var reportType = Request["output"] ?? "page";
                        if (!_renderedOutputs.Contains(reportType))
                        {
                            if (reportType == "batch" && Request.HttpMethod.Equals("POST") && parameters.ContainsKey("action"))
                            {
                                var action = process.Actions.FirstOrDefault(a => a.Description == parameters["action"]);

                                if (action != null)
                                {
                                    // check security
                                    var actionPart = _orchardServices.ContentManager.Get(action.Id);
                                    if (actionPart != null && _orchardServices.Authorizer.Authorize(Permissions.ViewContent, actionPart))
                                    {
                                        // security okay
                                        parameters["entity"] = process.Entities.First().Alias;
                                        var batchParameters = _batchCreateService.Create(process, parameters);

                                        Common.AddOrchardVariables(batchParameters, _orchardServices, Request);

                                        batchParameters["count"] = parameters.ContainsKey("count") ? parameters["count"] : "0";
                                        var count = _batchWriteService.Write(Request, process, batchParameters);

                                        if (count > 0)
                                        {
                                            if (_batchRunService.Run(action, batchParameters))
                                            {
                                                if (action.Url == string.Empty)
                                                {
                                                    if (batchParameters.ContainsKey("BatchId"))
                                                    {
                                                        _orchardServices.Notifier.Information(T(string.Format("Processed {0} records in batch {1}.", count, batchParameters["BatchId"])));
                                                    }
                                                    else
                                                    {
                                                        _orchardServices.Notifier.Information(T(string.Format("Processed {0} records.", count)));
                                                    }
                                                    var referrer = HttpContext.Request.UrlReferrer == null?Url.Action("Report", new { Id = id }) : HttpContext.Request.UrlReferrer.ToString();

                                                    return(_batchRedirectService.Redirect(referrer, batchParameters));
                                                }
                                                return(_batchRedirectService.Redirect(action.Url, batchParameters));
                                            }

                                            var message = batchParameters.ContainsKey("BatchId") ? string.Format("Batch {0} failed.", batchParameters["BatchId"]) : "Batch failed.";
                                            Logger.Error(message);
                                            _orchardServices.Notifier.Error(T(message));
                                            foreach (var key in batchParameters.Keys)
                                            {
                                                Logger.Error("Batch Parameter {0} = {1}.", key, batchParameters[key]);
                                            }
                                            return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, message));
                                        }
                                    }
                                    else
                                    {
                                        return(new HttpUnauthorizedResult("You do not have access to this bulk action."));
                                    }
                                }
                            }
                            else     // export
                            {
                                ConvertToExport(user, process, part, reportType, parameters);
                                process.Load(process.Serialize(), parameters);
                            }
                        }

                        if (Request["sort"] != null)
                        {
                            _sortService.AddSortToEntity(process.Entities.First(), Request["sort"]);
                        }

                        if (process.Errors().Any())
                        {
                            foreach (var error in process.Errors())
                            {
                                _orchardServices.Notifier.Add(NotifyType.Error, T(error));
                            }
                        }
                        else
                        {
                            if (process.Entities.Any(e => !e.Fields.Any(f => f.Input)))
                            {
                                _orchardServices.WorkContext.Resolve <ISchemaHelper>().Help(process);
                            }

                            if (!process.Errors().Any())
                            {
                                var runner = _orchardServices.WorkContext.Resolve <IRunTimeExecute>();
                                try {
                                    runner.Execute(process);
                                    process.Request = "Run";
                                    process.Time    = timer.ElapsedMilliseconds;

                                    if (process.Errors().Any())
                                    {
                                        foreach (var error in process.Errors())
                                        {
                                            _orchardServices.Notifier.Add(NotifyType.Error, T(error));
                                        }
                                        process.Status  = 500;
                                        process.Message = "There are errors in the pipeline.  See log.";
                                    }
                                    else
                                    {
                                        process.Status  = 200;
                                        process.Message = "Ok";
                                    }

                                    var o = process.Output();
                                    switch (o.Provider)
                                    {
                                    case "kml":
                                    case "geojson":
                                    case "file":
                                        Response.AddHeader("content-disposition", "attachment; filename=" + o.File);
                                        switch (o.Provider)
                                        {
                                        case "kml":
                                            Response.ContentType = "application/vnd.google-earth.kml+xml";
                                            break;

                                        case "geojson":
                                            Response.ContentType = "application/vnd.geo+json";
                                            break;

                                        default:
                                            Response.ContentType = "application/csv";
                                            break;
                                        }
                                        Response.Flush();
                                        Response.End();
                                        return(new EmptyResult());

                                    case "excel":
                                        return(new FilePathResult(o.File, Common.ExcelContentType)
                                        {
                                            FileDownloadName = _slugService.Slugify(part.Title()) + ".xlsx"
                                        });

                                    default:      // page and map are rendered to page
                                        break;
                                    }
                                } catch (Exception ex) {
                                    Logger.Error(ex, ex.Message);
                                    _orchardServices.Notifier.Error(T(ex.Message));
                                }
                            }
                        }
                    }
                }
                else
                {
                    _orchardServices.Notifier.Warning(user == "Anonymous" ? T("Sorry. Anonymous users do not have permission to view this report. You may need to login.") : T("Sorry {0}. You do not have permission to view this report.", user));
                }
            }

            return(View(new ReportViewModel(process, part)));
        }
Ejemplo n.º 2
0
        public ActionResult Index(int id)
        {
            var timer = new Stopwatch();

            timer.Start();

            var process = new Process {
                Name = "Report"
            };

            var part = _orchardServices.ContentManager.Get(id).As <PipelineConfigurationPart>();

            if (part == null)
            {
                process.Name = "Not Found";
            }
            else
            {
                var user = _orchardServices.WorkContext.CurrentUser == null ? "Anonymous" : _orchardServices.WorkContext.CurrentUser.UserName ?? "Anonymous";

                if (_orchardServices.Authorizer.Authorize(Permissions.ViewContent, part))
                {
                    process = _processService.Resolve(part);

                    var parameters = Common.GetParameters(Request, _orchardServices, _secureFileService);
                    if (part.NeedsInputFile && Convert.ToInt32(parameters[Common.InputFileIdName]) == 0)
                    {
                        _orchardServices.Notifier.Add(NotifyType.Error, T("This transformalize expects a file."));
                        process.Name = "File Not Found";
                    }

                    process.Load(part.Configuration, parameters);
                    process.Mode     = "report";
                    process.Buffer   = false; // no buffering for reports
                    process.ReadOnly = true;  // force reporting to omit system fields

                    // secure actions
                    var actions = process.Actions.Where(a => !a.Before && !a.After && !a.Description.StartsWith("Batch", StringComparison.OrdinalIgnoreCase));
                    foreach (var action in actions)
                    {
                        var p = _orchardServices.ContentManager.Get(action.Id);
                        if (!_orchardServices.Authorizer.Authorize(Permissions.ViewContent, p))
                        {
                            action.Description = "BatchUnauthorized";
                        }
                    }

                    Common.TranslatePageParametersToEntities(process, parameters, "page");

                    if (Request.HttpMethod.Equals("POST") && parameters.ContainsKey("action"))
                    {
                        var action = process.Actions.FirstOrDefault(a => a.Description == parameters["action"]);

                        if (action != null)
                        {
                            // check security
                            var actionPart = _orchardServices.ContentManager.Get(action.Id);
                            if (actionPart == null)
                            {
                                return(new HttpNotFoundResult(string.Format("The action id {0} does not refer to a content item id.", action.Id)));
                            }

                            if (_orchardServices.Authorizer.Authorize(Permissions.ViewContent, actionPart))
                            {
                                // security okay
                                parameters["entity"] = process.Entities.First().Alias;
                                var batchParameters = _batchCreateService.Create(process, parameters);

                                Common.AddOrchardVariables(batchParameters, _orchardServices, Request);

                                batchParameters["count"] = parameters.ContainsKey("count") ? parameters["count"] : "0";
                                var count = _batchWriteService.Write(Request, process, batchParameters);

                                if (count > 0)
                                {
                                    if (_batchRunService.Run(action, batchParameters))
                                    {
                                        if (action.Url == string.Empty)
                                        {
                                            if (batchParameters.ContainsKey("BatchId"))
                                            {
                                                _orchardServices.Notifier.Information(T(string.Format("Processed {0} records in batch {1}.", count, batchParameters["BatchId"])));
                                            }
                                            else
                                            {
                                                _orchardServices.Notifier.Information(T(string.Format("Processed {0} records.", count)));
                                            }
                                            var referrer = HttpContext.Request.UrlReferrer == null?Url.Action("Index", new { Id = id }) : HttpContext.Request.UrlReferrer.ToString();

                                            return(_batchRedirectService.Redirect(referrer, batchParameters));
                                        }
                                        return(_batchRedirectService.Redirect(action.Url, batchParameters));
                                    }

                                    var message = batchParameters.ContainsKey("BatchId") ? string.Format("Batch {0} failed.", batchParameters["BatchId"]) : "Batch failed.";
                                    Logger.Error(message);
                                    _orchardServices.Notifier.Error(T(message));
                                    foreach (var key in batchParameters.Keys)
                                    {
                                        Logger.Error("Batch Parameter {0} = {1}.", key, batchParameters[key]);
                                    }
                                    return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, message));
                                }
                            }
                            else
                            {
                                return(new HttpUnauthorizedResult("You do not have access to this bulk action."));
                            }
                        }
                    }


                    if (Request["sort"] != null)
                    {
                        _sortService.AddSortToEntity(process.Entities.First(), Request["sort"]);
                    }

                    if (process.Errors().Any())
                    {
                        foreach (var error in process.Errors())
                        {
                            _orchardServices.Notifier.Add(NotifyType.Error, T(error));
                        }
                    }
                    else
                    {
                        if (process.Entities.Any(e => !e.Fields.Any(f => f.Input)))
                        {
                            _orchardServices.WorkContext.Resolve <ISchemaHelper>().Help(process);
                        }

                        if (!process.Errors().Any())
                        {
                            var runner = _orchardServices.WorkContext.Resolve <IRunTimeExecute>();
                            try {
                                runner.Execute(process);
                                process.Request = "Run";
                                process.Time    = timer.ElapsedMilliseconds;

                                if (process.Errors().Any())
                                {
                                    foreach (var error in process.Errors())
                                    {
                                        _orchardServices.Notifier.Add(NotifyType.Error, T(error));
                                    }
                                    process.Status  = 500;
                                    process.Message = "There are errors in the pipeline.  See log.";
                                }
                                else
                                {
                                    process.Status  = 200;
                                    process.Message = "Ok";
                                }
                            } catch (Exception ex) {
                                Logger.Error(ex, ex.Message);
                                _orchardServices.Notifier.Error(T(ex.Message));
                            }
                        }
                    }
                }
                else
                {
                    _orchardServices.Notifier.Warning(user == "Anonymous" ? T("Sorry. Anonymous users do not have permission to view this report. You may need to login.") : T("Sorry {0}. You do not have permission to view this report.", user));
                }
            }

            return(View(new ReportViewModel(process, part)));
        }
Ejemplo n.º 3
0
        public ActionResult Index(int id)
        {
            var timer = new Stopwatch();

            timer.Start();

            var referrer = HttpContext.Request.UrlReferrer == null?Url.Action("Index", "Report", new { Id = id }) : HttpContext.Request.UrlReferrer.ToString();

            var process = new Process {
                Name = "Action"
            };

            var part = _orchardServices.ContentManager.Get(id).As <PipelineConfigurationPart>();

            if (part == null)
            {
                process.Name = "Not Found";
                _orchardServices.Notifier.Add(NotifyType.Error, T("Could not find pipeline id {0}", id));
                return(_batchRedirectService.Redirect(referrer, null));
            }

            var user = _orchardServices.WorkContext.CurrentUser == null ? "Anonymous" : _orchardServices.WorkContext.CurrentUser.UserName ?? "Anonymous";

            if (_orchardServices.Authorizer.Authorize(Permissions.ViewContent, part))
            {
                process = _processService.Resolve(part);

                var parameters = Common.GetParameters(Request, _orchardServices, _secureFileService);
                if (part.NeedsInputFile && Convert.ToInt32(parameters[Common.InputFileIdName]) == 0)
                {
                    _orchardServices.Notifier.Add(NotifyType.Error, T("This transformalize expects a file."));
                    process.Name = "File Not Found";
                }

                process.Load(part.Configuration, parameters);
                process.ReadOnly = true; // force actions to omit system fields

                if (parameters.ContainsKey("action"))
                {
                    var action = process.Actions.FirstOrDefault(a => a.Description == parameters["action"]);
                    var system = process.Parameters.FirstOrDefault(p => p.Name.Equals("System", StringComparison.OrdinalIgnoreCase));

                    if (system == null)
                    {
                        Logger.Error("The {0} requires a System parameter in order to run bulk actions.", part.Title());
                        _orchardServices.Notifier.Error(T("The {0} requires a System parameter in order to run bulk actions.", part.Title()));
                    }
                    else
                    {
                        if (system.Value == "*")
                        {
                            _orchardServices.Notifier.Warning(T("The {0} must be selected in order to run an action.", system.Label));
                            system = null;
                        }
                    }

                    if (action != null && system != null)
                    {
                        // check security
                        var actionPart = _orchardServices.ContentManager.Get(action.Id);
                        if (actionPart == null)
                        {
                            _orchardServices.Notifier.Add(NotifyType.Error, T("Could not find orchard content id {0} referenced in the {1}:{2} action.", id, action.Type, action.Description));
                            return(_batchRedirectService.Redirect(referrer, null));
                        }

                        if (_orchardServices.Authorizer.Authorize(Permissions.ViewContent, actionPart))
                        {
                            parameters["entity"] = process.Entities.First().Alias;

                            var batchParameters = _batchCreateService.Create(process, parameters);
                            batchParameters["Orchard.User"] = parameters["Orchard.User"];
                            batchParameters["count"]        = parameters.ContainsKey("count") ? parameters["count"] : "0";

                            var count = _batchWriteService.Write(Request, process, batchParameters);

                            if (count > 0)
                            {
                                // get default parameters from process
                                foreach (var p in process.Parameters.Where(pr => pr.Value != "*"))
                                {
                                    if (!batchParameters.ContainsKey(p.Name))
                                    {
                                        batchParameters[p.Name] = p.Value;
                                    }
                                }

                                // get parameters from action
                                foreach (var p in action.Parameters.Where(pr => pr.Value != "*"))
                                {
                                    if (batchParameters.ContainsKey(p.Name))
                                    {
                                        _orchardServices.Notifier.Add(NotifyType.Warning, T("The action parameter {0} is hiding another parameter", p.Name));
                                    }
                                    batchParameters[p.Name] = p.Value;
                                }

                                if (_batchRunService.Run(action, batchParameters))
                                {
                                    if (action.Url == string.Empty)
                                    {
                                        if (batchParameters.ContainsKey("BatchId"))
                                        {
                                            _orchardServices.Notifier.Information(T(string.Format("Processed {0} records in batch {1}.", count, batchParameters["BatchId"])));
                                        }
                                        else
                                        {
                                            _orchardServices.Notifier.Information(T(string.Format("Processed {0} records.", count)));
                                        }
                                        return(_batchRedirectService.Redirect(referrer, null));
                                    }
                                    return(_batchRedirectService.Redirect(action.Url, batchParameters));
                                }

                                var message = batchParameters.ContainsKey("BatchId") ? string.Format("Batch {0} failed.", batchParameters["BatchId"]) : "Batch failed.";
                                Logger.Error(message);
                                _orchardServices.Notifier.Error(T(message));
                                foreach (var key in batchParameters.Keys)
                                {
                                    Logger.Error("Batch Parameter {0} = {1}.", key, batchParameters[key]);
                                }
                                return(_batchRedirectService.Redirect(referrer, null));
                            }
                        }
                        else
                        {
                            return(new HttpUnauthorizedResult("You do not have access to this bulk action."));
                        }
                    }
                }
                else
                {
                    _orchardServices.Notifier.Warning(T("An action must be specified."));
                }
            }
            else
            {
                _orchardServices.Notifier.Warning(user == "Anonymous" ? T("Sorry. Anonymous users do not have permission to view this report. You may need to login.") : T("Sorry {0}. You do not have permission to view this report.", user));
            }

            return(_batchRedirectService.Redirect(referrer, null));
        }