Browse Source

Add tests and fix mongodb repo.

pull/3732/head
Halil İbrahim Kalkan 6 years ago
parent
commit
a75459d480
  1. 2
      modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs
  2. 49
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserStore.cs
  3. 21
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs
  4. 3
      modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs
  5. 17
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs

2
modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs

@ -45,6 +45,8 @@ namespace Volo.Abp.Identity
[Authorize(IdentityPermissions.Users.Default)] [Authorize(IdentityPermissions.Users.Default)]
public virtual async Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id) public virtual async Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id)
{ {
//TODO: Should also include roles of the related OUs.
var roles = await UserRepository.GetRolesAsync(id); var roles = await UserRepository.GetRolesAsync(id);
return new ListResultDto<IdentityRoleDto>( return new ListResultDto<IdentityRoleDto>(

49
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserStore.cs

@ -53,6 +53,7 @@ namespace Volo.Abp.Identity
protected IIdentityRoleRepository RoleRepository { get; } protected IIdentityRoleRepository RoleRepository { get; }
protected IGuidGenerator GuidGenerator { get; } protected IGuidGenerator GuidGenerator { get; }
protected ILogger<IdentityRoleStore> Logger { get; } protected ILogger<IdentityRoleStore> Logger { get; }
protected ILookupNormalizer LookupNormalizer { get; }
protected IIdentityUserRepository UserRepository { get; } protected IIdentityUserRepository UserRepository { get; }
public IdentityUserStore( public IdentityUserStore(
@ -60,12 +61,14 @@ namespace Volo.Abp.Identity
IIdentityRoleRepository roleRepository, IIdentityRoleRepository roleRepository,
IGuidGenerator guidGenerator, IGuidGenerator guidGenerator,
ILogger<IdentityRoleStore> logger, ILogger<IdentityRoleStore> logger,
ILookupNormalizer lookupNormalizer,
IdentityErrorDescriber describer = null) IdentityErrorDescriber describer = null)
{ {
UserRepository = userRepository; UserRepository = userRepository;
RoleRepository = roleRepository; RoleRepository = roleRepository;
GuidGenerator = guidGenerator; GuidGenerator = guidGenerator;
Logger = logger; Logger = logger;
LookupNormalizer = lookupNormalizer;
ErrorDescriber = describer ?? new IdentityErrorDescriber(); ErrorDescriber = describer ?? new IdentityErrorDescriber();
} }
@ -313,13 +316,17 @@ namespace Volo.Abp.Identity
Check.NotNull(user, nameof(user)); Check.NotNull(user, nameof(user));
Check.NotNull(normalizedRoleName, nameof(normalizedRoleName)); Check.NotNull(normalizedRoleName, nameof(normalizedRoleName));
var role = await RoleRepository.FindByNormalizedNameAsync(normalizedRoleName, cancellationToken: cancellationToken); if (await IsInRoleAsync(user, normalizedRoleName, cancellationToken))
{
return;
}
var role = await RoleRepository.FindByNormalizedNameAsync(normalizedRoleName, cancellationToken: cancellationToken);
if (role == null) if (role == null)
{ {
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Role {0} does not exist!", normalizedRoleName)); throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Role {0} does not exist!", normalizedRoleName));
} }
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, cancellationToken); await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, cancellationToken);
user.AddRole(role.Id); user.AddRole(role.Id);
@ -337,11 +344,7 @@ namespace Volo.Abp.Identity
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
Check.NotNull(user, nameof(user)); Check.NotNull(user, nameof(user));
Check.NotNullOrWhiteSpace(normalizedRoleName, nameof(normalizedRoleName));
if (string.IsNullOrWhiteSpace(normalizedRoleName))
{
throw new ArgumentException(nameof(normalizedRoleName) + " can not be null or whitespace");
}
var role = await RoleRepository.FindByNormalizedNameAsync(normalizedRoleName, cancellationToken: cancellationToken); var role = await RoleRepository.FindByNormalizedNameAsync(normalizedRoleName, cancellationToken: cancellationToken);
if (role == null) if (role == null)
@ -366,8 +369,11 @@ namespace Volo.Abp.Identity
Check.NotNull(user, nameof(user)); Check.NotNull(user, nameof(user));
var userRoles = await UserRepository.GetRoleNamesAsync(user.Id, cancellationToken: cancellationToken); var userRoles = await UserRepository
var userOrganizationUnitRoles = await UserRepository.GetRoleNamesInOrganizationUnitAsync(user.Id, cancellationToken: cancellationToken); .GetRoleNamesAsync(user.Id, cancellationToken: cancellationToken);
var userOrganizationUnitRoles = await UserRepository
.GetRoleNamesInOrganizationUnitAsync(user.Id, cancellationToken: cancellationToken);
return userRoles.Union(userOrganizationUnitRoles).ToList(); return userRoles.Union(userOrganizationUnitRoles).ToList();
} }
@ -380,26 +386,21 @@ namespace Volo.Abp.Identity
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>A <see cref="Task{TResult}"/> containing a flag indicating if the specified user is a member of the given group. If the /// <returns>A <see cref="Task{TResult}"/> containing a flag indicating if the specified user is a member of the given group. If the
/// user is a member of the group the returned value with be true, otherwise it will be false.</returns> /// user is a member of the group the returned value with be true, otherwise it will be false.</returns>
public virtual async Task<bool> IsInRoleAsync([NotNull] IdentityUser user, [NotNull] string normalizedRoleName, CancellationToken cancellationToken = default) public virtual async Task<bool> IsInRoleAsync(
[NotNull] IdentityUser user,
[NotNull] string normalizedRoleName,
CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
Check.NotNull(user, nameof(user)); Check.NotNull(user, nameof(user));
Check.NotNullOrWhiteSpace(normalizedRoleName, nameof(normalizedRoleName));
if (string.IsNullOrWhiteSpace(normalizedRoleName)) var roles = await GetRolesAsync(user, cancellationToken);
{
throw new ArgumentException(nameof(normalizedRoleName) + " can not be null or whitespace");
}
var role = await RoleRepository.FindByNormalizedNameAsync(normalizedRoleName, cancellationToken: cancellationToken);
if (role == null)
{
return false;
}
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, cancellationToken);
return user.IsInRole(role.Id); return roles
.Select(r => LookupNormalizer.NormalizeName(r))
.Contains(normalizedRoleName);
} }
/// <summary> /// <summary>

