public ActionResult Index(int Page = 1, int PageSize = 25, string FilterBy = "all", string SearchTerm = null) { logger.Debug("User " + WebSecurity.GetUserId(User.Identity.Name) + " \"" + User.Identity.Name + "\" visited AccountsManagement/Membership/Index"); ManageUsersViewModel viewModel = new ManageUsersViewModel(); viewModel.FilterBy = FilterBy; viewModel.SearchTerm = SearchTerm; // New search term if (System.Web.HttpContext.Current.Request.HttpMethod == "POST") { Page = 1; } if (PageSize == 0) PageSize = 25; viewModel.PageSize = PageSize; if (!string.IsNullOrEmpty(FilterBy)) { IQueryable<UserProfile> users = repository.Users; if (!Roles.IsUserInRole("Administrator")) { users = users.Where(u => u.CreatedByUserID == WebSecurity.CurrentUserId); } if (FilterBy == "lastaccess") { viewModel.PaginatedUserList = users .Where(u => u.UserId != 1) .OrderByDescending(u => u.LastAccessTime) .ToPaginatedList<UserProfile>(Page, PageSize); } else if (FilterBy == "all" || string.IsNullOrEmpty(SearchTerm)) { viewModel.PaginatedUserList = users .Where(u => u.UserId != 1) .OrderBy(u => u.UserId) .ToPaginatedList<UserProfile>(Page, PageSize); } else if (!string.IsNullOrEmpty(SearchTerm)) { if (FilterBy == "username") { viewModel.PaginatedUserList = users .Where(u => u.UserId != 1 && u.UserName.ToLower().IndexOf(SearchTerm.ToLower()) != -1) .OrderBy(u => u.UserId) .ToPaginatedList<UserProfile>(Page, PageSize); } else if (FilterBy == "email") { viewModel.PaginatedUserList = users .Where(u => u.UserId != 1 && u.Email.ToLower().IndexOf(SearchTerm.ToLower()) != -1) .OrderBy(u => u.UserId) .ToPaginatedList<UserProfile>(Page, PageSize); } } } return View(viewModel); }
/// <summary> /// Return two lists: /// 1) a list of Roles not granted to the user /// 2) a list of Roles granted to the user /// </summary> /// <param name="userName"></param> /// <returns></returns> public ActionResult GrantRolesToUsers(int UserID = -1, int Page = 1, int PageSize = 25, string FilterBy = "all", string SearchTerm = null) { logger.Debug("User " + WebSecurity.GetUserId(User.Identity.Name) + " \"" + User.Identity.Name + "\" visited AccountsManagement/Membership/GrantRolesToUser"); if (UserID == -1) { ManageUsersViewModel viewModel = new ManageUsersViewModel(); viewModel.FilterBy = FilterBy; viewModel.SearchTerm = SearchTerm; if (System.Web.HttpContext.Current.Request.HttpMethod == "POST") { Page = 1; } if (PageSize == 0) PageSize = 25; viewModel.PageSize = PageSize; if (!string.IsNullOrEmpty(FilterBy)) { IQueryable<UserProfile> users = repository.Users; if (!Roles.IsUserInRole("Administrator")) { users = users.Where(u => u.CreatedByUserID == WebSecurity.CurrentUserId); } if (FilterBy == "all" || string.IsNullOrEmpty(SearchTerm)) { viewModel.PaginatedUserList = users .Where(u => u.UserId != 1) .OrderBy(u => u.UserId) .ToPaginatedList<UserProfile>(Page, PageSize); } else if (!string.IsNullOrEmpty(SearchTerm)) { if (FilterBy == "username") { viewModel.PaginatedUserList = users .Where(u => u.UserId != 1 && u.UserName.ToLower().IndexOf(SearchTerm.ToLower()) != -1) .OrderBy(u => u.UserId) .ToPaginatedList<UserProfile>(Page, PageSize); } else if (FilterBy == "email") { viewModel.PaginatedUserList = users .Where(u => u.UserId != 1 && u.Email.ToLower().IndexOf(SearchTerm.ToLower()) != -1) .OrderBy(u => u.UserId) .ToPaginatedList<UserProfile>(Page, PageSize); } } } return View("UsersForRoles", viewModel); } if (UserID == 1) { logger.Warn("User " + WebSecurity.CurrentUserId + " \"" + User.Identity.Name + "\" try to change roles for Admin"); throw new HttpException(404, "User not found"); } UserProfile user = repository.Users.FirstOrDefault(u => u.UserId == UserID); if (user == null) { logger.Warn("User not found"); throw new HttpException(404, "User not found"); } GrantRolesToUserViewModel model = new GrantRolesToUserViewModel(); model.UserName = user.UserName; IEnumerable<string> availableRoles = Roles.GetAllRoles().Except(Roles.GetRolesForUser(user.UserName)); if (!Roles.IsUserInRole("Administrator")) availableRoles = availableRoles.Except(new string[] { "Administrator" }); IEnumerable<string> grantedRoles = Roles.GetRolesForUser(user.UserName); if (!Roles.IsUserInRole("Administrator")) grantedRoles = grantedRoles.Except(new string[] { "Administrator" }); model.AvailableRoles = new SelectList(availableRoles); model.GrantedRoles = (string.IsNullOrEmpty(user.UserName) ? new SelectList(new string[] { }) : new SelectList(grantedRoles)); return View(model); }