Skip to content

Commit ad19efc

Browse files
authored
[PM-22236] Fix invited accounts stuck in intermediate claimed status (#6810)
* Exclude invited users from claimed domain checks. These users should be excluded by the JOIN on UserId, but it's a known issue that some invited users have this FK set.
1 parent 8d30fbc commit ad19efc

11 files changed

Lines changed: 606 additions & 455 deletions

File tree

src/Core/AdminConsole/Repositories/IOrganizationRepository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public interface IOrganizationRepository : IRepository<Organization, Guid>
2121
Task<IEnumerable<string>> GetOwnerEmailAddressesById(Guid organizationId);
2222

2323
/// <summary>
24-
/// Gets the organizations that have a verified domain matching the user's email domain.
24+
/// Gets the organizations that have claimed the user's account. Currently, only one organization may claim a user.
25+
/// This requires that the organization has claimed the user's domain and the user is an organization member.
26+
/// It excludes invited members.
2527
/// </summary>
2628
Task<ICollection<Organization>> GetByVerifiedUserEmailDomainAsync(Guid userId);
2729

src/Infrastructure.EntityFramework/AdminConsole/Repositories/OrganizationRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ join od in dbContext.OrganizationDomains on ou.OrganizationId equals od.Organiza
325325
where ou.UserId == userWithDomain.UserId &&
326326
od.DomainName == userWithDomain.EmailDomain &&
327327
od.VerifiedDate != null &&
328-
o.Enabled == true
328+
o.Enabled == true &&
329+
ou.Status != OrganizationUserStatusType.Invited
329330
select o;
330331

331332
return await query.ToArrayAsync();

src/Infrastructure.EntityFramework/AdminConsole/Repositories/Queries/OrganizationUserReadByClaimedOrganizationDomainsQuery.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Bit.Core.Entities;
2+
using Bit.Core.Enums;
23

34
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
45

@@ -16,6 +17,7 @@ public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
1617
var query = from ou in dbContext.OrganizationUsers
1718
join u in dbContext.Users on ou.UserId equals u.Id
1819
where ou.OrganizationId == _organizationId
20+
&& ou.Status != OrganizationUserStatusType.Invited
1921
&& dbContext.OrganizationDomains
2022
.Any(od => od.OrganizationId == _organizationId &&
2123
od.VerifiedDate != null &&

src/Sql/dbo/Stored Procedures/OrganizationUser_ReadByOrganizationIdWithClaimedDomains_V2.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ BEGIN
88
SELECT *
99
FROM [dbo].[OrganizationUserView]
1010
WHERE [OrganizationId] = @OrganizationId
11+
AND [Status] != 0 -- Exclude invited users
1112
),
1213
UserDomains AS (
1314
SELECT U.[Id], U.[EmailDomain]
1415
FROM [dbo].[UserEmailDomainView] U
1516
WHERE EXISTS (
1617
SELECT 1
17-
FROM [dbo].[OrganizationDomainView] OD
18+
FROM [dbo].[OrganizationDomainView] OD
1819
WHERE OD.[OrganizationId] = @OrganizationId
1920
AND OD.[VerifiedDate] IS NOT NULL
2021
AND OD.[DomainName] = U.[EmailDomain]

src/Sql/dbo/Stored Procedures/Organization_ReadByClaimedUserEmailDomain.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ BEGIN
66

77
WITH CTE_User AS (
88
SELECT
9-
U.*,
9+
U.[Id],
1010
SUBSTRING(U.Email, CHARINDEX('@', U.Email) + 1, LEN(U.Email)) AS EmailDomain
1111
FROM dbo.[UserView] U
1212
WHERE U.[Id] = @UserId
@@ -19,4 +19,5 @@ BEGIN
1919
WHERE OD.[VerifiedDate] IS NOT NULL
2020
AND CU.EmailDomain = OD.[DomainName]
2121
AND O.[Enabled] = 1
22+
AND OU.[Status] != 0 -- Exclude invited users
2223
END

0 commit comments

Comments
 (0)