public async Task<IActionResult> ActivateService(SuperUserRegistration registration) { if (_identityProvider.CurrentIdentity == null) { return this.RedirectToSignIn(); } if (await _userService.AnyRegisteredUsersAsync()) { return View("AlreadyActivated"); } var result = await _userService.RegisterFirstSuperUserAsync ( registration, new ModelErrorCollection(ModelState) ); if (result == RegisterNewUserResult.AlreadyRegistered) { return View("AlreadyActivated"); } else if (result == RegisterNewUserResult.Failed) { ViewBag.UserName = _identityProvider.CurrentIdentity.UserName; return View(registration); } else { return View("ActivationSuccessful"); } }
/// <summary> /// Called to register the first super-user. /// </summary> public async Task<RegisterNewUserResult> RegisterFirstSuperUserAsync( SuperUserRegistration registration, IModelErrorCollection errors) { if (await AnyRegisteredUsersAsync()) { return RegisterNewUserResult.AlreadyRegistered; } if (registration.ActivationToken != _activationToken.Value) { errors.AddError("ActivationToken", "Incorrect activation token."); return RegisterNewUserResult.Failed; } if (!await _gitHubUserClient.DoesUserExistAsync(registration.GitHubLogin)) { errors.AddError("GitHubLogin", "The GitHub username does not exist."); return RegisterNewUserResult.Failed; } User user = new User() { UniqueId = _identityProvider.CurrentIdentity.UniqueId, UserName = _identityProvider.CurrentIdentity.UserName, FirstName = registration.FirstName, LastName = registration.LastName, EmailAddress = registration.EmailAddress, EmailAddressConfirmed = true, GitHubLogin = registration.GitHubLogin, SuperUser = true }; _dbContext.Users.Add(user); await _dbContext.SaveChangesAsync(); return RegisterNewUserResult.Success; }