-
Notifications
You must be signed in to change notification settings - Fork 2
/
CustomDashboardStorage.cs
68 lines (51 loc) · 2.78 KB
/
CustomDashboardStorage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using DevExpress.DashboardWeb;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
public class CustomDashboardStorage : IEditableDashboardStorage {
private readonly IHttpContextAccessor contextAccessor;
private string dashboardStorageFolder;
public CustomDashboardStorage(IWebHostEnvironment hostingEnvironment, IHttpContextAccessor contextAccessor) {
this.contextAccessor = contextAccessor;
this.dashboardStorageFolder = hostingEnvironment.ContentRootFileProvider.GetFileInfo("App_Data/Dashboards").PhysicalPath;
}
public IEnumerable<DashboardInfo> GetAvailableDashboardsInfo() {
var dashboardInfos = new List<DashboardInfo>();
var files = Directory.GetFiles(dashboardStorageFolder, "*.xml");
foreach (var item in files) {
var name = Path.GetFileNameWithoutExtension(item);
var userName = contextAccessor.HttpContext.Session.GetString("CurrentUser");
if (!string.IsNullOrEmpty(userName) && name.EndsWith(userName, System.StringComparison.InvariantCultureIgnoreCase))
dashboardInfos.Add(new DashboardInfo() { ID = name, Name = name });
}
return dashboardInfos;
}
public XDocument LoadDashboard(string dashboardID) {
if (GetAvailableDashboardsInfo().Any(di => di.Name == dashboardID)) {
var path = Path.Combine(dashboardStorageFolder, dashboardID + ".xml");
var content = File.ReadAllText(path);
return XDocument.Parse(content);
}
else {
throw new System.ApplicationException("You are not authorized to view this dashboard.");
}
}
public string AddDashboard(XDocument dashboard, string dashboardName) {
var userName = contextAccessor.HttpContext.Session.GetString("CurrentUser");
if (string.IsNullOrEmpty(userName) || userName != "Admin")
throw new System.ApplicationException("You are not authorized to add dashboards.");
var path = Path.Combine(dashboardStorageFolder, dashboardName + "_" + userName + ".xml");
File.WriteAllText(path, dashboard.ToString());
return Path.GetFileNameWithoutExtension(path);
}
public void SaveDashboard(string dashboardID, XDocument dashboard) {
var userName = contextAccessor.HttpContext.Session.GetString("CurrentUser");
if (string.IsNullOrEmpty(userName) || userName != "Admin")
throw new System.ApplicationException("You are not authorized to save dashboards.");
var path = Path.Combine(dashboardStorageFolder, dashboardID + ".xml");
File.WriteAllText(path, dashboard.ToString());
}
}