Beispiel #1
0
        //************************************ ORGANIZATION USER CLIENT  ***************************************
        public IActionResult OrgUserClients(int id)
        {
            T_PRT_ORG_USERS _orgUser = _DbPortal.GetT_PRT_ORG_USERS_ByOrgUserID(id);

            if (_orgUser != null)
            {
                var model = new OrgUserEditViewModel
                {
                    OrgUserIDX     = id,
                    OrgUserClients = _DbPortal.GetT_PRT_ORG_USERS_CLIENT_ByOrgUserID(id),
                    UserIDX        = _orgUser.Id,
                    ddl_Clients    = _DbPortal.GetT_PRT_CLIENTS().Select(x => new SelectListItem
                    {
                        Value = x.CLIENT_ID,
                        Text  = x.CLIENT_NAME
                    })
                };

                return(View(model));
            }
            else
            {
                TempData["Error"] = "No matching record found.";
                return(RedirectToAction("UserList"));
            }
        }
Beispiel #2
0
        //************************************ ORGANIZATION USER CLIENT (non-Global Admin)  ***************************************
        public IActionResult ManageUsers(int?id)
        {
            //********* id = OrgUserClientID

            string _UserIDX = _userManager.GetUserId(User);

            var model = new ManageUsersViewModel
            {
                ddl_AdminOfOrgClients = _DbPortal.GetT_PRT_ORG_USERS_CLIENT_AdminByUserID(_UserIDX).Select(x => new SelectListItem
                {
                    Value = x.ORG_USER_CLIENT_IDX.ToString(),
                    Text  = x.ORG_CLIENT_ALIAS + " - " + x.CLIENT_ID
                })
            };

            //get users currently listed for the org/client
            //get users for the organization
            if (id != null)
            {
                model.selOrgUserClient = id;

                T_PRT_ORG_USER_CLIENT _ouc = _DbPortal.GetT_PRT_ORG_USERS_CLIENT_ByID((int)id);
                if (_ouc != null)
                {
                    T_PRT_ORG_USERS _ou = _DbPortal.GetT_PRT_ORG_USERS_ByOrgUserID(_ouc.ORG_USER_IDX);
                    if (_ou != null)
                    {
                        model.client_id      = _ouc.CLIENT_ID;
                        model.selOrg         = _ou.ORG_ID;
                        model.OrgUserClients = _DbPortal.GetT_PRT_ORG_USERS_CLIENT_ByOrgIDandClientID(_ou.ORG_ID, _ouc.CLIENT_ID, false);
                        model.ddl_Users      = _DbPortal.GetT_PRT_ORG_USERS_ByOrgID(_ou.ORG_ID).Select(x => new SelectListItem
                        {
                            Value = x.ORG_USER_IDX.ToString(),
                            Text  = x.USER_NAME
                        });
                    }
                }
            }
            ;

            return(View(model));
        }
Beispiel #3
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);
        }
Beispiel #5
0
        public JsonResult AccessRightsRequest(int?orgUser, string client)
        {
            string _UserIDX = _userManager.GetUserId(User);

            T_PRT_ORG_USERS _ou = _DbPortal.GetT_PRT_ORG_USERS_ByOrgUserID(orgUser ?? -1);

            if (_ou != null)
            {
                int SuccID = _DbPortal.InsertUpdateT_PRT_ORG_USERS_CLIENT(null, orgUser, client, false, "R", _UserIDX);

                //return response
                if (SuccID > 0)
                {
                    //send email
                    List <string> _emailRecipients = new List <string>();

                    //**************first try to send to org / client admins
                    List <OrgUserClientDisplayType> _orgUserClientAdmins = _DbPortal.GetT_PRT_ORG_USERS_CLIENT_ByOrgIDandClientID(_ou.ORG_ID, client, true);
                    if (_orgUserClientAdmins != null && _orgUserClientAdmins.Count > 0)
                    {
                        foreach (OrgUserClientDisplayType _orgUserClientAdmin in _orgUserClientAdmins)
                        {
                            ApplicationUser _u = _userManager.FindByIdAsync(_orgUserClientAdmin.UserID).Result;
                            if (_u != null)
                            {
                                _emailRecipients.Add(_u.Email);
                            }
                        }
                    }

                    //**************if none found, then send to org admins

                    //**************finally send to portal admins
                    if (_emailRecipients.Count == 0)
                    {
                        IdentityRole _r = _roleManager.FindByNameAsync("PortalAdmin").Result;

                        IEnumerable <ApplicationUser> _us = _DbPortal.GetT_PRT_USERS_BelongingToRole(_r.Id);
                        if (_us != null)
                        {
                            foreach (ApplicationUser _u in _us)
                            {
                                _emailRecipients.Add(_u.Email);
                            }
                        }
                    }

                    string _UserName = _userManager.GetUserName(User);

                    //construct email parameters
                    List <emailParam> emailParams = new List <emailParam>()
                    {
                        new emailParam()
                        {
                            PARAM_NAME = "userName", PARAM_VAL = _UserName
                        },
                        new emailParam()
                        {
                            PARAM_NAME = "client", PARAM_VAL = client
                        },
                        new emailParam()
                        {
                            PARAM_NAME = "orgID", PARAM_VAL = _ou.ORG_ID
                        }
                    };

                    foreach (string _emailRecipient in _emailRecipients)
                    {
                        _emailSender.SendEmail(null, _emailRecipient, null, null, null, null, "ACCESS_REQUEST", emailParams);
                    }


                    return(Json(new
                    {
                        msg = "Success",
                        redirectUrl = Url.Action("AccessRights", "Manage")
                    }));
                }
            }

            //if got this far, it failed
            return(Json(new { msg = "Unable to request access." }));
        }