Added GetRoles to IIdentityUserAppService

pull/112/head
Halil İbrahim Kalkan 8 years ago
parent 0d88992089
commit ecd03af4c7

@ -7,6 +7,6 @@ namespace Volo.Abp.Identity
{
public interface IIdentityUserAppService : IAsyncCrudAppService<IdentityUserDto, Guid, PagedAndSortedResultRequestDto, IdentityUserCreateDto, IdentityUserUpdateDto>
{
Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id);
}
}

@ -2,7 +2,7 @@
namespace Volo.Abp.Identity
{
public class IdentityUserRoleInfoDto : EntityDto
public class IdentityRoleDto : EntityDto
{
public string Name { get; set; }
}

@ -7,6 +7,7 @@ namespace Volo.Abp.Identity
public AbpIdentityApplicationModuleAutoMapperProfile()
{
CreateMap<IdentityUser, IdentityUserDto>();
CreateMap<IdentityRole, IdentityRoleDto>();
}
}
}

@ -67,6 +67,14 @@ namespace Volo.Abp.Identity
await _userManager.DeleteAsync(user);
}
public async Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id)
{
var roles = await _userRepository.GetRolesAsync(id);
return new ListResultDto<IdentityRoleDto>(
ObjectMapper.Map<List<IdentityRole>, List<IdentityRoleDto>>(roles)
);
}
private async Task UpdateUserByInput(IdentityUser user, IdentityUserCreateOrUpdateDtoBase input)
{
await _userManager.SetEmailAsync(user, input.Email);

@ -82,5 +82,14 @@ namespace Volo.Abp.Identity
{
return await this.OrderBy(sorting ?? nameof(IdentityUser.UserName)).PageBy(skipCount, maxResultCount).ToListAsync();
}
public async Task<List<IdentityRole>> GetRolesAsync(Guid userId)
{
var query = from userRole in DbContext.UserRoles
join role in DbContext.Roles on userRole.RoleId equals role.Id
select role;
return await query.ToListAsync();
}
}
}

@ -1,4 +1,5 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
@ -8,6 +9,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Volo.Abp.Identity")]
[assembly: AssemblyTrademark("")]
[assembly: InternalsVisibleTo("Volo.Abp.Identity.Application.Tests")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from

@ -25,5 +25,7 @@ namespace Volo.Abp.Identity
Task<IList<IdentityUser>> GetListByNormalizedRoleNameAsync(string normalizedRoleName, CancellationToken cancellationToken);
Task<List<IdentityUser>> GetListAsync(string sorting, int maxResultCount, int skipCount);
Task<List<IdentityRole>> GetRolesAsync(Guid userId);
}
}

@ -9,6 +9,10 @@ namespace Volo.Abp.Identity
private readonly IIdentityUserRepository _userRepository;
private readonly IIdentityRoleRepository _roleRepository;
private IdentityRole _adminRole;
private IdentityRole _moderator;
private IdentityRole _supporterRole;
public AbpIdentityTestDataBuilder(
IGuidGenerator guidGenerator,
IIdentityUserRepository userRepository,
@ -27,14 +31,20 @@ namespace Volo.Abp.Identity
private void AddRoles()
{
_roleRepository.Insert(new IdentityRole(_guidGenerator.Create(), "admin"));
_roleRepository.Insert(new IdentityRole(_guidGenerator.Create(), "moderator"));
_roleRepository.Insert(new IdentityRole(_guidGenerator.Create(), "supporter"));
_adminRole = new IdentityRole(_guidGenerator.Create(), "admin");
_moderator = new IdentityRole(_guidGenerator.Create(), "moderator");
_supporterRole = new IdentityRole(_guidGenerator.Create(), "supporter");
_roleRepository.Insert(_adminRole);
_roleRepository.Insert(_moderator);
_roleRepository.Insert(_supporterRole);
}
private void AddUsers()
{
_userRepository.Insert(new IdentityUser(_guidGenerator.Create(), "john.nash"));
var john = new IdentityUser(_guidGenerator.Create(), "john.nash");
john.Roles.Add(new IdentityUserRole(_guidGenerator.Create(), john.Id, _moderator.Id));
_userRepository.Insert(john);
}
}
}

@ -140,6 +140,23 @@ namespace Volo.Abp.Identity
(await FindUserAsync("john.nash")).ShouldBeNull();
}
[Fact]
public async Task GetRolesAsync()
{
//Arrange
var johnNash = await GetUserAsync("john.nash");
//Act
var result = await _identityUserAppService.GetRolesAsync(johnNash.Id);
//Assert
result.Items.Count.ShouldBe(1);
result.Items[0].Name.ShouldBe("moderator");
}
private async Task<IdentityUser> GetUserAsync(string userName)
{
return (await _userRepository.GetListAsync()).First(u => u.UserName == userName);

Loading…
Cancel
Save