public IActionResult AddUser(string returnUrl = null) { // Initialise groups var vm = new AddUserViewModel { AvailableGroups = CompanyGroups.GetGroups().ToList() }; ViewData["ReturnUrl"] = returnUrl; return(View(vm)); }
private async Task <EditUserViewModel> BuildEditUserViewModel(ApplicationUser user) { IList <string> Roles = await _userManager.GetRolesAsync(user); Roles = Roles.Where(r => (r == Permission.User.ToString()) || (r == Permission.Admin.ToString())).ToList(); Permission currentRole = Permission.User; // The user doesn't have a role, shouldn't be possible but set them to default user anyways. if (Roles.Count == 0) { currentRole = Permission.User; } // Must check what permission the user currently has. else { var permissionValues = Enum.GetNames(typeof(Permission)); foreach (var role in Roles) { // Convert the role stored in the database into a type of permission enum. if (permissionValues.Contains(role)) { currentRole = (Permission)Enum.Parse(typeof(Permission), role); } } } // Need to get the users' groups from the claims table. IList <Claim> userClaims = await _userManager.GetClaimsAsync(user); // Filter claims to just groups the user belongs to? IList <string> groups = userClaims.Where(c => c.Type == "Group").Select(c => c.Value).ToList(); // Construct the model, what the user will be able to see and modify. var vm = new EditUserViewModel() { Id = user.Id, PreviousGroups = new List <string>(), AvailableGroups = CompanyGroups.GetGroups().ToList(), Username = user.UserName, FirstName = user.FirstName, LastName = user.LastName, IsLocked = user.IsLocked, UserRole = currentRole, SelectedGroups = groups, }; return(vm); }