|
|
|
@ -1,9 +1,7 @@ |
|
|
|
using MongoDB.Bson; |
|
|
|
using MongoDB.Driver; |
|
|
|
using MongoDB.Driver; |
|
|
|
using MongoDB.Driver.Linq; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Data; |
|
|
|
using System.Linq; |
|
|
|
using System.Linq.Dynamic.Core; |
|
|
|
using System.Threading; |
|
|
|
@ -11,7 +9,6 @@ using System.Threading.Tasks; |
|
|
|
using Volo.Abp.Domain.Repositories.MongoDB; |
|
|
|
using Volo.Abp.MongoDB; |
|
|
|
using Volo.Abp.MultiTenancy; |
|
|
|
using Volo.Abp.Uow; |
|
|
|
|
|
|
|
namespace Volo.Abp.Identity.MongoDB |
|
|
|
{ |
|
|
|
@ -30,7 +27,7 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
bool includeDetails = false, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return await GetMongoQueryable() |
|
|
|
return await (await GetMongoQueryableAsync(cancellationToken)) |
|
|
|
.Where(ou => ou.ParentId == parentId) |
|
|
|
.ToListAsync(GetCancellationToken(cancellationToken)); |
|
|
|
} |
|
|
|
@ -41,7 +38,7 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
bool includeDetails = false, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return await GetMongoQueryable() |
|
|
|
return await (await GetMongoQueryableAsync(cancellationToken)) |
|
|
|
.Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value) |
|
|
|
.ToListAsync(GetCancellationToken(cancellationToken)); |
|
|
|
} |
|
|
|
@ -51,7 +48,7 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
bool includeDetails = false, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return await GetMongoQueryable() |
|
|
|
return await (await GetMongoQueryableAsync(cancellationToken)) |
|
|
|
.Where(t => ids.Contains(t.Id)) |
|
|
|
.ToListAsync(GetCancellationToken(cancellationToken)); |
|
|
|
} |
|
|
|
@ -63,7 +60,7 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
bool includeDetails = false, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return await GetMongoQueryable() |
|
|
|
return await (await GetMongoQueryableAsync(cancellationToken)) |
|
|
|
.OrderBy(sorting ?? nameof(OrganizationUnit.DisplayName)) |
|
|
|
.As<IMongoQueryable<OrganizationUnit>>() |
|
|
|
.PageBy<OrganizationUnit, IMongoQueryable<OrganizationUnit>>(skipCount, maxResultCount) |
|
|
|
@ -75,7 +72,7 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
bool includeDetails = true, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return await GetMongoQueryable() |
|
|
|
return await (await GetMongoQueryableAsync(cancellationToken)) |
|
|
|
.OrderBy(x => x.Id) |
|
|
|
.FirstOrDefaultAsync( |
|
|
|
ou => ou.DisplayName == displayName, |
|
|
|
@ -92,7 +89,10 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>(DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id))) |
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>( |
|
|
|
dbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)) |
|
|
|
) |
|
|
|
.OrderBy(sorting ?? nameof(IdentityRole.Name)) |
|
|
|
.As<IMongoQueryable<IdentityRole>>() |
|
|
|
.PageBy<IdentityRole, IMongoQueryable<IdentityRole>>(skipCount, maxResultCount) |
|
|
|
@ -104,7 +104,10 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>( DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id))) |
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>( |
|
|
|
dbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)) |
|
|
|
) |
|
|
|
.As<IMongoQueryable<IdentityRole>>() |
|
|
|
.CountAsync(cancellationToken); |
|
|
|
} |
|
|
|
@ -119,7 +122,8 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>(DbContext.Roles.AsQueryable()) |
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>(dbContext.Roles.AsQueryable()) |
|
|
|
.Where(r => !roleIds.Contains(r.Id)) |
|
|
|
.WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter)) |
|
|
|
.OrderBy(sorting ?? nameof(IdentityRole.Name)) |
|
|
|
@ -134,7 +138,8 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>(DbContext.Roles.AsQueryable()) |
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>(dbContext.Roles.AsQueryable()) |
|
|
|
.Where(r => !roleIds.Contains(r.Id)) |
|
|
|
.WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter)) |
|
|
|
.As<IMongoQueryable<IdentityRole>>() |
|
|
|
@ -150,13 +155,13 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
bool includeDetails = false, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var query = CreateGetMembersFilteredQuery(organizationUnit, filter); |
|
|
|
|
|
|
|
cancellationToken = GetCancellationToken(cancellationToken); |
|
|
|
var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter, cancellationToken); |
|
|
|
return await query |
|
|
|
.OrderBy(sorting ?? nameof(IdentityUser.UserName)) |
|
|
|
.As<IMongoQueryable<IdentityUser>>() |
|
|
|
.PageBy<IdentityUser, IMongoQueryable<IdentityUser>>(skipCount, maxResultCount) |
|
|
|
.ToListAsync(GetCancellationToken(cancellationToken)); |
|
|
|
.ToListAsync(cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task<int> GetMembersCountAsync( |
|
|
|
@ -164,9 +169,9 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
string filter = null, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var query = CreateGetMembersFilteredQuery(organizationUnit, filter); |
|
|
|
|
|
|
|
return await query.CountAsync(GetCancellationToken(cancellationToken)); |
|
|
|
cancellationToken = GetCancellationToken(cancellationToken); |
|
|
|
var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter, cancellationToken); |
|
|
|
return await query.CountAsync(cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<List<IdentityUser>> GetUnaddedUsersAsync( |
|
|
|
@ -178,7 +183,8 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
bool includeDetails = false, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(DbContext.Users.AsQueryable()) |
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable()) |
|
|
|
.Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) |
|
|
|
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>( |
|
|
|
!filter.IsNullOrWhiteSpace(), |
|
|
|
@ -196,7 +202,8 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
public async Task<int> GetUnaddedUsersCountAsync(OrganizationUnit organizationUnit, string filter = null, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(DbContext.Users.AsQueryable()) |
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
return await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable()) |
|
|
|
.Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) |
|
|
|
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>( |
|
|
|
!filter.IsNullOrWhiteSpace(), |
|
|
|
@ -217,7 +224,8 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
|
|
|
|
public virtual async Task RemoveAllMembersAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var users = await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(DbContext.Users.AsQueryable()) |
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
var users = await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable()) |
|
|
|
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) |
|
|
|
.As<IMongoQueryable<IdentityUser>>() |
|
|
|
.ToListAsync(GetCancellationToken(cancellationToken)); |
|
|
|
@ -225,13 +233,17 @@ namespace Volo.Abp.Identity.MongoDB |
|
|
|
foreach (var user in users) |
|
|
|
{ |
|
|
|
user.RemoveOrganizationUnit(organizationUnit.Id); |
|
|
|
DbContext.Users.ReplaceOne(u => u.Id == user.Id, user); |
|
|
|
await dbContext.Users.ReplaceOneAsync(u => u.Id == user.Id, user, cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual IMongoQueryable<IdentityUser> CreateGetMembersFilteredQuery(OrganizationUnit organizationUnit, string filter = null) |
|
|
|
protected virtual async Task<IMongoQueryable<IdentityUser>> CreateGetMembersFilteredQueryAsync( |
|
|
|
OrganizationUnit organizationUnit, |
|
|
|
string filter = null, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(DbContext.Users.AsQueryable()) |
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
return ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable()) |
|
|
|
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) |
|
|
|
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>( |
|
|
|
!filter.IsNullOrWhiteSpace(), |
|
|
|
|