コード例 #1
0
        public IActionResult EditRole(ChangingRoleModel model)
        {
            var result = this.adminService.ChangeRole(model).Result;

            if (result == IdentityResult.Success)
            {
                return(RedirectToAction("Users"));
            }

            return(RedirectToAction("Error", "Home"));
        }
コード例 #2
0
        public IActionResult EditRole(ChangingRoleModel inputModel)
        {
            var result = this.AdminService.ChangeRole(inputModel).Result;

            if (result == IdentityResult.Success)
            {
                return(RedirectToAction(nameof(AllUsers)));
            }

            return(RedirectToAction("Error", "Home"));
        }
コード例 #3
0
        public async Task<IdentityResult> ChangeRole(ChangingRoleModel model)
        {
            string newRole = model.NewRole;
            var user = this.UserManager.FindByIdAsync(model.Id).Result;
            var currentRole = await this.UserManager.GetRolesAsync(user);
            IdentityResult result = null;

            result = await this.UserManager.RemoveFromRoleAsync(user, currentRole.First());
            result = await this.UserManager.AddToRoleAsync(user, newRole);

            return result;
        }
コード例 #4
0
        public void ChangeRole_Succeed_Role()
        {
            //arrange
            var roles = new[]
            {
                "admin",
                "superUser",
                "moderator",
                "user"
            };

            foreach (var currentRolename in roles)
            {
                var role = new IdentityRole
                {
                    Name = currentRolename
                };
                this.roleManager.CreateAsync(role).GetAwaiter();
            }

            var user = new FanFictionUser
            {
                Id       = "userId",
                Nickname = "AnotherUser",
                UserName = "******",
            };

            this.userManager.CreateAsync(user).GetAwaiter();
            this.Context.SaveChanges();

            //act
            string newRole = roles[2];
            var    model   = new ChangingRoleModel
            {
                Id       = user.Id,
                AppRoles = roles,
                NewRole  = newRole,
                Nickname = user.Nickname,
                Role     = GlobalConstants.DefaultRole
            };
            var methodResult = this.adminService.ChangeRole(model);

            //assert
            methodResult.Should().Equals(IdentityResult.Failed());
        }
コード例 #5
0
        public void ChangeRole_Should_Success()
        {
            var roles = new[]
            {
                "admin",
                "user"
            };

            foreach (var currentRolename in roles)
            {
                var role = new IdentityRole
                {
                    Name = currentRolename
                };
                this.roleService.CreateAsync(role).GetAwaiter();
            }

            var user = new BookCreatorUser()
            {
                UserName = "******",
                Name     = "Goshko Petkov"
            };

            this.userManager.CreateAsync(user).GetAwaiter();
            this.Context.SaveChanges();

            var newRole = roles[1];
            var model   = new ChangingRoleModel
            {
                Id       = user.Id,
                AppRoles = roles,
                NewRole  = newRole,
                Name     = user.Name,
                Role     = GlobalConstants.DefaultRole
            };

            var result = this.adminService.ChangeRole(model);

            result.Should().Equals(IdentityResult.Success);
        }