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