-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
RegistrationController.cs
183 lines (157 loc) · 5.96 KB
/
RegistrationController.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
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
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using OrchardCore.DisplayManagement.Notify;
using OrchardCore.Entities;
using OrchardCore.Modules;
using OrchardCore.Settings;
using OrchardCore.Users.Models;
using OrchardCore.Users.ViewModels;
namespace OrchardCore.Users.Controllers
{
[Feature("OrchardCore.Users.Registration")]
public class RegistrationController : Controller
{
private readonly UserManager<IUser> _userManager;
private readonly IAuthorizationService _authorizationService;
private readonly ISiteService _siteService;
private readonly INotifier _notifier;
private readonly ILogger _logger;
protected readonly IStringLocalizer S;
protected readonly IHtmlLocalizer H;
public RegistrationController(
UserManager<IUser> userManager,
IAuthorizationService authorizationService,
ISiteService siteService,
INotifier notifier,
ILogger<RegistrationController> logger,
IHtmlLocalizer<RegistrationController> htmlLocalizer,
IStringLocalizer<RegistrationController> stringLocalizer)
{
_userManager = userManager;
_authorizationService = authorizationService;
_siteService = siteService;
_notifier = notifier;
_logger = logger;
H = htmlLocalizer;
S = stringLocalizer;
}
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> Register(string returnUrl = null)
{
var settings = (await _siteService.GetSiteSettingsAsync()).As<RegistrationSettings>();
if (settings.UsersCanRegister != UserRegistrationType.AllowRegistration)
{
return NotFound();
}
ViewData["ReturnUrl"] = returnUrl;
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
var settings = (await _siteService.GetSiteSettingsAsync()).As<RegistrationSettings>();
if (settings.UsersCanRegister != UserRegistrationType.AllowRegistration)
{
return NotFound();
}
if (string.IsNullOrEmpty(model.Email))
{
ModelState.AddModelError("Email", S["Email is required."]);
}
if (ModelState.IsValid)
{
// Check if user with same email already exists
var userWithEmail = await _userManager.FindByEmailAsync(model.Email);
if (userWithEmail != null)
{
ModelState.AddModelError("Email", S["A user with the same email already exists."]);
}
}
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var iUser = await this.RegisterUser(model, S["Confirm your account"], _logger);
// If we get a user, redirect to returnUrl
if (iUser is User user)
{
if (settings.UsersMustValidateEmail && !user.EmailConfirmed)
{
return RedirectToAction("ConfirmEmailSent", new { ReturnUrl = returnUrl });
}
if (settings.UsersAreModerated && !user.IsEnabled)
{
return RedirectToAction("RegistrationPending", new { ReturnUrl = returnUrl });
}
return RedirectToLocal(returnUrl.ToUriComponents());
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> ConfirmEmail(string userId, string code)
{
if (userId == null || code == null)
{
return RedirectToAction(nameof(RegistrationController.Register), "Registration");
}
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return NotFound();
}
var result = await _userManager.ConfirmEmailAsync(user, code);
if (result.Succeeded)
{
return View();
}
return NotFound();
}
[HttpGet]
[AllowAnonymous]
public IActionResult ConfirmEmailSent(string returnUrl = null)
=> View(new { ReturnUrl = returnUrl });
[HttpGet]
[AllowAnonymous]
public IActionResult RegistrationPending(string returnUrl = null)
=> View(new { ReturnUrl = returnUrl });
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SendVerificationEmail(string id)
{
if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.ManageUsers))
{
return Forbid();
}
var user = await _userManager.FindByIdAsync(id) as User;
if (user != null)
{
await this.SendEmailConfirmationTokenAsync(user, S["Confirm your account"]);
await _notifier.SuccessAsync(H["Verification email sent."]);
}
return RedirectToAction(nameof(AdminController.Index), "Admin");
}
private IActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return Redirect("~/");
}
}
}
}