|
|
@ -2,9 +2,7 @@ |
|
|
using System; |
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
|
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
using Volo.Abp.Domain.Repositories; |
|
|
|
|
|
using Volo.Abp.Domain.Services; |
|
|
using Volo.Abp.Domain.Services; |
|
|
using Volo.Abp.Identity.Localization; |
|
|
using Volo.Abp.Identity.Localization; |
|
|
using Volo.Abp.Threading; |
|
|
using Volo.Abp.Threading; |
|
|
@ -17,11 +15,10 @@ namespace Volo.Abp.Identity.Organizations |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public class OrganizationUnitManager : DomainService |
|
|
public class OrganizationUnitManager : DomainService |
|
|
{ |
|
|
{ |
|
|
protected IOrganizationUnitRepository _organizationUnitRepository { get; private set; } |
|
|
protected IOrganizationUnitRepository OrganizationUnitRepository { get; } |
|
|
|
|
|
protected IStringLocalizer<IdentityResource> Localizer { get; } |
|
|
private readonly IStringLocalizer<IdentityResource> _localizer; |
|
|
protected IIdentityRoleRepository IdentityRoleRepository { get; } |
|
|
private readonly IIdentityRoleRepository _identityRoleRepository; |
|
|
protected ICancellationTokenProvider CancellationTokenProvider { get; } |
|
|
private readonly ICancellationTokenProvider _cancellationTokenProvider; |
|
|
|
|
|
|
|
|
|
|
|
public OrganizationUnitManager( |
|
|
public OrganizationUnitManager( |
|
|
IOrganizationUnitRepository organizationUnitRepository, |
|
|
IOrganizationUnitRepository organizationUnitRepository, |
|
|
@ -29,10 +26,10 @@ namespace Volo.Abp.Identity.Organizations |
|
|
IIdentityRoleRepository identityRoleRepository, |
|
|
IIdentityRoleRepository identityRoleRepository, |
|
|
ICancellationTokenProvider cancellationTokenProvider) |
|
|
ICancellationTokenProvider cancellationTokenProvider) |
|
|
{ |
|
|
{ |
|
|
_organizationUnitRepository = organizationUnitRepository; |
|
|
OrganizationUnitRepository = organizationUnitRepository; |
|
|
_localizer = localizer; |
|
|
Localizer = localizer; |
|
|
_identityRoleRepository = identityRoleRepository; |
|
|
IdentityRoleRepository = identityRoleRepository; |
|
|
_cancellationTokenProvider = cancellationTokenProvider; |
|
|
CancellationTokenProvider = cancellationTokenProvider; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[UnitOfWork] |
|
|
[UnitOfWork] |
|
|
@ -40,30 +37,36 @@ namespace Volo.Abp.Identity.Organizations |
|
|
{ |
|
|
{ |
|
|
organizationUnit.Code = await GetNextChildCodeAsync(organizationUnit.ParentId); |
|
|
organizationUnit.Code = await GetNextChildCodeAsync(organizationUnit.ParentId); |
|
|
await ValidateOrganizationUnitAsync(organizationUnit); |
|
|
await ValidateOrganizationUnitAsync(organizationUnit); |
|
|
await _organizationUnitRepository.InsertAsync(organizationUnit); |
|
|
await OrganizationUnitRepository.InsertAsync(organizationUnit); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public virtual async Task UpdateAsync(OrganizationUnit organizationUnit) |
|
|
public virtual async Task UpdateAsync(OrganizationUnit organizationUnit) |
|
|
{ |
|
|
{ |
|
|
await ValidateOrganizationUnitAsync(organizationUnit); |
|
|
await ValidateOrganizationUnitAsync(organizationUnit); |
|
|
await _organizationUnitRepository.UpdateAsync(organizationUnit); |
|
|
await OrganizationUnitRepository.UpdateAsync(organizationUnit); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public virtual async Task<string> GetNextChildCodeAsync(Guid? parentId) |
|
|
public virtual async Task<string> GetNextChildCodeAsync(Guid? parentId) |
|
|
{ |
|
|
{ |
|
|
var lastChild = await GetLastChildOrNullAsync(parentId); |
|
|
var lastChild = await GetLastChildOrNullAsync(parentId); |
|
|
if (lastChild == null) |
|
|
if (lastChild != null) |
|
|
{ |
|
|
{ |
|
|
var parentCode = parentId != null ? await GetCodeOrDefaultAsync(parentId.Value) : null; |
|
|
return OrganizationUnit.CalculateNextCode(lastChild.Code); |
|
|
return OrganizationUnit.AppendCode(parentCode, OrganizationUnit.CreateCode(1)); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return OrganizationUnit.CalculateNextCode(lastChild.Code); |
|
|
var parentCode = parentId != null |
|
|
|
|
|
? await GetCodeOrDefaultAsync(parentId.Value) |
|
|
|
|
|
: null; |
|
|
|
|
|
|
|
|
|
|
|
return OrganizationUnit.AppendCode( |
|
|
|
|
|
parentCode, |
|
|
|
|
|
OrganizationUnit.CreateCode(1) |
|
|
|
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public virtual async Task<OrganizationUnit> GetLastChildOrNullAsync(Guid? parentId) |
|
|
public virtual async Task<OrganizationUnit> GetLastChildOrNullAsync(Guid? parentId) |
|
|
{ |
|
|
{ |
|
|
var children = await _organizationUnitRepository.GetChildrenAsync(parentId); |
|
|
var children = await OrganizationUnitRepository.GetChildrenAsync(parentId); |
|
|
return children.OrderBy(c => c.Code).LastOrDefault(); |
|
|
return children.OrderBy(c => c.Code).LastOrDefault(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -74,16 +77,16 @@ namespace Volo.Abp.Identity.Organizations |
|
|
|
|
|
|
|
|
foreach (var child in children) |
|
|
foreach (var child in children) |
|
|
{ |
|
|
{ |
|
|
await _organizationUnitRepository.DeleteAsync(child); |
|
|
await OrganizationUnitRepository.DeleteAsync(child); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
await _organizationUnitRepository.DeleteAsync(id); |
|
|
await OrganizationUnitRepository.DeleteAsync(id); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[UnitOfWork] |
|
|
[UnitOfWork] |
|
|
public virtual async Task MoveAsync(Guid id, Guid? parentId) |
|
|
public virtual async Task MoveAsync(Guid id, Guid? parentId) |
|
|
{ |
|
|
{ |
|
|
var organizationUnit = await _organizationUnitRepository.GetAsync(id); |
|
|
var organizationUnit = await OrganizationUnitRepository.GetAsync(id); |
|
|
if (organizationUnit.ParentId == parentId) |
|
|
if (organizationUnit.ParentId == parentId) |
|
|
{ |
|
|
{ |
|
|
return; |
|
|
return; |
|
|
@ -110,7 +113,7 @@ namespace Volo.Abp.Identity.Organizations |
|
|
|
|
|
|
|
|
public virtual async Task<string> GetCodeOrDefaultAsync(Guid id) |
|
|
public virtual async Task<string> GetCodeOrDefaultAsync(Guid id) |
|
|
{ |
|
|
{ |
|
|
var ou = await _organizationUnitRepository.GetAsync(id); |
|
|
var ou = await OrganizationUnitRepository.GetAsync(id); |
|
|
return ou?.Code; |
|
|
return ou?.Code; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -122,7 +125,7 @@ namespace Volo.Abp.Identity.Organizations |
|
|
|
|
|
|
|
|
if (siblings.Any(ou => ou.DisplayName == organizationUnit.DisplayName)) |
|
|
if (siblings.Any(ou => ou.DisplayName == organizationUnit.DisplayName)) |
|
|
{ |
|
|
{ |
|
|
throw new UserFriendlyException(_localizer["OrganizationUnitDuplicateDisplayNameWarning", organizationUnit.DisplayName]); |
|
|
throw new UserFriendlyException(Localizer["OrganizationUnitDuplicateDisplayNameWarning", organizationUnit.DisplayName]); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -130,17 +133,17 @@ namespace Volo.Abp.Identity.Organizations |
|
|
{ |
|
|
{ |
|
|
if (!recursive) |
|
|
if (!recursive) |
|
|
{ |
|
|
{ |
|
|
return await _organizationUnitRepository.GetChildrenAsync(parentId); |
|
|
return await OrganizationUnitRepository.GetChildrenAsync(parentId); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!parentId.HasValue) |
|
|
if (!parentId.HasValue) |
|
|
{ |
|
|
{ |
|
|
return await _organizationUnitRepository.GetListAsync(); |
|
|
return await OrganizationUnitRepository.GetListAsync(includeDetails: true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var code = await GetCodeOrDefaultAsync(parentId.Value); |
|
|
var code = await GetCodeOrDefaultAsync(parentId.Value); |
|
|
|
|
|
|
|
|
return await _organizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, parentId); |
|
|
return await OrganizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, parentId); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public virtual Task<bool> IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) |
|
|
public virtual Task<bool> IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) |
|
|
@ -151,8 +154,8 @@ namespace Volo.Abp.Identity.Organizations |
|
|
public virtual async Task AddRoleToOrganizationUnitAsync(Guid roleId, Guid ouId) |
|
|
public virtual async Task AddRoleToOrganizationUnitAsync(Guid roleId, Guid ouId) |
|
|
{ |
|
|
{ |
|
|
await AddRoleToOrganizationUnitAsync( |
|
|
await AddRoleToOrganizationUnitAsync( |
|
|
await _identityRoleRepository.GetAsync(roleId), |
|
|
await IdentityRoleRepository.GetAsync(roleId), |
|
|
await _organizationUnitRepository.GetAsync(ouId, true) |
|
|
await OrganizationUnitRepository.GetAsync(ouId, true) |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -171,8 +174,8 @@ namespace Volo.Abp.Identity.Organizations |
|
|
public virtual async Task RemoveRoleFromOrganizationUnitAsync(Guid roleId, Guid ouId) |
|
|
public virtual async Task RemoveRoleFromOrganizationUnitAsync(Guid roleId, Guid ouId) |
|
|
{ |
|
|
{ |
|
|
await RemoveRoleFromOrganizationUnitAsync( |
|
|
await RemoveRoleFromOrganizationUnitAsync( |
|
|
await _identityRoleRepository.GetAsync(roleId), |
|
|
await IdentityRoleRepository.GetAsync(roleId), |
|
|
await _organizationUnitRepository.GetAsync(ouId, true) |
|
|
await OrganizationUnitRepository.GetAsync(ouId, true) |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|