public async Task <JsonResult> OrgUserDelete(int id, string id2) { T_PRT_ORG_USERS orgUser = _DbPortal.GetT_PRT_ORG_USERS_ByOrgUserID(id); int succId = _DbPortal.DeleteT_PRT_ORG_USERS(orgUser); if (succId > 0) { WordPressHelper.SetUserManager(_userManager); ApplicationUser appUser = await WordPressHelper.GetApplicationUser(orgUser.Id); WordPressHelper wordPressHelper = new WordPressHelper(_userManager, _DbPortal, _log, _emailSender); int orgUserCount = _DbPortal.GetOrgUsersCount(orgUser.Id); if (orgUserCount == 0) { //if we have user in wordpress, make it inactive if (appUser.WordPressUserId > 0) { //string wordPressUri = wordPressHelper.SetWordPressUri(orgUser.ORG_ID); // string userName = wordPressHelper.GetUserName(); // string password = wordPressHelper.GetPassword(); int.TryParse(appUser.WordPressUserId.ToString(), out var wpuid); //WordPressClient wordPressClient = await wordPressHelper.GetAuthenticatedWordPressClient(wordPressUri, userName, password); WordPressClient wordPressClient = await wordPressHelper.GetAuthenticatedWordPressClient(orgUser.ORG_ID); bool isUserUpdated = await wordPressHelper.UpdateWordPressUser(appUser, wordPressClient, wpuid, "inactive"); } } else { //revoke access from the site/organization from wordpress int.TryParse(appUser.WordPressUserId.ToString(), out var wpuid); wordPressHelper.AddRemoveUserSite(wpuid, orgUser.ORG_ID, 0); } return(Json("Success")); } else { return(Json("Unable to delete user from organization.")); } }
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))); }