|
|
@ -43,15 +43,15 @@ namespace Volo.Abp.Identity |
|
|
IOrganizationUnitRepository organizationUnitRepository, |
|
|
IOrganizationUnitRepository organizationUnitRepository, |
|
|
ISettingProvider settingProvider) |
|
|
ISettingProvider settingProvider) |
|
|
: base( |
|
|
: base( |
|
|
store, |
|
|
store, |
|
|
optionsAccessor, |
|
|
optionsAccessor, |
|
|
passwordHasher, |
|
|
passwordHasher, |
|
|
userValidators, |
|
|
userValidators, |
|
|
passwordValidators, |
|
|
passwordValidators, |
|
|
keyNormalizer, |
|
|
keyNormalizer, |
|
|
errors, |
|
|
errors, |
|
|
services, |
|
|
services, |
|
|
logger) |
|
|
logger) |
|
|
{ |
|
|
{ |
|
|
OrganizationUnitRepository = organizationUnitRepository; |
|
|
OrganizationUnitRepository = organizationUnitRepository; |
|
|
SettingProvider = settingProvider; |
|
|
SettingProvider = settingProvider; |
|
|
@ -71,7 +71,8 @@ namespace Volo.Abp.Identity |
|
|
return user; |
|
|
return user; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public virtual async Task<IdentityResult> SetRolesAsync([NotNull] IdentityUser user, [NotNull] IEnumerable<string> roleNames) |
|
|
public virtual async Task<IdentityResult> SetRolesAsync([NotNull] IdentityUser user, |
|
|
|
|
|
[NotNull] IEnumerable<string> roleNames) |
|
|
{ |
|
|
{ |
|
|
Check.NotNull(user, nameof(user)); |
|
|
Check.NotNull(user, nameof(user)); |
|
|
Check.NotNull(roleNames, nameof(roleNames)); |
|
|
Check.NotNull(roleNames, nameof(roleNames)); |
|
|
@ -101,7 +102,8 @@ namespace Volo.Abp.Identity |
|
|
|
|
|
|
|
|
public virtual async Task<bool> IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) |
|
|
public virtual async Task<bool> IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) |
|
|
{ |
|
|
{ |
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); |
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, |
|
|
|
|
|
CancellationTokenProvider.Token); |
|
|
return user.IsInOrganizationUnit(ou.Id); |
|
|
return user.IsInOrganizationUnit(ou.Id); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -110,12 +112,13 @@ namespace Volo.Abp.Identity |
|
|
await AddToOrganizationUnitAsync( |
|
|
await AddToOrganizationUnitAsync( |
|
|
await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), |
|
|
await UserRepository.GetAsync(userId, cancellationToken: CancellationToken), |
|
|
await OrganizationUnitRepository.GetAsync(ouId, cancellationToken: CancellationToken) |
|
|
await OrganizationUnitRepository.GetAsync(ouId, cancellationToken: CancellationToken) |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public virtual async Task AddToOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) |
|
|
public virtual async Task AddToOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) |
|
|
{ |
|
|
{ |
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); |
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, |
|
|
|
|
|
CancellationTokenProvider.Token); |
|
|
|
|
|
|
|
|
if (user.OrganizationUnits.Any(cou => cou.OrganizationUnitId == ou.Id)) |
|
|
if (user.OrganizationUnits.Any(cou => cou.OrganizationUnitId == ou.Id)) |
|
|
{ |
|
|
{ |
|
|
@ -125,19 +128,23 @@ namespace Volo.Abp.Identity |
|
|
await CheckMaxUserOrganizationUnitMembershipCountAsync(user.OrganizationUnits.Count + 1); |
|
|
await CheckMaxUserOrganizationUnitMembershipCountAsync(user.OrganizationUnits.Count + 1); |
|
|
|
|
|
|
|
|
user.AddOrganizationUnit(ou.Id); |
|
|
user.AddOrganizationUnit(ou.Id); |
|
|
|
|
|
await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public virtual async Task RemoveFromOrganizationUnitAsync(Guid userId, Guid ouId) |
|
|
public virtual async Task RemoveFromOrganizationUnitAsync(Guid userId, Guid ouId) |
|
|
{ |
|
|
{ |
|
|
var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); |
|
|
var user = await UserRepository.GetAsync(userId, cancellationToken: CancellationToken); |
|
|
user.RemoveOrganizationUnit(ouId); |
|
|
user.RemoveOrganizationUnit(ouId); |
|
|
|
|
|
await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public virtual async Task RemoveFromOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) |
|
|
public virtual async Task RemoveFromOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) |
|
|
{ |
|
|
{ |
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); |
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, |
|
|
|
|
|
CancellationTokenProvider.Token); |
|
|
|
|
|
|
|
|
user.RemoveOrganizationUnit(ou.Id); |
|
|
user.RemoveOrganizationUnit(ou.Id); |
|
|
|
|
|
await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public virtual async Task SetOrganizationUnitsAsync(Guid userId, params Guid[] organizationUnitIds) |
|
|
public virtual async Task SetOrganizationUnitsAsync(Guid userId, params Guid[] organizationUnitIds) |
|
|
@ -155,7 +162,8 @@ namespace Volo.Abp.Identity |
|
|
|
|
|
|
|
|
await CheckMaxUserOrganizationUnitMembershipCountAsync(organizationUnitIds.Length); |
|
|
await CheckMaxUserOrganizationUnitMembershipCountAsync(organizationUnitIds.Length); |
|
|
|
|
|
|
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); |
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, |
|
|
|
|
|
CancellationTokenProvider.Token); |
|
|
|
|
|
|
|
|
//Remove from removed OUs
|
|
|
//Remove from removed OUs
|
|
|
foreach (var ouId in user.OrganizationUnits.Select(uou => uou.OrganizationUnitId).ToArray()) |
|
|
foreach (var ouId in user.OrganizationUnits.Select(uou => uou.OrganizationUnitId).ToArray()) |
|
|
@ -174,11 +182,14 @@ namespace Volo.Abp.Identity |
|
|
user.AddOrganizationUnit(organizationUnitId); |
|
|
user.AddOrganizationUnit(organizationUnitId); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await UserRepository.UpdateAsync(user, cancellationToken: CancellationToken); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private async Task CheckMaxUserOrganizationUnitMembershipCountAsync(int requestedCount) |
|
|
private async Task CheckMaxUserOrganizationUnitMembershipCountAsync(int requestedCount) |
|
|
{ |
|
|
{ |
|
|
var maxCount = await SettingProvider.GetAsync<int>(IdentitySettingNames.OrganizationUnit.MaxUserMembershipCount); |
|
|
var maxCount = |
|
|
|
|
|
await SettingProvider.GetAsync<int>(IdentitySettingNames.OrganizationUnit.MaxUserMembershipCount); |
|
|
if (requestedCount > maxCount) |
|
|
if (requestedCount > maxCount) |
|
|
{ |
|
|
{ |
|
|
throw new BusinessException(IdentityErrorCodes.MaxAllowedOuMembership) |
|
|
throw new BusinessException(IdentityErrorCodes.MaxAllowedOuMembership) |
|
|
@ -187,9 +198,11 @@ namespace Volo.Abp.Identity |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[UnitOfWork] |
|
|
[UnitOfWork] |
|
|
public virtual async Task<List<OrganizationUnit>> GetOrganizationUnitsAsync(IdentityUser user, bool includeDetails = false) |
|
|
public virtual async Task<List<OrganizationUnit>> GetOrganizationUnitsAsync(IdentityUser user, |
|
|
|
|
|
bool includeDetails = false) |
|
|
{ |
|
|
{ |
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); |
|
|
await UserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, |
|
|
|
|
|
CancellationTokenProvider.Token); |
|
|
|
|
|
|
|
|
return await OrganizationUnitRepository.GetListAsync( |
|
|
return await OrganizationUnitRepository.GetListAsync( |
|
|
user.OrganizationUnits.Select(t => t.OrganizationUnitId), |
|
|
user.OrganizationUnits.Select(t => t.OrganizationUnitId), |
|
|
|