Skip to content

Commit

Permalink
Update MongoIdentityUserRepository.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
MansurBesleney committed Jan 23, 2025
1 parent 3839317 commit 2f67c20
Showing 1 changed file with 50 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public virtual async Task<IdentityUser> FindByNormalizedUserNameAsync(
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(
u => u.NormalizedUserName == normalizedUserName,
Expand All @@ -43,13 +43,13 @@ public virtual async Task<List<string>> GetRoleNamesAsync(
.Select(r => r.OrganizationUnitId)
.ToArray();

var organizationUnits = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
var organizationUnits = await (await GetQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToListAsync(cancellationToken: cancellationToken);
var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
var roleIds = user.Roles.Select(r => r.RoleId).ToArray();
var allRoleIds = orgUnitRoleIds.Union(roleIds);
return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(cancellationToken);
return await (await GetQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(cancellationToken);
}

public virtual async Task<List<string>> GetRoleNamesInOrganizationUnitAsync(
Expand All @@ -63,13 +63,13 @@ public virtual async Task<List<string>> GetRoleNamesInOrganizationUnitAsync(
.Select(r => r.OrganizationUnitId)
.ToArray();

var organizationUnits = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
var organizationUnits = await (await GetQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToListAsync(cancellationToken: cancellationToken);

var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();

var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken);
var queryable = await GetQueryableAsync<IdentityRole>(cancellationToken);

return await queryable
.Where(r => roleIds.Contains(r.Id))
Expand All @@ -83,7 +83,7 @@ public virtual async Task<IdentityUser> FindByLoginAsync(
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.Where(u => u.Logins.Any(login => login.LoginProvider == loginProvider && login.ProviderKey == providerKey))
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
Expand All @@ -94,7 +94,7 @@ public virtual async Task<IdentityUser> FindByNormalizedEmailAsync(
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.OrderBy(x => x.Id).FirstOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, GetCancellationToken(cancellationToken));
}

Expand All @@ -103,14 +103,14 @@ public virtual async Task<List<IdentityUser>> GetListByClaimAsync(
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.Where(u => u.Claims.Any(c => c.ClaimType == claim.Type && c.ClaimValue == claim.Value))
.ToListAsync(GetCancellationToken(cancellationToken));
}

public virtual async Task RemoveClaimFromAllUsersAsync(string claimType, bool autoSave, CancellationToken cancellationToken = default)
{
var users = await (await GetMongoQueryableAsync(cancellationToken))
var users = await (await GetQueryableAsync(cancellationToken))
.Where(u => u.Claims.Any(c => c.ClaimType == claimType))
.ToListAsync(GetCancellationToken(cancellationToken));

Expand All @@ -129,7 +129,7 @@ public virtual async Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync(
{
cancellationToken = GetCancellationToken(cancellationToken);

var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken);
var queryable = await GetQueryableAsync<IdentityRole>(cancellationToken);

var role = await queryable
.Where(x => x.NormalizedName == normalizedRoleName)
Expand All @@ -141,7 +141,7 @@ public virtual async Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync(
return new List<IdentityUser>();
}

return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.Where(u => u.Roles.Any(r => r.RoleId == role.Id))
.ToListAsync(cancellationToken);
}
Expand All @@ -150,7 +150,7 @@ public virtual async Task<List<Guid>> GetUserIdListByRoleIdAsync(Guid roleId, Ca
{
cancellationToken = GetCancellationToken(cancellationToken);

return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.Where(u => u.Roles.Any(r => r.RoleId == roleId))
.Select(x => x.Id)
.ToListAsync(cancellationToken);
Expand Down Expand Up @@ -203,8 +203,7 @@ public virtual async Task<List<IdentityUser>> GetListAsync(

return await query
.OrderBy(sorting.IsNullOrWhiteSpace() ? nameof(IdentityUser.CreationTime) + " desc" : sorting)
.As<IMongoQueryable<IdentityUser>>()
.PageBy<IdentityUser, IMongoQueryable<IdentityUser>>(skipCount, maxResultCount)
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}

Expand All @@ -219,13 +218,13 @@ public virtual async Task<List<IdentityRole>> GetRolesAsync(
.Select(r => r.OrganizationUnitId)
.ToArray();

var organizationUnits = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
var organizationUnits = await (await GetQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToListAsync(cancellationToken: cancellationToken);
var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
var roleIds = user.Roles.Select(r => r.RoleId).ToArray();
var allRoleIds = orgUnitRoleIds.Union(roleIds);
return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).ToListAsync(cancellationToken);
return await (await GetQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).ToListAsync(cancellationToken);
}

public virtual async Task<List<OrganizationUnit>> GetOrganizationUnitsAsync(
Expand All @@ -237,7 +236,7 @@ public virtual async Task<List<OrganizationUnit>> GetOrganizationUnitsAsync(
var user = await GetAsync(id, cancellationToken: cancellationToken);
var organizationUnitIds = user.OrganizationUnits.Select(r => r.OrganizationUnitId);

return await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
return await (await GetQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToListAsync(cancellationToken);
}
Expand Down Expand Up @@ -290,7 +289,7 @@ public virtual async Task<List<IdentityUser>> GetUsersInOrganizationUnitAsync(
Guid organizationUnitId,
CancellationToken cancellationToken = default)
{
var result = await (await GetMongoQueryableAsync(cancellationToken))
var result = await (await GetQueryableAsync(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnitId))
.ToListAsync(GetCancellationToken(cancellationToken));
return result;
Expand All @@ -300,7 +299,7 @@ public virtual async Task<List<IdentityUser>> GetUsersInOrganizationsListAsync(
List<Guid> organizationUnitIds,
CancellationToken cancellationToken = default)
{
var result = await (await GetMongoQueryableAsync(cancellationToken))
var result = await (await GetQueryableAsync(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => organizationUnitIds.Contains(uou.OrganizationUnitId)))
.ToListAsync(GetCancellationToken(cancellationToken));
return result;
Expand All @@ -312,12 +311,12 @@ public virtual async Task<List<IdentityUser>> GetUsersInOrganizationUnitWithChil
{
cancellationToken = GetCancellationToken(cancellationToken);

var organizationUnitIds = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
var organizationUnitIds = await (await GetQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => ou.Code.StartsWith(code))
.Select(ou => ou.Id)
.ToListAsync(cancellationToken);

return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => organizationUnitIds.Contains(uou.OrganizationUnitId)))
.ToListAsync(cancellationToken);
}
Expand All @@ -328,7 +327,7 @@ public virtual async Task<IdentityUser> FindByTenantIdAndUserNameAsync(
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.FirstOrDefaultAsync(
u => u.TenantId == tenantId && u.UserName == userName,
GetCancellationToken(cancellationToken)
Expand All @@ -337,14 +336,14 @@ public virtual async Task<IdentityUser> FindByTenantIdAndUserNameAsync(

public virtual async Task<List<IdentityUser>> GetListByIdsAsync(IEnumerable<Guid> ids, bool includeDetails = false, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.Where(x => ids.Contains(x.Id))
.ToListAsync(GetCancellationToken(cancellationToken));
}

public virtual async Task UpdateRoleAsync(Guid sourceRoleId, Guid? targetRoleId, CancellationToken cancellationToken = default)
{
var users = await (await GetMongoQueryableAsync(cancellationToken))
var users = await (await GetQueryableAsync(cancellationToken))
.Where(x => x.Roles.Any(r => r.RoleId == sourceRoleId))
.ToListAsync(GetCancellationToken(cancellationToken));

Expand All @@ -362,7 +361,7 @@ public virtual async Task UpdateRoleAsync(Guid sourceRoleId, Guid? targetRoleId,

public virtual async Task UpdateOrganizationAsync(Guid sourceOrganizationId, Guid? targetOrganizationId, CancellationToken cancellationToken = default)
{
var users = await (await GetMongoQueryableAsync(cancellationToken))
var users = await (await GetQueryableAsync(cancellationToken))
.Where(x => x.OrganizationUnits.Any(r => r.OrganizationUnitId == sourceOrganizationId))
.ToListAsync(GetCancellationToken(cancellationToken));

Expand Down Expand Up @@ -394,16 +393,15 @@ public virtual async Task<List<IdentityUserIdWithRoleNames>> GetRoleNamesAsync(
var organizationUnitIds = userAndOrganizationUnitIds.SelectMany(x => x.Value);
var roleIds = userAndRoleIds.SelectMany(x => x.Value);

var organizationUnitAndRoleIds = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken)).Where(ou => organizationUnitIds.Contains(ou.Id))
.Select(userOrganizationUnit => new
{
var organizationUnitAndRoleIds = await (await GetQueryableAsync<OrganizationUnit>(cancellationToken)).Where(ou => organizationUnitIds.Contains(ou.Id))
.Select(userOrganizationUnit => new {
userOrganizationUnit.Id,
userOrganizationUnit.Roles
}).ToListAsync(cancellationToken: cancellationToken);
var allOrganizationUnitRoleIds = organizationUnitAndRoleIds.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToList();
var allRoleIds = roleIds.Union(allOrganizationUnitRoleIds);

var roles = await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).Select(r => new{ r.Id, r.Name }).ToListAsync(cancellationToken);
var roles = await (await GetQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).Select(r => new { r.Id, r.Name }).ToListAsync(cancellationToken);
var userRoles = userAndRoleIds.ToDictionary(x => x.Key, x => roles.Where(r => x.Value.Contains(r.Id)).Select(r => r.Name).ToArray());

var result = userRoles.Select(x => new IdentityUserIdWithRoleNames { Id = x.Key, RoleNames = x.Value }).ToList();
Expand All @@ -417,16 +415,16 @@ public virtual async Task<List<IdentityUserIdWithRoleNames>> GetRoleNamesAsync(
{
user.RoleNames = user.RoleNames.Union(roleNames).ToArray();
}
else if(roleNames.Any())
else if (roleNames.Any())
{
result.Add(new IdentityUserIdWithRoleNames { Id = userAndOrganizationUnitId.Key, RoleNames = roleNames});
result.Add(new IdentityUserIdWithRoleNames { Id = userAndOrganizationUnitId.Key, RoleNames = roleNames });
}
}

return result;
}

protected virtual async Task<IMongoQueryable<IdentityUser>> GetFilteredQueryableAsync(
protected virtual async Task<IQueryable<IdentityUser>> GetFilteredQueryableAsync(
string filter = null,
Guid? roleId = null,
Guid? organizationUnitId = null,
Expand All @@ -447,11 +445,11 @@ protected virtual async Task<IMongoQueryable<IdentityUser>> GetFilteredQueryable
CancellationToken cancellationToken = default)
{
var upperFilter = filter?.ToUpperInvariant();
var query = await GetMongoQueryableAsync(cancellationToken);
var query = await GetQueryableAsync(cancellationToken);

if (roleId.HasValue)
{
var organizationUnitIds = (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
var organizationUnitIds = (await GetQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => ou.Roles.Any(r => r.RoleId == roleId.Value))
.Select(userOrganizationUnit => userOrganizationUnit.Id)
.ToArray();
Expand All @@ -460,7 +458,7 @@ protected virtual async Task<IMongoQueryable<IdentityUser>> GetFilteredQueryable
}

return query
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
.WhereIf(
!filter.IsNullOrWhiteSpace(),
u =>
u.NormalizedUserName.Contains(upperFilter) ||
Expand All @@ -469,21 +467,21 @@ protected virtual async Task<IMongoQueryable<IdentityUser>> GetFilteredQueryable
(u.Surname != null && u.Surname.Contains(filter)) ||
(u.PhoneNumber != null && u.PhoneNumber.Contains(filter))
)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(organizationUnitId.HasValue, identityUser => identityUser.OrganizationUnits.Any(x => x.OrganizationUnitId == organizationUnitId.Value))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(!string.IsNullOrWhiteSpace(userName), x => x.UserName == userName)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(!string.IsNullOrWhiteSpace(phoneNumber), x => x.PhoneNumber == phoneNumber)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(!string.IsNullOrWhiteSpace(emailAddress), x => x.Email == emailAddress)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(!string.IsNullOrWhiteSpace(name), x => x.Name == name)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(!string.IsNullOrWhiteSpace(surname), x => x.Surname == surname)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(isLockedOut.HasValue && isLockedOut.Value, x => x.LockoutEnabled && x.LockoutEnd != null && x.LockoutEnd > DateTimeOffset.UtcNow)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(isLockedOut.HasValue && !isLockedOut.Value, x => !(x.LockoutEnabled && x.LockoutEnd != null && x.LockoutEnd > DateTimeOffset.UtcNow))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(notActive.HasValue, x => x.IsActive == !notActive.Value)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(emailConfirmed.HasValue, x => x.EmailConfirmed == emailConfirmed.Value)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(isExternal.HasValue, x => x.IsExternal == isExternal.Value)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(maxCreationTime != null, p => p.CreationTime <= maxCreationTime)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(minCreationTime != null, p => p.CreationTime >= minCreationTime)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(maxModifitionTime != null, p => p.LastModificationTime <= maxModifitionTime)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(minModifitionTime != null, p => p.LastModificationTime >= minModifitionTime)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(id.HasValue, x => x.Id == id);
.WhereIf(organizationUnitId.HasValue, identityUser => identityUser.OrganizationUnits.Any(x => x.OrganizationUnitId == organizationUnitId.Value))
.WhereIf(!string.IsNullOrWhiteSpace(userName), x => x.UserName == userName)
.WhereIf(!string.IsNullOrWhiteSpace(phoneNumber), x => x.PhoneNumber == phoneNumber)
.WhereIf(!string.IsNullOrWhiteSpace(emailAddress), x => x.Email == emailAddress)
.WhereIf(!string.IsNullOrWhiteSpace(name), x => x.Name == name)
.WhereIf(!string.IsNullOrWhiteSpace(surname), x => x.Surname == surname)
.WhereIf(isLockedOut.HasValue && isLockedOut.Value, x => x.LockoutEnabled && x.LockoutEnd != null && x.LockoutEnd > DateTimeOffset.UtcNow)
.WhereIf(isLockedOut.HasValue && !isLockedOut.Value, x => !(x.LockoutEnabled && x.LockoutEnd != null && x.LockoutEnd > DateTimeOffset.UtcNow))
.WhereIf(notActive.HasValue, x => x.IsActive == !notActive.Value)
.WhereIf(emailConfirmed.HasValue, x => x.EmailConfirmed == emailConfirmed.Value)
.WhereIf(isExternal.HasValue, x => x.IsExternal == isExternal.Value)
.WhereIf(maxCreationTime != null, p => p.CreationTime <= maxCreationTime)
.WhereIf(minCreationTime != null, p => p.CreationTime >= minCreationTime)
.WhereIf(maxModifitionTime != null, p => p.LastModificationTime <= maxModifitionTime)
.WhereIf(minModifitionTime != null, p => p.LastModificationTime >= minModifitionTime)
.WhereIf(id.HasValue, x => x.Id == id);
}
}
}

0 comments on commit 2f67c20

Please sign in to comment.