public async Task GetUsersInRoleInTenant_Test() { CreateTenantDto createTenantDto = new CreateTenantDto() { TenancyName = "TestTenant", Name = "TestTenant", AdminPhoneNumber = "13851400000", IsActive = true }; var tenantDto = await _tenantAppService.CreateAsync(createTenantDto); var createRoleDto = new CreateRoleDto() { Name = "RoleTest", DisplayName = "Test role", Description = "Role for test", GrantedPermissions = new List <string>() { PermissionNames.Pages_Roles } }; var roleDto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto); CreateUserDto createUser1Dto = new CreateUserDto() { UserName = "******", Password = User.DefaultPassword, Name = "John", Surname = "Smith", PhoneNumber = "13851400001", IsActive = true }; var user1Dto = await _omUserAppService.CreateUserInTenantAsync(tenantDto.Id, createUser1Dto); SetRoleUserDto setRoleUserDto = new SetRoleUserDto() { UserId = user1Dto.Id, RoleId = roleDto.Id }; await _omRoleAppService.AddUserToRoleInTenantAsync(tenantDto.Id, setRoleUserDto); CreateUserDto createUser2Dto = new CreateUserDto() { UserName = "******", Password = User.DefaultPassword, Name = "Mike", Surname = "Smith", PhoneNumber = "13851400002", IsActive = true, RoleNames = new[] { "RoleTest" } }; var user2Dto = await _omUserAppService.CreateUserInTenantAsync(tenantDto.Id, createUser2Dto); var roleUsers = await _omRoleAppService.GetUsersInRoleInTenantAsync(tenantDto.Id, new EntityDto <int>(roleDto.Id)); roleUsers.Count.ShouldBe(2); }
/// <summary> /// 对当前租户的指定角色添加一个用户 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task AddUserToRoleAsync(SetRoleUserDto input) { try { var user = await _userManager.GetUserByIdAsync(input.UserId); var role = await _roleManager.GetRoleByIdAsync(input.RoleId); await _userManager.AddToRoleAsync(user, role.Name); } catch (Exception exception) { throw new UserFriendlyException(exception.Message); } }
public async Task RemoveUserFromRole_Test() { var createRoleDto = new CreateRoleDto() { Name = "RoleTest", DisplayName = "Test role", Description = "Role for test", GrantedPermissions = new List <string>() { PermissionNames.Pages_Roles } }; var roleDto = await _roleAppService.CreateAsync(createRoleDto); CreateUserDto createUserDto = new CreateUserDto() { UserName = "******", Password = User.DefaultPassword, Name = "John", Surname = "Smith", PhoneNumber = "13851400000", IsActive = true }; var userDto = await _userAppService.CreateAsync(createUserDto); var getUser1Dto = await _userAppService.GetAsync(new EntityDto <long>(userDto.Id)); getUser1Dto.RoleNames.Length.ShouldBe(0); SetRoleUserDto setRoleUserDto = new SetRoleUserDto() { UserId = userDto.Id, RoleId = roleDto.Id }; await _roleAppService.AddUserToRoleAsync(setRoleUserDto); var getUser2Dto = await _userAppService.GetAsync(new EntityDto <long>(userDto.Id)); getUser2Dto.RoleNames.Length.ShouldBe(1); await _roleAppService.RemoveUserFromRoleAsync(setRoleUserDto); var getUser3Dto = await _userAppService.GetAsync(new EntityDto <long>(userDto.Id)); getUser3Dto.RoleNames.Length.ShouldBe(0); }
/// <summary> /// 运维专用:删除特定租户下指定角色中的某一用户 /// </summary> /// <param name="tenantId"></param> /// <param name="input"></param> /// <returns></returns> public async Task RemoveUserFromRoleInTenantAsync(int tenantId, SetRoleUserDto input) { using (CurrentUnitOfWork.SetTenantId(tenantId)) { try { var user = await _userManager.GetUserByIdAsync(input.UserId); var role = await _roleManager.GetRoleByIdAsync(input.RoleId); await _userManager.RemoveFromRoleAsync(user, role.Name); } catch (Exception exception) { throw new UserFriendlyException(exception.Message); } } }