public IActionResult EditRole(ChangingRoleModel model) { var result = this.adminService.ChangeRole(model).Result; if (result == IdentityResult.Success) { return(RedirectToAction("Users")); } return(RedirectToAction("Error", "Home")); }
public IActionResult EditRole(ChangingRoleModel inputModel) { var result = this.AdminService.ChangeRole(inputModel).Result; if (result == IdentityResult.Success) { return(RedirectToAction(nameof(AllUsers))); } return(RedirectToAction("Error", "Home")); }
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; }
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()); }
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); }