mirror of
https://github.com/bitwarden/server.git
synced 2025-07-04 01:22:50 -05:00
[PM-18972] - Fix query for Org By User Domain (#5474)
* Changed query to avoid table scan. Added index to speed up query as well.
This commit is contained in:
@ -290,21 +290,33 @@ public class OrganizationRepository : Repository<Core.AdminConsole.Entities.Orga
|
||||
|
||||
public async Task<ICollection<Core.AdminConsole.Entities.Organization>> GetByVerifiedUserEmailDomainAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
using var scope = ServiceScopeFactory.CreateScope();
|
||||
|
||||
var query = from u in dbContext.Users
|
||||
join ou in dbContext.OrganizationUsers on u.Id equals ou.UserId
|
||||
join o in dbContext.Organizations on ou.OrganizationId equals o.Id
|
||||
join od in dbContext.OrganizationDomains on ou.OrganizationId equals od.OrganizationId
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var userQuery = from u in dbContext.Users
|
||||
where u.Id == userId
|
||||
&& od.VerifiedDate != null
|
||||
&& u.Email.ToLower().EndsWith("@" + od.DomainName.ToLower())
|
||||
select o;
|
||||
select u;
|
||||
|
||||
return await query.ToArrayAsync();
|
||||
var user = await userQuery.FirstOrDefaultAsync();
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
return new List<Core.AdminConsole.Entities.Organization>();
|
||||
}
|
||||
|
||||
var userWithDomain = new { UserId = user.Id, EmailDomain = user.Email.Split('@').Last() };
|
||||
|
||||
var query = from o in dbContext.Organizations
|
||||
join ou in dbContext.OrganizationUsers on o.Id equals ou.OrganizationId
|
||||
join od in dbContext.OrganizationDomains on ou.OrganizationId equals od.OrganizationId
|
||||
where ou.UserId == userWithDomain.UserId &&
|
||||
od.DomainName == userWithDomain.EmailDomain &&
|
||||
od.VerifiedDate != null &&
|
||||
o.Enabled == true
|
||||
select o;
|
||||
|
||||
return await query.ToArrayAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.AdminConsole.Entities.Organization>> GetAddableToProviderByUserIdAsync(
|
||||
|
Reference in New Issue
Block a user