mirror of https://github.com/abpframework/abp.git
13 changed files with 268 additions and 18 deletions
@ -0,0 +1,11 @@ |
|||
using System; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Application.Services; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public interface IIdentityRoleAppService : IAsyncCrudAppService<IdentityRoleDto, Guid, PagedAndSortedResultRequestDto, IdentityRoleCreateDto, IdentityRoleUpdateDto> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class IdentityRoleCreateDto : IdentityRoleCreateOrUpdateDtoBase |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class IdentityRoleCreateOrUpdateDtoBase |
|||
{ |
|||
public string Name { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class IdentityRoleUpdateDto : IdentityRoleCreateOrUpdateDtoBase |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,68 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Application.Services; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class IdentityRoleAppService : ApplicationService, IIdentityRoleAppService |
|||
{ |
|||
private readonly IdentityRoleManager _roleManager; |
|||
private readonly IIdentityRoleRepository _roleRepository; |
|||
|
|||
public IdentityRoleAppService( |
|||
IdentityRoleManager roleManager, |
|||
IIdentityRoleRepository roleRepository) |
|||
{ |
|||
_roleManager = roleManager; |
|||
_roleRepository = roleRepository; |
|||
} |
|||
|
|||
public async Task<IdentityRoleDto> GetAsync(Guid id) |
|||
{ |
|||
return ObjectMapper.Map<IdentityRole, IdentityRoleDto>( |
|||
await _roleManager.GetByIdAsync(id) |
|||
); |
|||
} |
|||
|
|||
public async Task<PagedResultDto<IdentityRoleDto>> GetListAsync(PagedAndSortedResultRequestDto input) |
|||
{ |
|||
var count = (int)await _roleRepository.GetCountAsync(); |
|||
var list = await _roleRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount); |
|||
|
|||
return new PagedResultDto<IdentityRoleDto>( |
|||
count, |
|||
ObjectMapper.Map<List<IdentityRole>, List<IdentityRoleDto>>(list) |
|||
); |
|||
} |
|||
|
|||
public async Task<IdentityRoleDto> CreateAsync(IdentityRoleCreateDto input) |
|||
{ |
|||
var role = new IdentityRole(GuidGenerator.Create(), input.Name); |
|||
|
|||
await _roleManager.CreateAsync(role); |
|||
await CurrentUnitOfWork.SaveChangesAsync(); |
|||
|
|||
return ObjectMapper.Map<IdentityRole, IdentityRoleDto>(role); |
|||
} |
|||
|
|||
public async Task<IdentityRoleDto> UpdateAsync(Guid id, IdentityRoleUpdateDto input) |
|||
{ |
|||
var role = await _roleManager.GetByIdAsync(id); |
|||
|
|||
await _roleManager.SetRoleNameAsync(role, input.Name); |
|||
|
|||
await _roleManager.UpdateAsync(role); |
|||
await CurrentUnitOfWork.SaveChangesAsync(); |
|||
|
|||
return ObjectMapper.Map<IdentityRole, IdentityRoleDto>(role); |
|||
} |
|||
|
|||
public async Task DeleteAsync(Guid id) |
|||
{ |
|||
var role = await _roleManager.GetByIdAsync(id); |
|||
await _roleManager.DeleteAsync(role); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,125 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Xunit; |
|||
using Shouldly; |
|||
using Volo.Abp.Application.Dtos; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class IdentityRoleAppService_Tests: AbpIdentityApplicationTestBase |
|||
{ |
|||
private readonly IIdentityRoleAppService _roleAppService; |
|||
private readonly IIdentityRoleRepository _roleRepository; |
|||
|
|||
public IdentityRoleAppService_Tests() |
|||
{ |
|||
_roleAppService = ServiceProvider.GetRequiredService<IIdentityRoleAppService>(); |
|||
_roleRepository = ServiceProvider.GetRequiredService<IIdentityRoleRepository>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetAsync() |
|||
{ |
|||
//Arrange
|
|||
|
|||
var moderator = await GetRoleAsync("moderator"); |
|||
|
|||
//Act
|
|||
|
|||
var result = await _roleAppService.GetAsync(moderator.Id); |
|||
|
|||
//Assert
|
|||
|
|||
result.Id.ShouldBe(moderator.Id); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetListAsync() |
|||
{ |
|||
//Act
|
|||
|
|||
var result = await _roleAppService.GetListAsync(new PagedAndSortedResultRequestDto()); |
|||
|
|||
//Assert
|
|||
|
|||
result.TotalCount.ShouldBeGreaterThan(0); |
|||
result.Items.Count.ShouldBeGreaterThan(0); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task CreateAsync() |
|||
{ |
|||
//Arrange
|
|||
|
|||
var input = new IdentityRoleCreateDto |
|||
{ |
|||
Name = Guid.NewGuid().ToString("N").Left(8) |
|||
}; |
|||
|
|||
//Act
|
|||
|
|||
var result = await _roleAppService.CreateAsync(input); |
|||
|
|||
//Assert
|
|||
|
|||
result.Id.ShouldNotBe(Guid.Empty); |
|||
result.Name.ShouldBe(input.Name); |
|||
|
|||
var role = await _roleRepository.GetAsync(result.Id); |
|||
role.Name.ShouldBe(input.Name); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task UpdateAsync() |
|||
{ |
|||
//Arrange
|
|||
|
|||
var moderator = await GetRoleAsync("moderator"); |
|||
|
|||
var input = new IdentityRoleUpdateDto |
|||
{ |
|||
Name = Guid.NewGuid().ToString("N").Left(8) |
|||
}; |
|||
|
|||
//Act
|
|||
|
|||
var result = await _roleAppService.UpdateAsync(moderator.Id, input); |
|||
|
|||
//Assert
|
|||
|
|||
result.Id.ShouldBe(moderator.Id); |
|||
result.Name.ShouldBe(input.Name); |
|||
|
|||
var updatedRole = await _roleRepository.GetAsync(moderator.Id); |
|||
updatedRole.Name.ShouldBe(input.Name); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task DeleteAsync() |
|||
{ |
|||
//Arrange
|
|||
|
|||
var moderator = await GetRoleAsync("moderator"); |
|||
|
|||
//Act
|
|||
|
|||
await _roleAppService.DeleteAsync(moderator.Id); |
|||
|
|||
//Assert
|
|||
|
|||
(await FindRoleAsync("moderator")).ShouldBeNull(); |
|||
} |
|||
|
|||
private async Task<IdentityRole> GetRoleAsync(string roleName) |
|||
{ |
|||
return (await _roleRepository.GetListAsync()).First(u => u.Name == roleName); |
|||
} |
|||
|
|||
private async Task<IdentityRole> FindRoleAsync(string roleName) |
|||
{ |
|||
return (await _roleRepository.GetListAsync()).FirstOrDefault(u => u.Name == roleName); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue