예제 #1
0
        public object ChangeRole(ChangeUserRoleModel model)
        {
            bool success = _userService.ChangeRole(model);

            if (!success)
            {
                return(new ResponseDetails(false, $"User with Id: { model.UserId } or Role with Id : { model.RoleId } is not found."));
            }

            return(new ResponseDetails(true, "Role updated successfully."));
        }
        public ActionResult ManageAllUsers()
        {
            #region Get all users
            // Get the SuperAdmin account -- only the SuperAdmin can access this page, so get them from the session
            var superAdmin = UserManager.FindById(User.Identity.GetUserId());

            // Build a list of all the current users in the system, minus the SuperAdmin
            List <UserProfile> users = unitOfWork.Profiles.Find(x => x.UserID != superAdmin.Id).ToList();

            // Convert to the model used in the view
            List <ChangeUserRoleModel> usersToChange = new List <ChangeUserRoleModel>();
            foreach (var user in users)
            {
                // Get the required information from the AspNet tables
                var currentUserFromSystem = UserManager.FindById(user.UserID);
                var roleName = UserManager.GetRoles(currentUserFromSystem.Id).FirstOrDefault();

                // Convert the information to the correct model for the view
                ChangeUserRoleModel currentUserToChange = new ChangeUserRoleModel
                {
                    User = user,
                    Role = roleName
                };

                // Add the current user to the list
                usersToChange.Add(currentUserToChange);
            }
            #endregion

            #region SelectList for roles
            // Set up a SelectList containing the available roles a user can have
            List <SelectListItem> list = new List <SelectListItem>();
            foreach (var role in RoleManager.Roles)
            {
                // Exclude the SuperAdmin role, as there should only be one
                if (role.Name != "SuperAdmin")
                {
                    list.Add(new SelectListItem()
                    {
                        Value = role.Name, Text = role.Name
                    });
                }
            }

            // Store the list to be used in the view
            ViewBag.Roles = list;
            #endregion

            // Return the view
            return(View(usersToChange));
        }
예제 #3
0
        public async Task <IActionResult> EditUserRole(string id)
        {
            User user = await Context.User.FindAsync(id);

            if (user != null)
            {
                var userRoles = await _userManager.GetRolesAsync(user);

                var allRoles = _roleManager.Roles.ToList();
                ChangeUserRoleModel model = new ChangeUserRoleModel
                {
                    UserId    = user.Id,
                    UserEmail = user.Email,
                    UserRoles = userRoles,
                    AllRoles  = allRoles
                };
                return(View(model));
            }
            return(NotFound());
        }
예제 #4
0
        public bool ChangeRole(ChangeUserRoleModel model)
        {
            bool roleExists = _unitOfWork.RoleRepository.RoleExists(model.RoleId);

            if (roleExists)
            {
                User user = _unitOfWork.UserRepository.Get(model.UserId);

                if (user != null)
                {
                    user.RoleId = model.RoleId;

                    _unitOfWork.UserRepository.Update(user);
                    _unitOfWork.SaveChanges();

                    return(true);
                }
            }

            return(false);
        }
        public ActionResult ManageAllUsers(ChangeUserRoleModel model)
        {
            // Make sure the model is in a valid state before we do anything with it
            if (ModelState.IsValid)
            {
                // Look up the given user and remove their current role
                var givenUser = UserManager.FindById(model.User.UserID);

                if (givenUser != null)
                {
                    #region Changing from "StandardUser" to "Administrator"

                    /* If we're changing this user from a "StandardUser" to an "Administrator", we need to remove their
                     * Call Assignments, if any exist */

                    // Look up the given user in the UserProfiles table
                    var currentUser = unitOfWork.Profiles.Find(x => x.ProfileID == model.User.ProfileID).FirstOrDefault();
                    if (currentUser != null && model.Role == "Administrator")
                    {
                        // Check if this user is a Caller, remove their Call Assignments from the table
                        if (currentUser.IsCaller)
                        {
                            // Build a list of all the current Call Assignments
                            List <CallAssignment> assignments = unitOfWork.Assignments.Find(x => x.CallerID == currentUser.UserID).ToList();
                            if (assignments != null)
                            {
                                // Remove all Call Assignments assigned to this user
                                foreach (var entry in assignments)
                                {
                                    unitOfWork.Assignments.Remove(entry);
                                }
                            }

                            // Set the IsCaller flag to false
                            currentUser.IsCaller = false;
                        }
                    }
                    #endregion
                    #region Changing from "Administrator" to "StandardUser"
                    else
                    {
                        // Set the IsCaller flag to true
                        currentUser.IsCaller = true;
                    }
                    #endregion
                }

                #region Update the role
                // Remove the user's current role
                string roleName = UserManager.GetRoles(givenUser.Id).FirstOrDefault();
                UserManager.RemoveFromRole(givenUser.Id, roleName);

                // Add the new role to the user
                UserManager.AddToRole(givenUser.Id, model.Role);

                // Save the changes
                UserManager.Update(givenUser);
                #endregion

                // Save the changes to the database
                unitOfWork.Complete();

                // Regenerate the Call List since we are adding / removing a Caller
                GenerateCallList();

                // Return this view
                return(RedirectToAction("ManageAllUsers"));
            }

            // If we got this far, the model state wasn't valid - redirect to the Dashboard
            return(RedirectToAction("Dashboard", "Admins"));
        }
예제 #6
0
        public async Task <IActionResult> ChangeRoleAsync(ChangeUserRoleModel model)
        {
            await _unitOfWork.UserRepository.ChangeUserRoleAsync(model.UserId, model.RoleId);

            return(Ok());
        }