public async Task <int> SetupWordPressAccess(string uidx, string orgId, string accessLevel, string statusInd) { _log.InsertT_PRT_SYS_LOG("Info", "SetupWordPressAccess called."); int actResult = 1; try { ApplicationUser user = await _userManager.FindByIdAsync(uidx); if (user != null) { //_log.InsertT_PRT_SYS_LOG("Info", "we have a valid user."); int orgUserCount = _DbPortal.GetOrgUsersCount(uidx); SetWordPressUri(orgId); //_log.InsertT_PRT_SYS_LOG("Info", WordPressUri); WordPressClient wordPressClient = await GetAuthenticatedWordPressClient(WordPressUri, UserName, Password); var isTokenValid = await wordPressClient.IsValidJWToken(); if (isTokenValid) { //_log.InsertT_PRT_SYS_LOG("Info", "Token is valid."); if (accessLevel == "A" && statusInd == "A") { //_log.InsertT_PRT_SYS_LOG("Info", "AccessLevel/Status is A"); int.TryParse(user.WordPressUserId.ToString(), out var wpuid); if (orgUserCount > 0) { T_PRT_ORG_USERS orgUser = _DbPortal.GetUserOrg(uidx, orgId); if (orgUser == null) { //This situation is unlikely to occur, since we do an upsert before reaching here actResult = 0; _log.InsertT_PRT_SYS_LOG("ERROR", "Org-User not found."); } else { if (wpuid > 0) { //_log.InsertT_PRT_SYS_LOG("Info", "User already exists, update as administrator."); await UpdateWordPressUser(user, wordPressClient, wpuid, "administrator"); AddRemoveUserSite(wpuid, orgId, 1); } else { //_log.InsertT_PRT_SYS_LOG("Info", "User does not exist, add new user as administrator."); User createdUser = await CreateWordPressUser(user, wordPressClient, orgId); if (createdUser != null) { _DbPortal.UpdateT_PRT_USERS_WordPressUserId(user, createdUser.Id); } else { actResult = 0; _log.InsertT_PRT_SYS_LOG("ERROR", "New user could not be added."); } } } } else { if (wpuid > 0) { //_log.InsertT_PRT_SYS_LOG("Info", "User already exist, update as administrator."); await UpdateWordPressUser(user, wordPressClient, wpuid, "administrator"); AddRemoveUserSite(wpuid, orgId, 1); } else { //_log.InsertT_PRT_SYS_LOG("Info", "User does not exist, add new user as administrator."); User createdUser = await CreateWordPressUser(user, wordPressClient, orgId); _DbPortal.UpdateT_PRT_USERS_WordPressUserId(user, createdUser.Id); } } } else { //_log.InsertT_PRT_SYS_LOG("Info", "AccessLevel/Status is NOT A"); if (Int32.TryParse(user.WordPressUserId.ToString(), out var wuid) && wuid > 0) { if (orgUserCount > 1) { AddRemoveUserSite(wuid, orgId, 0); } else { await UpdateWordPressUser(user, wordPressClient, wuid, "inactive"); } } else { actResult = 0; _log.InsertT_PRT_SYS_LOG("ERROR", "Issue with WordPress user id."); } } } else { actResult = 0; _log.InsertT_PRT_SYS_LOG("ERROR", "JWT token is not valid."); } } else { actResult = 0; _log.InsertT_PRT_SYS_LOG("ERROR", "user is null."); } } catch (Exception ex) { actResult = 0; _log.InsertT_PRT_SYS_LOG("ERROR", ex.Message + " : " + ex.StackTrace); //Log errors } return(actResult); }