Пример #1
0
        private void Can_Edit_Other_Users_Roles()
        {
            SetFakeHttpContext("Tests");
            IdentityUser          user1 = new IdentityUser("user1");
            IdentityUser          user2 = new IdentityUser("user2");
            IdentityUser          user3 = new IdentityUser("user3");
            IdentityRole          role  = new IdentityRole("Admin");
            RoleModificationМodel model = new RoleModificationМodel()
            {
                IdsToAdd    = new string[] { user2.Id },
                IdsToDelete = new string[] { user1.Id },
                RoleName    = "Admin"
            };

            _mockUserManager.Setup(m => m.FindByIdAsync(user1.Id)).ReturnsAsync(user1);
            _mockUserManager.Setup(m => m.FindByIdAsync(user2.Id)).ReturnsAsync(user2);
            _mockUserManager.Setup(m => m.FindByIdAsync(user3.Id)).ReturnsAsync(user3);
            _mockUserManager.Setup(m => m.AddToRoleAsync(It.IsAny <IdentityUser>(), model.RoleName)).ReturnsAsync(IdentityResult.Success);
            _mockUserManager.Setup(m => m.RemoveFromRoleAsync(It.IsAny <IdentityUser>(), model.RoleName)).ReturnsAsync(IdentityResult.Success);

            IActionResult result = _roleAdminController.Edit(model).Result;

            _mockUserManager.Verify(m => m.AddToRoleAsync(user2, model.RoleName));
            _mockUserManager.Verify(m => m.RemoveFromRoleAsync(user1, model.RoleName));
        }
Пример #2
0
        public async Task <IActionResult> Edit(RoleModificationМodel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    AppUser user = await _userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await _userManager.AddToRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    AppUser user = await _userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await _userManager.RemoveFromRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
            }
            if (ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(await Edit(model.RoleId));
            }
        }
Пример #3
0
        public async Task <IActionResult> Edit(RoleModificationМodel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    IdentityUser user = await _userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await _userManager.AddToRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    IdentityUser user = await _userManager.FindByIdAsync(userId);

                    if (user != null && user.UserName != HttpContext.User.Identity.Name)
                    {
                        result = await _userManager.RemoveFromRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
            }
            return(await Edit());
        }