Browse Source

Created IdentityRoleAppService

pull/113/head
Halil İbrahim Kalkan 9 years ago
parent
commit
f0b9e500ee
  1. 11
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityRoleAppService.cs
  2. 2
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserAppService.cs
  3. 7
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleCreateDto.cs
  4. 7
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleCreateOrUpdateDtoBase.cs
  5. 7
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleUpdateDto.cs
  6. 2
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityUserUpdateRolesDto.cs
  7. 68
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityRoleAppService.cs
  8. 10
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs
  9. 8
      src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EfCoreIdentityRoleRepository.cs
  10. 3
      src/Volo.Abp.Identity/Volo/Abp/Identity/IIdentityRoleRepository.cs
  11. 16
      src/Volo.Abp.Identity/Volo/Abp/Identity/IdentityRoleManager.cs
  12. 125
      test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityRoleAppService_Tests.cs
  13. 20
      test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs

11
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityRoleAppService.cs

@ -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>
{
}
}

2
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserAppService.cs

@ -9,6 +9,6 @@ namespace Volo.Abp.Identity
{ {
Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id); Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id);
Task UpdateRolesAsync(Guid id, UpdateIdentityUserRolesDto input); Task UpdateRolesAsync(Guid id, IdentityUserUpdateRolesDto input);
} }
} }

7
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleCreateDto.cs

@ -0,0 +1,7 @@
namespace Volo.Abp.Identity
{
public class IdentityRoleCreateDto : IdentityRoleCreateOrUpdateDtoBase
{
}
}

7
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleCreateOrUpdateDtoBase.cs

@ -0,0 +1,7 @@
namespace Volo.Abp.Identity
{
public class IdentityRoleCreateOrUpdateDtoBase
{
public string Name { get; set; }
}
}

7
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityRoleUpdateDto.cs

@ -0,0 +1,7 @@
namespace Volo.Abp.Identity
{
public class IdentityRoleUpdateDto : IdentityRoleCreateOrUpdateDtoBase
{
}
}

2
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/UpdateIdentityUserRolesDto.cs → src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IdentityUserUpdateRolesDto.cs

@ -2,7 +2,7 @@
namespace Volo.Abp.Identity namespace Volo.Abp.Identity
{ {
public class UpdateIdentityUserRolesDto public class IdentityUserUpdateRolesDto
{ {
[Required] [Required]
public string[] RoleNames { get; set; } public string[] RoleNames { get; set; }

68
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityRoleAppService.cs

@ -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);
}
}
}

10
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs

@ -28,12 +28,12 @@ namespace Volo.Abp.Identity
public async Task<PagedResultDto<IdentityUserDto>> GetListAsync(PagedAndSortedResultRequestDto input) public async Task<PagedResultDto<IdentityUserDto>> GetListAsync(PagedAndSortedResultRequestDto input)
{ {
var userCount = (int)await _userRepository.GetCountAsync(); var count = (int)await _userRepository.GetCountAsync();
var users = await _userRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount); var list = await _userRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount);
return new PagedResultDto<IdentityUserDto>( return new PagedResultDto<IdentityUserDto>(
userCount, count,
ObjectMapper.Map<List<IdentityUser>, List<IdentityUserDto>>(users) ObjectMapper.Map<List<IdentityUser>, List<IdentityUserDto>>(list)
); );
} }
@ -75,7 +75,7 @@ namespace Volo.Abp.Identity
); );
} }
public async Task UpdateRolesAsync(Guid id, UpdateIdentityUserRolesDto input) public async Task UpdateRolesAsync(Guid id, IdentityUserUpdateRolesDto input)
{ {
var user = await _userManager.GetByIdAsync(id); var user = await _userManager.GetByIdAsync(id);
await _userManager.SetRolesAsync(user, input.RoleNames); await _userManager.SetRolesAsync(user, input.RoleNames);

8
src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EfCoreIdentityRoleRepository.cs

@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -18,5 +21,10 @@ namespace Volo.Abp.Identity
{ {
return DbSet.FirstOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, cancellationToken); return DbSet.FirstOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, cancellationToken);
} }
public async Task<List<IdentityRole>> GetListAsync(string sorting, int maxResultCount, int skipCount)
{
return await this.OrderBy(sorting ?? nameof(IdentityRole.Name)).PageBy(skipCount, maxResultCount).ToListAsync();
}
} }
} }