21
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs

@ -49,10 +49,23 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken)); var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken));
var organizationUnitIds = user.OrganizationUnits.Select(r => r.OrganizationUnitId);
var organizationUnits = DbContext.OrganizationUnits.AsQueryable().Where(ou => organizationUnitIds.Contains(ou.Id)); var organizationUnitIds = user.OrganizationUnits
var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)); .Select(r => r.OrganizationUnitId)
return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(GetCancellationToken(cancellationToken)); .ToArray();
var organizationUnits = DbContext.OrganizationUnits
.AsQueryable()
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToArray();
var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
return await DbContext.Roles //TODO: Such usage suppress filters!
.AsQueryable()
.Where(r => roleIds.Contains(r.Id))
.Select(r => r.Name)
.ToListAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task<IdentityUser> FindByLoginAsync( public virtual async Task<IdentityUser> FindByLoginAsync(

3
modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs

@ -189,9 +189,10 @@ namespace Volo.Abp.Identity
//Assert //Assert
result.Items.Count.ShouldBe(2); result.Items.Count.ShouldBe(3);
result.Items.ShouldContain(r => r.Name == "moderator"); result.Items.ShouldContain(r => r.Name == "moderator");
result.Items.ShouldContain(r => r.Name == "supporter"); result.Items.ShouldContain(r => r.Name == "supporter");
result.Items.ShouldContain(r => r.Name == "manager");
} }
[Fact] [Fact]

17
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs

@ -20,8 +20,9 @@ namespace Volo.Abp.Identity
private readonly OrganizationUnitManager _organizationUnitManager; private readonly OrganizationUnitManager _organizationUnitManager;
private IdentityRole _adminRole; private IdentityRole _adminRole;
private IdentityRole _moderator; private IdentityRole _moderatorRole;
private IdentityRole _supporterRole; private IdentityRole _supporterRole;
private IdentityRole _managerRole;
private OrganizationUnit _ou111; private OrganizationUnit _ou111;
private OrganizationUnit _ou112; private OrganizationUnit _ou112;
@ -57,12 +58,15 @@ namespace Volo.Abp.Identity
{ {
_adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.NormalizeName("admin")); _adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.NormalizeName("admin"));
_moderator = new IdentityRole(_testData.RoleModeratorId, "moderator"); _moderatorRole = new IdentityRole(_testData.RoleModeratorId, "moderator");
_moderator.AddClaim(_guidGenerator, new Claim("test-claim", "test-value")); _moderatorRole.AddClaim(_guidGenerator, new Claim("test-claim", "test-value"));
await _roleRepository.InsertAsync(_moderator); await _roleRepository.InsertAsync(_moderatorRole);
_supporterRole = new IdentityRole(_guidGenerator.Create(), "supporter"); _supporterRole = new IdentityRole(_guidGenerator.Create(), "supporter");
await _roleRepository.InsertAsync(_supporterRole); await _roleRepository.InsertAsync(_supporterRole);
_managerRole = new IdentityRole(_guidGenerator.Create(), "manager");
await _roleRepository.InsertAsync(_managerRole);
} }
/* Creates OU tree as shown below: /* Creates OU tree as shown below:
@ -86,7 +90,8 @@ namespace Volo.Abp.Identity
_ou111 = new OrganizationUnit(null, "OU111", ou11.Id); _ou111 = new OrganizationUnit(null, "OU111", ou11.Id);
_ou111.Code = OrganizationUnit.CreateCode(1, 1, 1); _ou111.Code = OrganizationUnit.CreateCode(1, 1, 1);
_ou111.AddRole(_moderator.Id); _ou111.AddRole(_moderatorRole.Id);
_ou111.AddRole(_managerRole.Id);
await _organizationUnitRepository.InsertAsync(_ou111); await _organizationUnitRepository.InsertAsync(_ou111);
} }
@ -98,7 +103,7 @@ namespace Volo.Abp.Identity
await _userRepository.InsertAsync(adminUser); await _userRepository.InsertAsync(adminUser);
var john = new IdentityUser(_testData.UserJohnId, "john.nash", "john.nash@abp.io"); var john = new IdentityUser(_testData.UserJohnId, "john.nash", "john.nash@abp.io");
john.AddRole(_moderator.Id); john.AddRole(_moderatorRole.Id);
john.AddRole(_supporterRole.Id); john.AddRole(_supporterRole.Id);
john.AddOrganizationUnit(_ou111.Id); john.AddOrganizationUnit(_ou111.Id);
john.AddOrganizationUnit(_ou112.Id); john.AddOrganizationUnit(_ou112.Id);

Loading…
Cancel
Save