public async Task <IActionResult> PortalRegister(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FIRST_NAME = model.FirstName, LAST_NAME = model.LastName }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); //Encrypt and store password to database //used for WordPress user management var _user = await _userManager.FindByEmailAsync(model.Email); if (_user != null) { _DbPortal.UpdateT_PRT_USERS_PasswordEncrypt(_user, model.Password); } var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); bool emailSucc = _emailSender.SendEmail(null, model.Email, null, null, null, null, "EMAIL_CONFIRM", "callbackUrl", callbackUrl); //_log.InsertT_PRT_SYS_LOG("info-cburl", callbackUrl); //if users email is associated with an organization, then associate user with org List <T_PRT_ORGANIZATIONS> orgs = _DbPortal.GetT_PRT_ORGANIZATIONS_ByEmail(model.Email); if (orgs != null && orgs.Count == 1) { _DbPortal.InsertUpdateT_PRT_ORG_USERS(null, orgs[0].ORG_ID, user.Id, "U", "A", user.Id); } TempData["Success"] = "Account has been created. Please check your email to verify your account."; TempData["toastrTimeout"] = "true"; return(RedirectToLocal(returnUrl)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> ChangePassword(ChangePasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await _userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } var changePasswordResult = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword); if (!changePasswordResult.Succeeded) { AddErrors(changePasswordResult); return(View(model)); } _log.InsertT_PRT_SYS_LOG("Info", "Password changed successfully, begin wordpress activities."); string wpMessage = ""; WordPressHelper wordPressHelper = new WordPressHelper(_userManager, _DbPortal, _log, _emailSender); //We need this password to setup in WordPress _DbPortal.UpdateT_PRT_USERS_PasswordEncrypt(user, model.NewPassword); if (user.WordPressUserId == null || user.WordPressUserId <= 0) { _log.InsertT_PRT_SYS_LOG("Info", "WordPressUserId not set, hence create new user."); List <UserOrgDisplayType> userOrgDisplayTypes = _DbPortal.GetT_PRT_ORG_USERS_ByUserID(user.Id); if (userOrgDisplayTypes != null && userOrgDisplayTypes.Count > 0) { _log.InsertT_PRT_SYS_LOG("Info", "User-Org relation found."); int isWordPressUserCreated = 0; foreach (UserOrgDisplayType uodt in userOrgDisplayTypes) { IList <string> sites = "ABSHAWNEE,KICKAPOO,MCNCREEK,SFNOES".Split(","); if (sites.Contains(uodt.ORG_ID.Trim().ToUpper())) { if (uodt.ACCESS_LEVEL == "A" && uodt.STATUS_IND == "A") { //_log.InsertT_PRT_SYS_LOG("Info", "Create user for org:" + uodt.ORG_NAME); if (isWordPressUserCreated == 0) { isWordPressUserCreated = await wordPressHelper.SetupWordPressAccess(user.Id, uodt.ORG_ID, uodt.ACCESS_LEVEL, uodt.STATUS_IND); if (isWordPressUserCreated == 0) { //_log.InsertT_PRT_SYS_LOG("Info", "User could not be created for org:" + uodt.ORG_NAME); wpMessage = "(Something went wrong with WordPress related activity!)"; } //_log.InsertT_PRT_SYS_LOG("Info", "User created for org:" + uodt.ORG_NAME); } else { //_log.InsertT_PRT_SYS_LOG("Info", "Assign user to remaining sites/organizations: " + uodt.ORG_NAME); //Assign user to remaining sites int wpuid = 0; Int32.TryParse(user.WordPressUserId.ToString(), out wpuid); var isUserUpdated = wordPressHelper.AddRemoveUserSite(wpuid, uodt.ORG_ID, 1); if (isUserUpdated == false) { //_log.InsertT_PRT_SYS_LOG("Info", "User could not be assigned to remaining sites/organizations for: " + uodt.ORG_NAME); wpMessage = "(Something went wrong with WordPress related activity!)"; } //_log.InsertT_PRT_SYS_LOG("Info", "User assigned to remaining sites/organizations for: " + uodt.ORG_NAME); } } } } } } else { _log.InsertT_PRT_SYS_LOG("Info", "WordPressUserId is set hence we update password for all the sites/organizations."); List <UserOrgDisplayType> userOrgDisplayTypes = _DbPortal.GetT_PRT_ORG_USERS_ByUserID(user.Id); Boolean isPasswordUpdated = false; foreach (UserOrgDisplayType uodt in userOrgDisplayTypes) { IList <string> sites = "ABSHAWNEE,KICKAPOO,MCNCREEK,SFNOES".Split(","); if (sites.Contains(uodt.ORG_ID.Trim().ToUpper())) { if (uodt.ACCESS_LEVEL == "A" && uodt.STATUS_IND == "A") { int wpuid = 0; Int32.TryParse(user.WordPressUserId.ToString(), out wpuid); WordPressClient wordPressClient = await wordPressHelper.GetAuthenticatedWordPressClient(uodt.ORG_ID); string role = "administrator"; if (uodt.ACCESS_LEVEL != "A" || uodt.STATUS_IND != "A") { role = "inactive"; } isPasswordUpdated = await wordPressHelper.UpdateWordPressUser(user, wordPressClient, wpuid, role); if (isPasswordUpdated == false) { _log.InsertT_PRT_SYS_LOG("Info", "Password could not be updated for org: " + uodt.ORG_NAME); wpMessage = "(Something went wrong with WordPress related activity!)"; } _log.InsertT_PRT_SYS_LOG("Info", "Password updated for org: " + uodt.ORG_NAME); } } } } await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User changed their password successfully."); StatusMessage = "Your password has been changed. " + wpMessage; return(RedirectToAction(nameof(ChangePassword))); }