Beispiel #1
0
        public async Task <IActionResult> RemoveUserFromRole(EditUserRolesViewModel model)
        {
            var user = await this.userManager.FindByIdAsync(model.UserId);

            var roles = await this.userManager.GetRolesAsync(user);

            var allRoles = this.roleManager.Roles;

            var userRoles = new List <RolesDropDownViewModel>();

            foreach (var role in allRoles)
            {
                bool isInRole = roles.Contains(role.Name);

                if (!isInRole)
                {
                    continue;
                }
                else
                {
                    var roleModel = new RolesDropDownViewModel
                    {
                        Id   = role.Id,
                        Name = role.Name,
                    };
                    userRoles.Add(roleModel);
                }
            }

            var viewModel = new EditUserRolesViewModel
            {
                UserName = user.UserName,
                UserId   = user.Id,
                Roles    = userRoles,
            };

            var roleName = allRoles
                           .Where(n => n.Id == model.RoleId)
                           .Select(x => x.Name)
                           .FirstOrDefault();

            var result = await this.userManager.RemoveFromRoleAsync(user, roleName);

            if (result.Succeeded)
            {
                return(this.RedirectToAction("AllUsers"));
            }
            else
            {
                return(this.View(viewModel));
            }
        }
        public ActionResult CreateRoleSelectList(string id)
        {
            UserContext ctx = new UserContext();

            var user = ctx.Users.Find(id);

            RolesDropDownViewModel vm = new RolesDropDownViewModel();

            vm.SelectedRoleId = user.Roles.FirstOrDefault().RoleId;
            vm.Roles          = new SelectList(ctx.Roles, "Id", "Name");

            return(PartialView("_ShowRoles", vm));
        }
Beispiel #3
0
        public async Task <IActionResult> RemoveUserFromRole(string id)
        {
            var user = await this.userManager.FindByIdAsync(id);

            var roles = await this.userManager.GetRolesAsync(user);

            var allRoles  = this.roleManager.Roles;
            var userRoles = new List <RolesDropDownViewModel>();

            foreach (var role in allRoles)
            {
                bool isInRole = roles.Contains(role.Name);

                if (!isInRole)
                {
                    continue;
                }
                else
                {
                    var roleModel = new RolesDropDownViewModel
                    {
                        Id   = role.Id,
                        Name = role.Name,
                    };
                    userRoles.Add(roleModel);
                }
            }

            var viewModel = new EditUserRolesViewModel
            {
                UserName = user.UserName,
                UserId   = user.Id,
                Roles    = userRoles,
            };

            return(this.View(viewModel));
        }