Esempio n. 1
0
        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 <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);
        }