This repository was archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 754
Expand file tree
/
Copy pathBaseController.cs
More file actions
406 lines (351 loc) · 16.4 KB
/
Copy pathBaseController.cs
File metadata and controls
406 lines (351 loc) · 16.4 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using Grand.Core;
using Grand.Domain.Customers;
using Grand.Framework.Events;
using Grand.Framework.Kendoui;
using Grand.Framework.Localization;
using Grand.Framework.Mvc.Filters;
using Grand.Framework.UI;
using Grand.Services.Common;
using Grand.Services.Localization;
using Grand.Services.Logging;
using Grand.Services.Stores;
using MediatR;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace Grand.Framework.Controllers
{
/// <summary>
/// Base controller
/// </summary>
[SignOutFromExternalAuthentication]
[ValidatePassword]
[SaveIpAddress]
[SaveLastActivity]
[SaveLastVisitedPage]
public abstract class BaseController : Controller
{
#region Rendering
/// <summary>
/// Render componentto string
/// </summary>
/// <param name="componentName">Component name</param>
/// <returns>Result</returns>
protected virtual string RenderViewComponentToString(string componentName, object arguments = null)
{
//original implementation: https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/ViewComponentResultExecutor.cs
//we customized it to allow running from controllers
//TODO add support for parameters (pass ViewComponent as input parameter)
if (String.IsNullOrEmpty(componentName))
throw new ArgumentNullException("componentName");
var actionContextAccessor = HttpContext.RequestServices.GetService(typeof(IActionContextAccessor)) as IActionContextAccessor;
if (actionContextAccessor == null)
throw new Exception("IActionContextAccessor cannot be resolved");
var context = new ActionContext(HttpContext, RouteData, ControllerContext.ActionDescriptor, ModelState);
var viewComponentResult = ViewComponent(componentName, arguments);
var viewData = ViewData;
if (viewData == null)
{
throw new NotImplementedException();
//TODO viewData = new ViewDataDictionary(_modelMetadataProvider, context.ModelState);
}
var tempData = TempData;
if (tempData == null)
{
throw new NotImplementedException();
//TODO tempData = _tempDataDictionaryFactory.GetTempData(context.HttpContext);
}
using (var writer = new StringWriter())
{
var viewContext = new ViewContext(
context,
NullView.Instance,
viewData,
tempData,
writer,
new HtmlHelperOptions());
// IViewComponentHelper is stateful, we want to make sure to retrieve it every time we need it.
var viewComponentHelper = context.HttpContext.RequestServices.GetRequiredService<IViewComponentHelper>();
(viewComponentHelper as IViewContextAware)?.Contextualize(viewContext);
Task<IHtmlContent> result = viewComponentResult.ViewComponentType == null ?
viewComponentHelper.InvokeAsync(viewComponentResult.ViewComponentName, viewComponentResult.Arguments) :
viewComponentHelper.InvokeAsync(viewComponentResult.ViewComponentType, viewComponentResult.Arguments);
result.Result.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
/// <summary>
/// Render partial view to string
/// </summary>
/// <returns>Result</returns>
protected virtual async Task<string> RenderPartialViewToString()
{
return await RenderPartialViewToString(null, null);
}
/// <summary>
/// Render partial view to string
/// </summary>
/// <param name="viewName">View name</param>
/// <returns>Result</returns>
protected virtual async Task<string> RenderPartialViewToString(string viewName)
{
return await RenderPartialViewToString(viewName, null);
}
/// <summary>
/// Render partial view to string
/// </summary>
/// <param name="model">Model</param>
/// <returns>Result</returns>
protected virtual async Task<string> RenderPartialViewToString(object model)
{
return await RenderPartialViewToString(null, model);
}
/// <summary>
/// Render partial view to string
/// </summary>
/// <param name="viewName">View name</param>
/// <param name="model">Model</param>
/// <returns>Result</returns>
protected virtual async Task<string> RenderPartialViewToString(string viewName, object model)
{
//get Razor view engine
var razorViewEngine = HttpContext.RequestServices.GetRequiredService<IRazorViewEngine>();
//create action context
var actionContext = new ActionContext(HttpContext, RouteData, ControllerContext.ActionDescriptor, ModelState);
//set view name as action name in case if not passed
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.ActionDescriptor.ActionName;
//set model
ViewData.Model = model;
var viewResult = razorViewEngine.FindView(actionContext, viewName, false);
if (viewResult.View == null)
{
//or try to get a view by the path
viewResult = razorViewEngine.GetView(null, viewName, false);
if (viewResult.View == null)
throw new ArgumentNullException($"{viewName} view was not found");
}
using (var stringWriter = new StringWriter())
{
var viewContext = new ViewContext(actionContext, viewResult.View, ViewData, TempData, stringWriter, new HtmlHelperOptions());
await viewResult.View.RenderAsync(viewContext);
return stringWriter.GetStringBuilder().ToString();
}
}
#endregion
#region Notifications
/// <summary>
/// Display success notification
/// </summary>
/// <param name="message">Message</param>
/// <param name="persistForTheNextRequest">A value indicating whether a message should be persisted for the next request</param>
protected virtual void SuccessNotification(string message, bool persistForTheNextRequest = true)
{
AddNotification(NotifyType.Success, message, persistForTheNextRequest);
}
/// <summary>
/// Display warning notification
/// </summary>
/// <param name="message">Message</param>
/// <param name="persistForTheNextRequest">A value indicating whether a message should be persisted for the next request</param>
protected virtual void WarningNotification(string message, bool persistForTheNextRequest = true)
{
AddNotification(NotifyType.Warning, message, persistForTheNextRequest);
}
/// <summary>
/// Display error notification
/// </summary>
/// <param name="message">Message</param>
/// <param name="persistForTheNextRequest">A value indicating whether a message should be persisted for the next request</param>
protected virtual void ErrorNotification(string message, bool persistForTheNextRequest = true)
{
AddNotification(NotifyType.Error, message, persistForTheNextRequest);
}
/// <summary>
/// Display error notification
/// </summary>
/// <param name="persistForTheNextRequest">A value indicating whether a message should be persisted for the next request</param>
protected virtual void ErrorNotification(ModelStateDictionary ModelState, bool persistForTheNextRequest = true)
{
var modelErrors = new List<string>();
foreach (var modelState in ModelState.Values)
{
foreach (var modelError in modelState.Errors)
{
modelErrors.Add(modelError.ErrorMessage);
}
}
AddNotification(NotifyType.Error, string.Join(',', modelErrors), persistForTheNextRequest);
}
/// <summary>
/// Display error notification
/// </summary>
/// <param name="exception">Exception</param>
/// <param name="persistForTheNextRequest">A value indicating whether a message should be persisted for the next request</param>
/// <param name="logException">A value indicating whether exception should be logged</param>
protected virtual void ErrorNotification(Exception exception, bool persistForTheNextRequest = true, bool logException = true)
{
if (logException)
LogException(exception);
AddNotification(NotifyType.Error, exception.Message, persistForTheNextRequest);
}
/// <summary>
/// Log exception
/// </summary>
/// <param name="exception">Exception</param>
protected void LogException(Exception exception)
{
var workContext = HttpContext.RequestServices.GetRequiredService<IWorkContext>();
var logger = HttpContext.RequestServices.GetRequiredService<ILogger>();
var customer = workContext.CurrentCustomer;
logger.Error(exception.Message, exception, customer);
}
/// <summary>
/// Display notification
/// </summary>
/// <param name="type">Notification type</param>
/// <param name="message">Message</param>
/// <param name="persistForTheNextRequest">A value indicating whether a message should be persisted for the next request</param>
protected virtual void AddNotification(NotifyType type, string message, bool persistForTheNextRequest)
{
var dataKey = string.Format("grand.notifications.{0}", type);
if (persistForTheNextRequest)
{
//1. Compare with null (first usage)
//2. For some unknown reasons sometimes List<string> is converted to string[]. And it throws exceptions. That's why we reset it
if (TempData[dataKey] == null || !(TempData[dataKey] is List<string>))
TempData[dataKey] = new List<string>();
((List<string>)TempData[dataKey]).Add(message);
}
else
{
//1. Compare with null (first usage)
//2. For some unknown reasons sometimes List<string> is converted to string[]. And it throws exceptions. That's why we reset it
if (ViewData[dataKey] == null || !(ViewData[dataKey] is List<string>))
ViewData[dataKey] = new List<string>();
((List<string>)ViewData[dataKey]).Add(message);
}
}
/// <summary>
/// Error's json data for kendo grid
/// </summary>
/// <param name="errorMessage">Error message</param>
/// <returns>Error's json data</returns>
protected JsonResult ErrorForKendoGridJson(string errorMessage)
{
var gridModel = new DataSourceResult {
Errors = errorMessage
};
return Json(gridModel);
}
/// <summary>
/// Error's json data for kendo grid
/// </summary>
/// <param name="modelState">Model state</param>
/// <returns>Error's json data</returns>
protected JsonResult ErrorForKendoGridJson(ModelStateDictionary modelState)
{
var gridModel = new DataSourceResult {
Errors = ModelState.SerializeErrors()
};
return Json(gridModel);
}
/// <summary>
/// Display "Edit" (manage) link (in public store)
/// </summary>
/// <param name="editPageUrl">Edit page URL</param>
protected virtual void DisplayEditLink(string editPageUrl)
{
var pageHeadBuilder = HttpContext.RequestServices.GetRequiredService<IPageHeadBuilder>();
pageHeadBuilder.AddEditPageUrl(editPageUrl);
}
/// <summary>
/// Get active store scope (for multi-store configuration mode)
/// </summary>
/// <param name="storeService">Store service</param>
/// <param name="workContext">Work context</param>
/// <returns>Store ID; 0 if we are in a shared mode</returns>
protected virtual async Task<string> GetActiveStoreScopeConfiguration(IStoreService storeService, IWorkContext workContext)
{
//ensure that we have 2 (or more) stores
if ((await storeService.GetAllStores()).Count < 2)
return "";
var storeId = workContext.CurrentCustomer.GetAttributeFromEntity<string>(SystemCustomerAttributeNames.AdminAreaStoreScopeConfiguration);
var store = await storeService.GetStoreById(storeId);
return store != null ? store.Id : "";
}
#endregion
#region Localization
/// <summary>
/// Add locales for localizable entities
/// </summary>
/// <typeparam name="TLocalizedModelLocal">Localizable model</typeparam>
/// <param name="languageService">Language service</param>
/// <param name="locales">Locales</param>
protected virtual async Task AddLocales<TLocalizedModelLocal>(ILanguageService languageService,
IList<TLocalizedModelLocal> locales) where TLocalizedModelLocal : ILocalizedModelLocal
{
await AddLocales(languageService, locales, null);
}
/// <summary>
/// Add locales for localizable entities
/// </summary>
/// <typeparam name="TLocalizedModelLocal">Localizable model</typeparam>
/// <param name="languageService">Language service</param>
/// <param name="locales">Locales</param>
/// <param name="configure">Configure action</param>
protected virtual async Task AddLocales<TLocalizedModelLocal>(ILanguageService languageService,
IList<TLocalizedModelLocal> locales, Action<TLocalizedModelLocal, string> configure) where TLocalizedModelLocal : ILocalizedModelLocal
{
foreach (var language in await languageService.GetAllLanguages(true))
{
var locale = Activator.CreateInstance<TLocalizedModelLocal>();
locale.LanguageId = language.Id;
if (configure != null)
configure.Invoke(locale, locale.LanguageId);
locales.Add(locale);
}
}
#endregion
#region Security
/// <summary>
/// Access denied view
/// </summary>
/// <returns>Access denied view</returns>
protected virtual IActionResult AccessDeniedView()
{
var webHelper = HttpContext.RequestServices.GetRequiredService<IWebHelper>();
return RedirectToAction("AccessDenied", "Home", new { pageUrl = webHelper.GetRawUrl(this.Request) });
}
/// <summary>
/// Access denied json data for kendo grid
/// </summary>
/// <returns>Access denied json data</returns>
protected JsonResult AccessDeniedKendoGridJson()
{
var localizationService = HttpContext.RequestServices.GetRequiredService<ILocalizationService>();
return ErrorForKendoGridJson(localizationService.GetResource("Admin.AccessDenied.Description"));
}
#endregion
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
// event notification before execute
var mediator = context.HttpContext.RequestServices.GetService<IMediator>();
await mediator.Publish(new ActionExecutingContextNotification(context, true));
await next();
//event notification after execute
await mediator.Publish(new ActionExecutingContextNotification(context, false));
}
}
}