|
5 | 5 | using OrchardCore.Email; |
6 | 6 | using OrchardCoreContrib.Email.Hotmail.Drivers; |
7 | 7 | using OrchardCoreContrib.Email.Hotmail.ViewModels; |
8 | | -using System.Threading.Tasks; |
9 | 8 |
|
10 | | -namespace OrchardCoreContrib.Email.Hotmail.Controllers |
| 9 | +namespace OrchardCoreContrib.Email.Hotmail.Controllers; |
| 10 | + |
| 11 | +public class AdminController( |
| 12 | + IHtmlLocalizer<AdminController> H, |
| 13 | + IAuthorizationService authorizationService, |
| 14 | + INotifier notifier, |
| 15 | + ISmtpService smtpService) : Controller |
11 | 16 | { |
12 | | - public class AdminController : Controller |
| 17 | + [HttpGet] |
| 18 | + public async Task<IActionResult> Index() |
13 | 19 | { |
14 | | - private readonly IAuthorizationService _authorizationService; |
15 | | - private readonly INotifier _notifier; |
16 | | - private readonly ISmtpService _smtpService; |
17 | | - private readonly IHtmlLocalizer H; |
18 | | - |
19 | | - public AdminController( |
20 | | - IHtmlLocalizer<AdminController> htmlLocalizer, |
21 | | - IAuthorizationService authorizationService, |
22 | | - INotifier notifier, |
23 | | - ISmtpService smtpService) |
| 20 | + if (!await authorizationService.AuthorizeAsync(User, HotmailPermissions.ManageHotmailSettings)) |
24 | 21 | { |
25 | | - H = htmlLocalizer; |
26 | | - _authorizationService = authorizationService; |
27 | | - _notifier = notifier; |
28 | | - _smtpService = smtpService; |
| 22 | + return Forbid(); |
29 | 23 | } |
30 | 24 |
|
31 | | - [HttpGet] |
32 | | - public async Task<IActionResult> Index() |
33 | | - { |
34 | | - if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageHotmailSettings)) |
35 | | - { |
36 | | - return Forbid(); |
37 | | - } |
| 25 | + return View(); |
| 26 | + } |
38 | 27 |
|
39 | | - return View(); |
| 28 | + [HttpPost, ActionName(nameof(Index))] |
| 29 | + public async Task<IActionResult> IndexPost(HotmailSettingsViewModel model) |
| 30 | + { |
| 31 | + if (!await authorizationService.AuthorizeAsync(User, HotmailPermissions.ManageHotmailSettings)) |
| 32 | + { |
| 33 | + return Forbid(); |
40 | 34 | } |
41 | 35 |
|
42 | | - [HttpPost, ActionName(nameof(Index))] |
43 | | - public async Task<IActionResult> IndexPost(HotmailSettingsViewModel model) |
| 36 | + if (ModelState.IsValid) |
44 | 37 | { |
45 | | - if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageHotmailSettings)) |
46 | | - { |
47 | | - return Forbid(); |
48 | | - } |
49 | | - |
50 | | - if (ModelState.IsValid) |
51 | | - { |
52 | | - var message = CreateMessageFromViewModel(model); |
| 38 | + var message = CreateMessageFromViewModel(model); |
53 | 39 |
|
54 | | - var result = await _smtpService.SendAsync(message); |
| 40 | + var result = await smtpService.SendAsync(message); |
55 | 41 |
|
56 | | - if (!result.Succeeded) |
57 | | - { |
58 | | - foreach (var error in result.Errors) |
59 | | - { |
60 | | - ModelState.AddModelError("*", error.ToString()); |
61 | | - } |
62 | | - } |
63 | | - else |
| 42 | + if (!result.Succeeded) |
| 43 | + { |
| 44 | + foreach (var error in result.Errors) |
64 | 45 | { |
65 | | - await _notifier.SuccessAsync(H["Message sent successfully"]); |
66 | | - |
67 | | - return Redirect(Url.Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = HotmailSettingsDisplayDriver.GroupId })); |
| 46 | + ModelState.AddModelError("*", error.ToString()); |
68 | 47 | } |
69 | 48 | } |
| 49 | + else |
| 50 | + { |
| 51 | + await notifier.SuccessAsync(H["Message sent successfully"]); |
70 | 52 |
|
71 | | - return View(model); |
| 53 | + return Redirect(Url.Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = HotmailSettingsDisplayDriver.GroupId })); |
| 54 | + } |
72 | 55 | } |
73 | 56 |
|
74 | | - private MailMessage CreateMessageFromViewModel(HotmailSettingsViewModel testSettings) |
75 | | - { |
76 | | - var message = new MailMessage |
77 | | - { |
78 | | - To = testSettings.To, |
79 | | - Bcc = testSettings.Bcc, |
80 | | - Cc = testSettings.Cc, |
81 | | - ReplyTo = testSettings.ReplyTo |
82 | | - }; |
| 57 | + return View(model); |
| 58 | + } |
83 | 59 |
|
84 | | - if (!string.IsNullOrWhiteSpace(testSettings.Sender)) |
85 | | - { |
86 | | - message.Sender = testSettings.Sender; |
87 | | - } |
| 60 | + private MailMessage CreateMessageFromViewModel(HotmailSettingsViewModel testSettings) |
| 61 | + { |
| 62 | + var message = new MailMessage |
| 63 | + { |
| 64 | + To = testSettings.To, |
| 65 | + Bcc = testSettings.Bcc, |
| 66 | + Cc = testSettings.Cc, |
| 67 | + ReplyTo = testSettings.ReplyTo |
| 68 | + }; |
88 | 69 |
|
89 | | - if (!string.IsNullOrWhiteSpace(testSettings.Subject)) |
90 | | - { |
91 | | - message.Subject = testSettings.Subject; |
92 | | - } |
| 70 | + if (!string.IsNullOrWhiteSpace(testSettings.Sender)) |
| 71 | + { |
| 72 | + message.Sender = testSettings.Sender; |
| 73 | + } |
93 | 74 |
|
94 | | - if (!string.IsNullOrWhiteSpace(testSettings.Body)) |
95 | | - { |
96 | | - message.Body = testSettings.Body; |
97 | | - } |
| 75 | + if (!string.IsNullOrWhiteSpace(testSettings.Subject)) |
| 76 | + { |
| 77 | + message.Subject = testSettings.Subject; |
| 78 | + } |
98 | 79 |
|
99 | | - return message; |
| 80 | + if (!string.IsNullOrWhiteSpace(testSettings.Body)) |
| 81 | + { |
| 82 | + message.Body = testSettings.Body; |
100 | 83 | } |
| 84 | + |
| 85 | + return message; |
101 | 86 | } |
102 | 87 | } |
0 commit comments