Exemplo n.º 1
0
        public async Task <IActionResult> EditUser(string id)
        {
            User user = await _userManager.FindByIdAsync(id);

            IEnumerable <string> allRoles  = _roleManager.GetAllRoles();
            IList <string>       userRoles = await _userManager.GetRolesAsync(user);

            Dictionary <string, bool> dictionary = new Dictionary <string, bool>();

            foreach (string role in allRoles)
            {
                if (userRoles.Contains(role))
                {
                    dictionary.Add(role, true);
                }
                else
                {
                    dictionary.Add(role, false);
                }
            }
            EditUserViewModel model = new EditUserViewModel
            {
                Email     = user.Email,
                FirstName = user.FirstName,
                Id        = user.Id,
                LastName  = user.LastName,
                Status    = user.Status,
                UserName  = user.UserName,
                Roles     = dictionary
            };

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> EditRole(string roleid, string userid)
        {
            if (ModelState.IsValid)
            {
                IdentityRole role = await _roleManager.FindByIdAsync(roleid);

                User user = await _userManager.FindByIdAsync(userid);

                IdentityResult result;
                if (await _userManager.IsInRoleAsync(user, role.Name))
                {
                    result = await _userManager.RemoveFromRoleAsync(user, role.Name);
                }
                else
                {
                    result = await _userManager.AddToRoleAsync(user, role.Name);
                }

                if (result.Succeeded)
                {
                    return(RedirectToAction(nameof(EditRole), new { id = role.Id }));
                }
                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError(error.Code, error.Description);
                }
            }
            IdentityRole role1 = await _roleManager.FindByIdAsync(roleid);

            IEnumerable <User> members = await _userManager.GetUsersInRoleAsync(role1.Name);

            RoleEditViewModel model = new RoleEditViewModel()
            {
                Members    = members,
                NonMembers = _userManager.Users.Except(members),
                Role       = role1
            };

            return(View(model));
        }