Repository navigation
Expand file tree
/
Copy pathSystemController.cs
More file actions
308 lines (272 loc) · 13.5 KB
/
Copy pathSystemController.cs
File metadata and controls
308 lines (272 loc) · 13.5 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using Grand.Business.Core.Enums.Checkout;
using Grand.Business.Core.Interfaces.Catalog.Directory;
using Grand.Business.Core.Interfaces.Checkout.Payments;
using Grand.Business.Core.Interfaces.Checkout.Shipping;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Domain.Permissions;
using Grand.Domain.Directory;
using Grand.Infrastructure;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Roslyn;
using Grand.Web.Admin.Extensions;
using Grand.Web.AdminShared.Models.Common;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Security.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Runtime.InteropServices;
using Grand.Web.Common.Localization;
namespace Grand.Web.Admin.Controllers;
[PermissionAuthorize(PermissionSystemName.System)]
public class SystemController : BaseAdminController
{
#region Constructors
public SystemController(IPaymentService paymentService,
IShippingService shippingService,
ICurrencyService currencyService,
IMeasureService measureService,
IDateTimeService dateTimeService,
IContextAccessor contextAccessor,
IRequestTranslationService translationService,
IHostApplicationLifetime applicationLifetime,
IWebHostEnvironment webHostEnvironment,
ILogger<SystemController> logger,
CurrencySettings currencySettings,
MeasureSettings measureSettings,
ExtensionsConfig extConfig, AccessControlConfig accessControlConfig)
{
_paymentService = paymentService;
_shippingService = shippingService;
_currencyService = currencyService;
_measureService = measureService;
_currencySettings = currencySettings;
_measureSettings = measureSettings;
_dateTimeService = dateTimeService;
_contextAccessor = contextAccessor;
_translationService = translationService;
_applicationLifetime = applicationLifetime;
_webHostEnvironment = webHostEnvironment;
_logger = logger;
_extConfig = extConfig;
_accessControlConfig = accessControlConfig;
}
#endregion
#region Fields
private readonly IPaymentService _paymentService;
private readonly IShippingService _shippingService;
private readonly ICurrencyService _currencyService;
private readonly IMeasureService _measureService;
private readonly IDateTimeService _dateTimeService;
private readonly IContextAccessor _contextAccessor;
private readonly IRequestTranslationService _translationService;
private readonly IHostApplicationLifetime _applicationLifetime;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly ILogger<SystemController> _logger;
private readonly CurrencySettings _currencySettings;
private readonly MeasureSettings _measureSettings;
private readonly ExtensionsConfig _extConfig;
private readonly AccessControlConfig _accessControlConfig;
#endregion
#region Methods
public async Task<IActionResult> SystemInfo()
{
var model = new SystemInfoModel {
GrandVersion = GrandVersion.FullVersion,
GitBranch = GrandVersion.GitBranch,
GitCommit = GrandVersion.GitCommit,
OperatingSystem = RuntimeInformation.OSDescription,
AspNetInfo = RuntimeInformation.FrameworkDescription
};
model.MachineName = Environment.MachineName;
model.WebRootPath = _webHostEnvironment.WebRootPath;
model.ContentRootPath = _webHostEnvironment.ContentRootPath;
model.EnvironmentName = _webHostEnvironment.EnvironmentName;
model.ServerTimeZone = TimeZoneInfo.Local.StandardName;
model.ServerLocalTime = DateTime.Now;
model.ApplicationTime =
_dateTimeService.ConvertToUserTime(DateTime.UtcNow, TimeZoneInfo.Utc, _dateTimeService.CurrentTimeZone);
model.UtcTime = DateTime.UtcNow;
foreach (var header in HttpContext.Request.Headers)
model.ServerVariables.Add(new SystemInfoModel.ServerVariableModel {
Name = header.Key,
Value = header.Value
});
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().ToList().OrderBy(x => x.FullName))
model.LoadedAssemblies.Add(new SystemInfoModel.LoadedAssembly {
FullName = assembly.FullName
});
//current host
var currenthostName = _contextAccessor.StoreContext.CurrentHost.HostName;
if (!string.IsNullOrEmpty(currenthostName) &&
currenthostName.Equals(HttpContext.Request.Host.Value, StringComparison.OrdinalIgnoreCase))
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Pass,
Text = _translationService.GetResource("Admin.System.Warnings.URL.Match")
});
else
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Warning,
Text = string.Format(_translationService.GetResource("Admin.System.Warnings.URL.NoMatch"),
currenthostName, HttpContext.Request.Host.Host)
});
//primary exchange rate currency
var perCurrency = await _currencyService.GetCurrencyById(_currencySettings.PrimaryExchangeRateCurrencyId);
if (perCurrency != null)
{
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Pass,
Text = _translationService.GetResource("Admin.System.Warnings.ExchangeCurrency.Set")
});
if (perCurrency.Rate != 1)
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Fail,
Text = _translationService.GetResource("Admin.System.Warnings.ExchangeCurrency.Rate1")
});
}
else
{
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Fail,
Text = _translationService.GetResource("Admin.System.Warnings.ExchangeCurrency.NotSet")
});
}
//primary store currency
var pscCurrency = await _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId);
if (pscCurrency != null)
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Pass,
Text = _translationService.GetResource("Admin.System.Warnings.PrimaryCurrency.Set")
});
else
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Fail,
Text = _translationService.GetResource("Admin.System.Warnings.PrimaryCurrency.NotSet")
});
//base measure weight
var bWeight = await _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId);
if (bWeight != null)
{
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Pass,
Text = _translationService.GetResource("Admin.System.Warnings.DefaultWeight.Set")
});
if (bWeight.Ratio != 1)
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Fail,
Text = _translationService.GetResource("Admin.System.Warnings.DefaultWeight.Ratio1")
});
}
else
{
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Fail,
Text = _translationService.GetResource("Admin.System.Warnings.DefaultWeight.NotSet")
});
}
//base dimension weight
var bDimension = await _measureService.GetMeasureDimensionById(_measureSettings.BaseDimensionId);
if (bDimension != null)
{
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Pass,
Text = _translationService.GetResource("Admin.System.Warnings.DefaultDimension.Set")
});
if (bDimension.Ratio != 1)
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Fail,
Text = _translationService.GetResource("Admin.System.Warnings.DefaultDimension.Ratio1")
});
}
else
{
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Fail,
Text = _translationService.GetResource("Admin.System.Warnings.DefaultDimension.NotSet")
});
}
//shipping rate coputation methods
var srcMethods = await _shippingService.LoadActiveShippingRateCalculationProviders();
if (srcMethods.Count == 0)
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Fail,
Text = _translationService.GetResource("Admin.System.Warnings.Shipping.NoComputationMethods")
});
if (srcMethods.Count(x => x.ShippingRateCalculationType == ShippingRateCalculationType.Off) > 1)
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Warning,
Text = _translationService.GetResource("Admin.System.Warnings.Shipping.OnlyOneOffline")
});
//payment methods
if ((await _paymentService.LoadActivePaymentMethods()).Any())
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Pass,
Text = _translationService.GetResource("Admin.System.Warnings.PaymentMethods.OK")
});
else
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Fail,
Text = _translationService.GetResource("Admin.System.Warnings.PaymentMethods.NoActive")
});
//performance settings
if (_accessControlConfig.IgnoreStoreLimitations)
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Warning,
Text = _translationService.GetResource("Admin.System.Warnings.Performance.IgnoreStoreLimitations")
});
if (_accessControlConfig.IgnoreAcl)
model.SystemWarnings.Add(new SystemInfoModel.SystemWarningModel {
Level = SystemInfoModel.SystemWarningModel.SystemWarningLevel.Warning,
Text = _translationService.GetResource("Admin.System.Warnings.Performance.IgnoreAcl")
});
return View(model);
}
public async Task<IActionResult> ClearCache(string returnUrl, [FromServices] ICacheBase cacheBase)
{
_logger.LogInformation($"Clear cache has been done by the user: {_contextAccessor.WorkContext.CurrentCustomer.Email}");
await cacheBase.Clear();
//home page
if (string.IsNullOrEmpty(returnUrl))
return RedirectToAction("Index", "Home", new { area = Constants.AreaAdmin });
//prevent open redirection attack
if (!Url.IsLocalUrl(returnUrl))
return RedirectToAction("Index", "Home", new { area = Constants.AreaAdmin });
return Redirect(returnUrl);
}
//POST only: a GET here let any link follower - a crawler, a browser prefetch, a stray click on a
//copied URL - stop the application
[HttpPost]
public IActionResult RestartApplication(string returnUrl = "")
{
_logger.LogInformation($"The application has been restarted by the user {_contextAccessor.WorkContext.CurrentCustomer.Email}");
//stop application
_applicationLifetime.StopApplication();
//home page
if (string.IsNullOrEmpty(returnUrl))
return RedirectToAction("Index", "Home", new { area = Constants.AreaAdmin });
//prevent open redirection attack
if (!Url.IsLocalUrl(returnUrl))
return RedirectToAction("Index", "Home", new { area = Constants.AreaAdmin });
return Redirect(returnUrl);
}
public IActionResult Roslyn()
{
return View(_extConfig.UseRoslynScripts);
}
[HttpPost]
public IActionResult Roslyn(DataSourceRequest command)
{
var scripts = RoslynCompiler.ReferencedScripts != null
? RoslynCompiler.ReferencedScripts.ToList()
: new List<ResultCompiler>();
var gridModel = new DataSourceResult {
Data = scripts.Select(x => new {
FileName = x.OriginalFile,
x.IsCompiled,
Errors = string.Join(",", x.ErrorInfo)
}),
Total = scripts.Count
};
return Json(gridModel);
}
#endregion
}