Example #1
0
        public async Task <IActionResult> OrgUserEdit(int?edit_oRG_USER_IDX, string uidx, string org_id, string AccessLevel, string StatusInd)
        {
            if (ModelState.IsValid)
            {
                int newID = _DbPortal.InsertUpdateT_PRT_ORG_USERS(edit_oRG_USER_IDX, org_id, uidx, AccessLevel, StatusInd, User.Identity.Name);

                if (newID == 0)
                {
                    TempData["Error"] = "Unable to add user to organization.";
                }
                else
                {
                    WordPressHelper wordPressHelper = new WordPressHelper(_userManager, _DbPortal, _log, _emailSender);
                    int             isWPUserAdded   = await wordPressHelper.SetupWordPressAccess(uidx, org_id, AccessLevel, StatusInd);

                    TempData["Success"] = "Record successfully added.";
                }
            }
            else
            {
                TempData["Error"] = "Unable to add user to organization.";
            }

            return(RedirectToAction("UserEdit", "Admin", new { id = uidx }));
        }
Example #2
0
        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)));
        }