Exemplo n.º 1
0
        public ActionResult Download(int id)
        {
            var part = _orchardServices.ContentManager.Get(id).As <PipelineConfigurationPart>();

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

            if (part == null)
            {
                process.Name = "Not Found";
                return(new FileStreamResult(GenerateStreamFromString(process.Serialize()), "text/xml")
                {
                    FileDownloadName = id + ".xml"
                });
            }

            if (!_orchardServices.Authorizer.Authorize(Permissions.ViewContent, part))
            {
                process.Name = "Not Authorized";
                return(new FileStreamResult(GenerateStreamFromString(process.Serialize()), "text/xml")
                {
                    FileDownloadName = id + ".xml"
                });
            }

            return(new FileStreamResult(GenerateStreamFromString(part.Configuration), "text/" + part.EditorMode)
            {
                FileDownloadName = _slugService.Slugify(part.Title()) + "." + part.EditorMode
            });
        }
Exemplo n.º 2
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.EditorMode, part.EditorMode);

                    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 (output.Provider.In("internal", "file"))
                    {
                        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);

                                        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($"Processed {count} records in batch {batchParameters["BatchId"]}."));
                                                    }
                                                    else
                                                    {
                                                        _orchardServices.Notifier.Information(T($"Processed {count} records."));
                                                    }
                                                }
                                                else
                                                {
                                                    return(_batchRedirectService.Redirect(action.Url, batchParameters));
                                                }
                                            }
                                        }
                                    }
                                    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, 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)));
        }