3
src/Volo.Abp.Identity/Volo/Abp/Identity/IIdentityRoleRepository.cs

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
@ -7,5 +8,7 @@ namespace Volo.Abp.Identity
public interface IIdentityRoleRepository : IRepository<IdentityRole> public interface IIdentityRoleRepository : IRepository<IdentityRole>
{ {
Task<IdentityRole> FindByNormalizedNameAsync(string normalizedRoleName, CancellationToken cancellationToken); Task<IdentityRole> FindByNormalizedNameAsync(string normalizedRoleName, CancellationToken cancellationToken);
Task<List<IdentityRole>> GetListAsync(string sorting, int maxResultCount, int skipCount);
} }
} }

16
src/Volo.Abp.Identity/Volo/Abp/Identity/IdentityRoleManager.cs

@ -1,6 +1,9 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Services; using Volo.Abp.Domain.Services;
namespace Volo.Abp.Identity namespace Volo.Abp.Identity
@ -21,5 +24,16 @@ namespace Volo.Abp.Identity
logger) logger)
{ {
} }
public async Task<IdentityRole> GetByIdAsync(Guid id)
{
var role = await Store.FindByIdAsync(id.ToString(), CancellationToken);
if (role == null)
{
throw new EntityNotFoundException(typeof(IdentityRole), id);
}
return role;
}
} }
} }

125
test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityRoleAppService_Tests.cs

@ -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);
}
}
}

20
test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs

@ -10,12 +10,12 @@ namespace Volo.Abp.Identity
{ {
public class IdentityUserAppService_Tests : AbpIdentityApplicationTestBase public class IdentityUserAppService_Tests : AbpIdentityApplicationTestBase
{ {
private readonly IIdentityUserAppService _identityUserAppService; private readonly IIdentityUserAppService _userAppService;
private readonly IIdentityUserRepository _userRepository; private readonly IIdentityUserRepository _userRepository;
public IdentityUserAppService_Tests() public IdentityUserAppService_Tests()
{ {
_identityUserAppService = ServiceProvider.GetRequiredService<IIdentityUserAppService>(); _userAppService = ServiceProvider.GetRequiredService<IIdentityUserAppService>();
_userRepository = ServiceProvider.GetRequiredService<IIdentityUserRepository>(); _userRepository = ServiceProvider.GetRequiredService<IIdentityUserRepository>();
} }
@ -28,7 +28,7 @@ namespace Volo.Abp.Identity
//Act //Act
var result = await _identityUserAppService.GetAsync(johnNash.Id); var result = await _userAppService.GetAsync(johnNash.Id);
//Assert //Assert
@ -44,7 +44,7 @@ namespace Volo.Abp.Identity
{ {
//Act //Act
var result = await _identityUserAppService.GetListAsync(new PagedAndSortedResultRequestDto()); var result = await _userAppService.GetListAsync(new PagedAndSortedResultRequestDto());
//Assert //Assert
@ -69,7 +69,7 @@ namespace Volo.Abp.Identity
//Act //Act
var result = await _identityUserAppService.CreateAsync(input); var result = await _userAppService.CreateAsync(input);
//Assert //Assert
@ -105,7 +105,7 @@ namespace Volo.Abp.Identity
//Act //Act
var result = await _identityUserAppService.UpdateAsync(johnNash.Id, input); var result = await _userAppService.UpdateAsync(johnNash.Id, input);
//Assert //Assert
@ -132,7 +132,7 @@ namespace Volo.Abp.Identity
//Act //Act
await _identityUserAppService.DeleteAsync(johnNash.Id); await _userAppService.DeleteAsync(johnNash.Id);
//Assert //Assert
@ -148,7 +148,7 @@ namespace Volo.Abp.Identity
//Act //Act
var result = await _identityUserAppService.GetRolesAsync(johnNash.Id); var result = await _userAppService.GetRolesAsync(johnNash.Id);
//Assert //Assert
@ -166,9 +166,9 @@ namespace Volo.Abp.Identity
//Act //Act
await _identityUserAppService.UpdateRolesAsync( await _userAppService.UpdateRolesAsync(
johnNash.Id, johnNash.Id,
new UpdateIdentityUserRolesDto new IdentityUserUpdateRolesDto
{ {
RoleNames = new[] {"moderator", "admin"} RoleNames = new[] {"moderator", "admin"}
} }

Loading…
Cancel
Save