Add tests and fix mongodb repo.

pull/3732/head
Halil İbrahim Kalkan 6 years ago
parent 6c8557810f
commit a75459d480

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

@ -53,6 +53,7 @@ namespace Volo.Abp.Identity
protected IIdentityRoleRepository RoleRepository { get; }
protected IGuidGenerator GuidGenerator { get; }
protected ILogger<IdentityRoleStore> Logger { get; }
protected ILookupNormalizer LookupNormalizer { get; }
protected IIdentityUserRepository UserRepository { get; }
public IdentityUserStore(
@ -60,12 +61,14 @@ namespace Volo.Abp.Identity
IIdentityRoleRepository roleRepository,
IGuidGenerator guidGenerator,
ILogger<IdentityRoleStore> logger,
ILookupNormalizer lookupNormalizer,
IdentityErrorDescriber describer = null)
{
UserRepository = userRepository;
RoleRepository = roleRepository;
GuidGenerator = guidGenerator;
Logger = logger;
LookupNormalizer = lookupNormalizer;
ErrorDescriber = describer ?? new IdentityErrorDescriber();
}
@ -313,13 +316,17 @@ namespace Volo.Abp.Identity
Check.NotNull(user, nameof(user));
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)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Role {0} does not exist!", normalizedRoleName));
}
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, cancellationToken);
user.AddRole(role.Id);
@ -337,11 +344,7 @@ namespace Volo.Abp.Identity
cancellationToken.ThrowIfCancellationRequested();
Check.NotNull(user, nameof(user));
if (string.IsNullOrWhiteSpace(normalizedRoleName))
{
throw new ArgumentException(nameof(normalizedRoleName) + " can not be null or whitespace");
}
Check.NotNullOrWhiteSpace(normalizedRoleName, nameof(normalizedRoleName));
var role = await RoleRepository.FindByNormalizedNameAsync(normalizedRoleName, cancellationToken: cancellationToken);
if (role == null)
@ -366,8 +369,11 @@ namespace Volo.Abp.Identity
Check.NotNull(user, nameof(user));
var userRoles = await UserRepository.GetRoleNamesAsync(user.Id, cancellationToken: cancellationToken);
var userOrganizationUnitRoles = await UserRepository.GetRoleNamesInOrganizationUnitAsync(user.Id, cancellationToken: cancellationToken);
var userRoles = await UserRepository
.GetRoleNamesAsync(user.Id, cancellationToken: cancellationToken);
var userOrganizationUnitRoles = await UserRepository
.GetRoleNamesInOrganizationUnitAsync(user.Id, cancellationToken: cancellationToken);
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>
/// <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>
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();
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);
if (role == null)
{
return false;
}
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles, cancellationToken);
var roles = await GetRolesAsync(user, cancellationToken);
return user.IsInRole(role.Id);
return roles
.Select(r => LookupNormalizer.NormalizeName(r))
.Contains(normalizedRoleName);
}
/// <summary>

@ -49,10 +49,23 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default)
{
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 roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId));
return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(GetCancellationToken(cancellationToken));
var organizationUnitIds = user.OrganizationUnits
.Select(r => r.OrganizationUnitId)
.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(

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

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

Loading…
Cancel
Save