mirror of
https://github.com/bitwarden/server.git
synced 2025-04-04 20:50:21 -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:
parent
bea0d0d76f
commit
6cb97d9bf9
@ -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(
|
||||
|
@ -4,12 +4,19 @@ AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON;
|
||||
|
||||
WITH CTE_User AS (
|
||||
SELECT
|
||||
U.*,
|
||||
SUBSTRING(U.Email, CHARINDEX('@', U.Email) + 1, LEN(U.Email)) AS EmailDomain
|
||||
FROM dbo.[UserView] U
|
||||
WHERE U.[Id] = @UserId
|
||||
)
|
||||
SELECT O.*
|
||||
FROM [dbo].[UserView] U
|
||||
INNER JOIN [dbo].[OrganizationUserView] OU ON U.[Id] = OU.[UserId]
|
||||
INNER JOIN [dbo].[OrganizationView] O ON OU.[OrganizationId] = O.[Id]
|
||||
INNER JOIN [dbo].[OrganizationDomainView] OD ON OU.[OrganizationId] = OD.[OrganizationId]
|
||||
WHERE U.[Id] = @UserId
|
||||
AND OD.[VerifiedDate] IS NOT NULL
|
||||
AND U.[Email] LIKE '%@' + OD.[DomainName];
|
||||
FROM CTE_User CU
|
||||
INNER JOIN dbo.[OrganizationUserView] OU ON CU.[Id] = OU.[UserId]
|
||||
INNER JOIN dbo.[OrganizationView] O ON OU.[OrganizationId] = O.[Id]
|
||||
INNER JOIN dbo.[OrganizationDomainView] OD ON OU.[OrganizationId] = OD.[OrganizationId]
|
||||
WHERE OD.[VerifiedDate] IS NOT NULL
|
||||
AND CU.EmailDomain = OD.[DomainName]
|
||||
AND O.[Enabled] = 1
|
||||
END
|
||||
|
@ -22,3 +22,8 @@ CREATE NONCLUSTERED INDEX [IX_OrganizationDomain_VerifiedDate]
|
||||
ON [dbo].[OrganizationDomain] ([VerifiedDate])
|
||||
INCLUDE ([OrganizationId],[DomainName]);
|
||||
GO
|
||||
|
||||
CREATE NONCLUSTERED INDEX [IX_OrganizationDomain_DomainNameVerifiedDateOrganizationId]
|
||||
ON [dbo].[OrganizationDomain] ([DomainName],[VerifiedDate])
|
||||
INCLUDE ([OrganizationId])
|
||||
GO
|
||||
|
@ -0,0 +1,31 @@
|
||||
CREATE OR ALTER PROCEDURE [dbo].[Organization_ReadByClaimedUserEmailDomain]
|
||||
@UserId UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON;
|
||||
|
||||
WITH CTE_User AS (
|
||||
SELECT
|
||||
U.*,
|
||||
SUBSTRING(U.Email, CHARINDEX('@', U.Email) + 1, LEN(U.Email)) AS EmailDomain
|
||||
FROM dbo.[UserView] U
|
||||
WHERE U.[Id] = @UserId
|
||||
)
|
||||
SELECT O.*
|
||||
FROM CTE_User CU
|
||||
INNER JOIN dbo.[OrganizationUserView] OU ON CU.[Id] = OU.[UserId]
|
||||
INNER JOIN dbo.[OrganizationView] O ON OU.[OrganizationId] = O.[Id]
|
||||
INNER JOIN dbo.[OrganizationDomainView] OD ON OU.[OrganizationId] = OD.[OrganizationId]
|
||||
WHERE OD.[VerifiedDate] IS NOT NULL
|
||||
AND CU.EmailDomain = OD.[DomainName]
|
||||
AND O.[Enabled] = 1
|
||||
END
|
||||
GO
|
||||
|
||||
IF NOT EXISTS(SELECT name FROM sys.indexes WHERE name = 'IX_OrganizationDomain_DomainNameVerifiedDateOrganizationId')
|
||||
BEGIN
|
||||
CREATE NONCLUSTERED INDEX [IX_OrganizationDomain_DomainNameVerifiedDateOrganizationId]
|
||||
ON [dbo].[OrganizationDomain] ([DomainName],[VerifiedDate])
|
||||
INCLUDE ([OrganizationId])
|
||||
END
|
||||
GO
|
Loading…
x
Reference in New Issue
Block a user