Mike Phares 83789cdd91 Added ControllerExtensions to be used instead of HtmlViewRenderer for net8
Added HttpException class for missing HttpException for net8

Wrapped HttpContext.Session, GetJsonResult, IsAjaxRequest and GetUserIdentityName in controllers for net8

Added AuthenticationService to test Fab2ApprovalMKLink code for net8

Compile conditionally flags to debug in dotnet core
2025-05-19 13:29:54 -07:00

244 lines
8.1 KiB
C#

using System;
using System.Linq;
#if !NET8
using System.Web;
using System.Web.Mvc;
#endif
#if NET8
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
#endif
using Fab2ApprovalSystem.DMO;
using Fab2ApprovalSystem.Misc;
using Fab2ApprovalSystem.Models;
#if !NET8
using System.Collections.Generic;
using Fab2ApprovalSystem.ViewModels;
#endif
namespace Fab2ApprovalSystem.Controllers;
[Authorize]
#if !NET8
[SessionExpireFilter]
#endif
#if NET8
[Route("[controller]")]
#endif
public class ReportsController : Controller {
public const string specialNullString = "~NULL~";
private readonly AppSettings? _AppSettings = GlobalVars.AppSettings;
// GET: Export
public ActionResult Index() {
UserAccountDMO userDMO = new();
ViewBag.HasITARAccess = userDMO.GetITARAccess(GlobalVars.GetUserId(GetSession()));
return View();
}
#if !NET8
public ActionResult Report(string id, string docType = "") {
if (string.IsNullOrEmpty(id))
return RedirectToAction("Index");
UserAccountDMO userDMO = new();
if (!userDMO.GetITARAccess(GlobalVars.GetUserId(GetSession())))
return View("UnAuthorizedAccess");
ReportViewModel<SelectListItem> m = new();
var reports = GetReportList(docType);
foreach (var report in reports) {
if (string.Equals(report.ReportID, id, StringComparison.OrdinalIgnoreCase)) {
m.ReportID = report.ReportID;
m.ReportName = report.Name;
m.Description = report.Description;
m.DocType = docType;
var c = SetupSSRSHelperClient();
m.Parameters = c.GetReportParameters(report.FullPath).Select(parm => {
ReportParameterViewModel<SelectListItem> r = new();
r.Visible = (parm.PromptUser.HasValue == false || parm.PromptUser == true) && !string.IsNullOrEmpty(parm.Prompt);
r.Prompt = parm.Prompt;
r.Name = parm.Name;
r.HtmlID = "parm_" + parm.Name;
if (parm.MultiValue.HasValue && parm.MultiValue.Value)
r.ControlType = ParameterControlTypes.Multiselect;
else if ((parm.ValidValues != null) && (parm.ValidValues.Length > 0))
r.ControlType = ParameterControlTypes.Dropdown;
else if (parm.DataType.Equals("DateTime", StringComparison.OrdinalIgnoreCase))
r.ControlType = ParameterControlTypes.DatePicker;
else
r.ControlType = ParameterControlTypes.Textbox;
r.SelectList = null;
if (parm.ValidValues != null) {
r.SelectList = parm.ValidValues.Select(vv => {
return new SelectListItem() {
Text = vv.Value,
Value = (vv.Key == null ? specialNullString : vv.Key),
Selected = (parm.DefaultValues != null && parm.DefaultValues.Contains(vv.Key))
};
}).ToList();
}
r.DefaultValue = "";
if (parm.DefaultValues != null && parm.DefaultValues.Length > 0)
r.DefaultValue = parm.DefaultValues[0];
return r;
}).ToArray();
}
}
return View(m);
}
public SSRSHelper.SSRSClient SetupSSRSHelperClient() {
var useCfgForBindings = false;
if (string.Equals(_AppSettings.SSRSBindingsByConfiguration, "true", StringComparison.OrdinalIgnoreCase))
useCfgForBindings = true;
var c = new SSRSHelper.SSRSClient(
Convert.ToString(_AppSettings.SSRSBaseURL),
Convert.ToString(_AppSettings.SSRSDomain),
Convert.ToString(_AppSettings.SSRSUsername),
Convert.ToString(_AppSettings.SSRSPassword),
useCfgForBindings);
c.Initialize();
return c;
}
private IEnumerable<SSRSHelper.ReportInfo> GetReportList(String docType) {
String folderName = Convert.ToString(_AppSettings.SSRSFolder);
if (folderName.EndsWith("/"))
folderName = folderName.TrimEnd('/');
if (!String.IsNullOrWhiteSpace(docType))
folderName = folderName + "/" + docType;
var c = SetupSSRSHelperClient();
return c.ListReports(folderName);
}
public ActionResult GetReports(string docType) {
var reports = GetReportList(docType);
return GetJsonResult(new {
Data =
reports.Select(r => new ReportViewModel<SelectListItem>() {
ReportName = r.Name ?? "",
Description = r.Description ?? "",
ReportID = r.ReportID ?? "",
DocType = docType
})
});
}
[HttpPost]
public ActionResult ExportReport(string DocType, string ReportID) {
var c = SetupSSRSHelperClient();
var reports = GetReportList(DocType);
var report = reports.Where(r => string.Equals(r.ReportID, ReportID, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (report == null)
return Content("Invalid report ID");
var reportParms = c.GetReportParameters(report.FullPath);
var parms = new SSRSHelper.ReportParameterCollection();
parms.Add("DocType", DocType);
parms.Add("UserID", GlobalVars.GetUserIdValue(GetSession()));
parms.Add("BaseURL", GlobalVars.hostURL);
foreach (var rp in reportParms) {
if (rp.MultiValue.HasValue && rp.MultiValue.Value) {
foreach (string v in Request.Params.GetValues("parm_" + rp.Name)) {
parms.Add(rp.Name, v);
}
} else {
string value = null;
if (Request.Params.AllKeys.Contains("parm_" + rp.Name))
value = Request.Params.GetValues("parm_" + rp.Name).FirstOrDefault();
if (value == specialNullString)
value = null;
if ((rp.AllowBlank.HasValue == false || rp.AllowBlank.Value == false) && value == "")
value = null;
if (value == null) {
if (rp.Nullable.HasValue == false || rp.Nullable.Value == false) {
if (rp.DefaultValues != null && rp.DefaultValues.Length > 0)
value = rp.DefaultValues[0];
if (value == null)
continue;
}
}
parms.Add(rp.Name, value);
}
}
var b = c.ExportReport(report.FullPath, GetUserIdentityName(), parms, "EXCELOPENXML");
try {
var b2 = c.FreezeExcelHeaders(b);
return File(b2, "application/octet-stream", MakeFilename(report.Name) + ".xlsx");
} catch {
}
return File(b, "application/octet-stream", MakeFilename(report.Name) + ".xlsx");
}
#endif
protected string MakeFilename(string reportName) {
string r = "";
char[] invalidChars = System.IO.Path.GetInvalidFileNameChars();
foreach (char c in reportName) {
if (invalidChars.Contains(c))
r += '_';
else
r += c;
}
return r;
}
#if !NET8
private System.Web.HttpSessionStateBase GetSession() =>
Session;
private JsonResult GetJsonResult(object? data) =>
Json(data, JsonRequestBehavior.AllowGet);
private bool IsAjaxRequest() =>
Request.IsAjaxRequest();
#endif
#if NET8
private Microsoft.AspNetCore.Http.ISession GetSession() =>
HttpContext.Session;
private JsonResult GetJsonResult(object? data) =>
Json(data);
private bool IsAjaxRequest() =>
Request.Headers.TryGetValue("X-Requested-With", out Microsoft.Extensions.Primitives.StringValues strings) && strings[0] == "XMLHttpRequest";
#endif
private string GetUserIdentityName() =>
@User.Identity.Name;
}