Skip to content

Commit ad35165

Browse files
committed
organization unit domain update
added GetMembersAsync and GetMEmbersCountAsync methods
1 parent a6ccd2c commit ad35165

File tree

5 files changed

+143
-23
lines changed

5 files changed

+143
-23
lines changed

modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,23 @@ Task<List<OrganizationUnit>> GetListAsync(
4242
);
4343

4444
Task<List<IdentityRole>> GetRolesAsync(
45-
Guid organizationUnitId,
45+
OrganizationUnit organizationUnit,
4646
bool includeDetails = false,
4747
CancellationToken cancellationToken = default
4848
);
49+
50+
Task<List<IdentityUser>> GetMembersAsync(
51+
OrganizationUnit organizationUnit,
52+
string sorting = null,
53+
int maxResultCount = int.MaxValue,
54+
int skipCount = 0,
55+
string filter = null,
56+
bool includeDetails = false,
57+
CancellationToken cancellationToken = default
58+
);
59+
Task<int> GetMembersCountAsync(
60+
OrganizationUnit organizationUnit,
61+
CancellationToken cancellationToken = default
62+
);
4963
}
5064
}

modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs

+53-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public EfCoreOrganizationUnitRepository(
2020
{
2121
}
2222

23-
public async Task<List<OrganizationUnit>> GetChildrenAsync(
23+
public virtual async Task<List<OrganizationUnit>> GetChildrenAsync(
2424
Guid? parentId,
2525
bool includeDetails = false,
2626
CancellationToken cancellationToken = default)
@@ -31,7 +31,7 @@ public async Task<List<OrganizationUnit>> GetChildrenAsync(
3131
.ToListAsync(GetCancellationToken(cancellationToken));
3232
}
3333

34-
public async Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(
34+
public virtual async Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(
3535
string code,
3636
Guid? parentId,
3737
bool includeDetails = false,
@@ -43,7 +43,7 @@ public async Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(
4343
.ToListAsync(GetCancellationToken(cancellationToken));
4444
}
4545

46-
public async Task<List<OrganizationUnit>> GetListAsync(
46+
public virtual async Task<List<OrganizationUnit>> GetListAsync(
4747
string sorting = null,
4848
int maxResultCount = int.MaxValue,
4949
int skipCount = 0,
@@ -56,7 +56,7 @@ public async Task<List<OrganizationUnit>> GetListAsync(
5656
.PageBy(skipCount, maxResultCount)
5757
.ToListAsync(GetCancellationToken(cancellationToken));
5858
}
59-
public async Task<List<OrganizationUnit>> GetListAsync(
59+
public virtual async Task<List<OrganizationUnit>> GetListAsync(
6060
IEnumerable<Guid> ids,
6161
bool includeDetails = false,
6262
CancellationToken cancellationToken = default)
@@ -67,7 +67,7 @@ public async Task<List<OrganizationUnit>> GetListAsync(
6767
.ToListAsync(GetCancellationToken(cancellationToken));
6868
}
6969

70-
public async Task<OrganizationUnit> GetAsync(
70+
public virtual async Task<OrganizationUnit> GetAsync(
7171
string displayName,
7272
bool includeDetails = true,
7373
CancellationToken cancellationToken = default)
@@ -78,22 +78,65 @@ public async Task<OrganizationUnit> GetAsync(
7878
ou => ou.DisplayName == displayName,
7979
GetCancellationToken(cancellationToken)
8080
);
81-
}
82-
public async Task<List<IdentityRole>> GetRolesAsync(
83-
Guid organizationUnitId, bool includeDetails = false,
81+
}
82+
83+
public virtual async Task<List<IdentityRole>> GetRolesAsync(
84+
OrganizationUnit organizationUnit,
85+
bool includeDetails = false,
8486
CancellationToken cancellationToken = default)
8587
{
8688
var query = from organizationRole in DbContext.Set<OrganizationUnitRole>()
8789
join role in DbContext.Roles.IncludeDetails(includeDetails) on organizationRole.RoleId equals role.Id
88-
where organizationRole.OrganizationUnitId == organizationUnitId
90+
where organizationRole.OrganizationUnitId == organizationUnit.Id
8991
select role;
9092

9193
return await query.ToListAsync(GetCancellationToken(cancellationToken));
9294
}
9395

96+
public virtual async Task<List<IdentityUser>> GetMembersAsync(
97+
OrganizationUnit organizationUnit,
98+
string sorting = null,
99+
int maxResultCount = int.MaxValue,
100+
int skipCount = 0,
101+
string filter = null,
102+
bool includeDetails = false,
103+
CancellationToken cancellationToken = default
104+
)
105+
{
106+
var query = from userOu in DbContext.Set<IdentityUserOrganizationUnit>()
107+
join user in DbContext.Users.IncludeDetails(includeDetails) on userOu.UserId equals user.Id
108+
where userOu.OrganizationUnitId == organizationUnit.Id
109+
select user;
110+
111+
if (!filter.IsNullOrWhiteSpace())
112+
{
113+
query = query.Where(u =>
114+
u.UserName.Contains(filter) ||
115+
u.Email.Contains(filter) ||
116+
(u.PhoneNumber != null && u.PhoneNumber.Contains(filter))
117+
);
118+
}
119+
120+
return await query.OrderBy(sorting ?? nameof(IdentityUser.UserName))
121+
.PageBy(skipCount, maxResultCount)
122+
.ToListAsync(GetCancellationToken(cancellationToken));
123+
}
124+
125+
public virtual async Task<int> GetMembersCountAsync(
126+
OrganizationUnit organizationUnit,
127+
CancellationToken cancellationToken = default)
128+
{
129+
var query = from userOu in DbContext.Set<IdentityUserOrganizationUnit>()
130+
join user in DbContext.Users on userOu.UserId equals user.Id
131+
where userOu.OrganizationUnitId == organizationUnit.Id
132+
select user;
133+
134+
return await query.CountAsync(GetCancellationToken(cancellationToken));
135+
}
136+
94137
public override IQueryable<OrganizationUnit> WithDetails()
95138
{
96139
return GetQueryable().IncludeDetails();
97-
}
140+
}
98141
}
99142
}

modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs

+38-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public MongoOrganizationUnitRepository(
1919
{
2020
}
2121

22-
public async Task<List<OrganizationUnit>> GetChildrenAsync(
22+
public virtual async Task<List<OrganizationUnit>> GetChildrenAsync(
2323
Guid? parentId,
2424
bool includeDetails = false,
2525
CancellationToken cancellationToken = default)
@@ -29,7 +29,7 @@ public async Task<List<OrganizationUnit>> GetChildrenAsync(
2929
.ToListAsync(GetCancellationToken(cancellationToken));
3030
}
3131

32-
public async Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(
32+
public virtual async Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(
3333
string code,
3434
Guid? parentId,
3535
bool includeDetails = false,
@@ -40,7 +40,7 @@ public async Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(
4040
.ToListAsync(GetCancellationToken(cancellationToken));
4141
}
4242

43-
public async Task<List<OrganizationUnit>> GetListAsync(
43+
public virtual async Task<List<OrganizationUnit>> GetListAsync(
4444
IEnumerable<Guid> ids,
4545
bool includeDetails = false,
4646
CancellationToken cancellationToken = default)
@@ -50,7 +50,7 @@ public async Task<List<OrganizationUnit>> GetListAsync(
5050
.ToListAsync(GetCancellationToken(cancellationToken));
5151
}
5252

53-
public async Task<List<OrganizationUnit>> GetListAsync(
53+
public virtual async Task<List<OrganizationUnit>> GetListAsync(
5454
string sorting = null,
5555
int maxResultCount = int.MaxValue,
5656
int skipCount = 0,
@@ -64,7 +64,7 @@ public async Task<List<OrganizationUnit>> GetListAsync(
6464
.ToListAsync(GetCancellationToken(cancellationToken));
6565
}
6666

67-
public async Task<OrganizationUnit> GetAsync(
67+
public virtual async Task<OrganizationUnit> GetAsync(
6868
string displayName,
6969
bool includeDetails = true,
7070
CancellationToken cancellationToken = default)
@@ -76,14 +76,43 @@ public async Task<OrganizationUnit> GetAsync(
7676
);
7777
}
7878

79-
public async Task<List<IdentityRole>> GetRolesAsync(
80-
Guid organizationUnitId,
79+
public virtual async Task<List<IdentityRole>> GetRolesAsync(
80+
OrganizationUnit organizationUnit,
8181
bool includeDetails = false,
8282
CancellationToken cancellationToken = default)
8383
{
84-
var organizationUnit = await GetAsync(organizationUnitId, includeDetails, cancellationToken);
8584
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray();
8685
return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).ToListAsync(cancellationToken);
87-
}
86+
}
87+
public virtual async Task<List<IdentityUser>> GetMembersAsync(
88+
OrganizationUnit organizationUnit,
89+
string sorting = null,
90+
int maxResultCount = int.MaxValue,
91+
int skipCount = 0,
92+
string filter = null,
93+
bool includeDetails = false,
94+
CancellationToken cancellationToken = default)
95+
{
96+
return await DbContext.Users.AsQueryable()
97+
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
98+
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
99+
!filter.IsNullOrWhiteSpace(),
100+
u =>
101+
u.UserName.Contains(filter) ||
102+
u.Email.Contains(filter)
103+
)
104+
.OrderBy(sorting ?? nameof(IdentityUser.UserName))
105+
.As<IMongoQueryable<IdentityUser>>()
106+
.PageBy<IdentityUser, IMongoQueryable<IdentityUser>>(skipCount, maxResultCount)
107+
.ToListAsync(GetCancellationToken(cancellationToken));
108+
}
109+
110+
public virtual async Task<int> GetMembersCountAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default)
111+
{
112+
return await DbContext.Users.AsQueryable()
113+
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
114+
.As<IMongoQueryable<IdentityUser>>()
115+
.CountAsync(GetCancellationToken(cancellationToken));
116+
}
88117
}
89118
}

modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ private async Task AddUsers()
119119
var neo = new IdentityUser(_testData.UserNeoId, "neo", "[email protected]");
120120
neo.AddRole(_supporterRole.Id);
121121
neo.AddClaim(_guidGenerator, new Claim("TestClaimType", "43"));
122+
neo.AddOrganizationUnit(_ou111.Id);
122123
await _userRepository.InsertAsync(neo);
123124
}
124125

modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs

+36-3
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,53 @@ public async Task GetOrganizationUnitRolesAsync()
8484
{
8585
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true);
8686

87-
var ou111Roles = await _organizationUnitRepository.GetRolesAsync(ou.Id, true);
87+
var ou111Roles = await _organizationUnitRepository.GetRolesAsync(ou, true);
8888
ou111Roles.Count.ShouldBe(2);
8989
ou111Roles.ShouldContain(n => n.Name == "manager");
9090
ou111Roles.ShouldContain(n => n.Name == "moderator");
9191
}
9292

9393
[Fact]
94-
public async Task GetUsersInOrganizationUnitListAsync()
94+
public async Task GetMembersInOrganizationUnitListAsync()
9595
{
9696
OrganizationUnit ou1 = await _organizationUnitRepository.GetAsync("OU111", true);
9797
OrganizationUnit ou2 = await _organizationUnitRepository.GetAsync("OU112", true);
9898
var users = await _identityUserRepository.GetUsersInOrganizationsListAsync(new List<Guid> { ou1.Id, ou2.Id });
99-
//var dodo = users.ToDictionary(u => u.Id, u => u);
10099
users.Count.ShouldBeGreaterThan(0);
101100
}
101+
[Fact]
102+
public async Task GetMembersInOrganizationUnitWithParamsAsync()
103+
{
104+
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true);
105+
var users = await _organizationUnitRepository.GetMembersAsync(ou, "UserName DESC", 5, 0, "n");
106+
107+
users.Count.ShouldBeGreaterThan(1);
108+
users.Count.ShouldBeLessThanOrEqualTo(5);
109+
110+
//Filter check
111+
users.ShouldAllBe(u => u.UserName.Contains("ne") || u.Email.Contains("n"));
112+
113+
//Order check
114+
for (var i = 0; i < users.Count - 1; i++)
115+
{
116+
string.Compare(
117+
users[i].UserName,
118+
users[i + 1].UserName,
119+
StringComparison.OrdinalIgnoreCase
120+
).ShouldBeGreaterThan(0);
121+
}
122+
123+
users = await _organizationUnitRepository.GetMembersAsync(ou, null, 999, 0, "undefined-username");
124+
users.Count.ShouldBe(0);
125+
}
126+
127+
[Fact]
128+
public async Task GetCountMembersCountInOrganizationUnit()
129+
{
130+
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true);
131+
var users = await _organizationUnitRepository.GetMembersCountAsync(ou);
132+
133+
users.ShouldBeGreaterThan(1);
134+
}
102135
}
103136
}

0 commit comments

Comments
 (0)