Skip to content

Commit c1cc257

Browse files
committed
feat: remove users from deleted organizations in idp org sync
Idp sync should exclude deleted organizations and auto remove members. They should not be members in the first place.
1 parent 6046b56 commit c1cc257

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

coderd/database/dbmem/dbmem.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -4172,7 +4172,11 @@ func (q *FakeQuerier) GetOrganizationsByUserID(_ context.Context, arg database.G
41724172
continue
41734173
}
41744174
for _, organization := range q.organizations {
4175-
if organization.ID != organizationMember.OrganizationID || organization.Deleted != arg.Deleted {
4175+
if organization.ID != organizationMember.OrganizationID {
4176+
continue
4177+
}
4178+
4179+
if arg.Deleted.Valid && organization.Deleted != arg.Deleted.Bool {
41764180
continue
41774181
}
41784182
organizations = append(organizations, organization)

coderd/database/queries.sql.go

+8-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/organizations.sql

+7-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ SELECT
5555
FROM
5656
organizations
5757
WHERE
58-
-- Optionally include deleted organizations
59-
deleted = @deleted AND
58+
-- Optionally provide a filter for deleted organizations.
59+
CASE WHEN
60+
sqlc.narg('deleted') :: boolean IS NULL THEN
61+
true
62+
ELSE
63+
deleted = sqlc.narg('deleted')
64+
END AND
6065
id = ANY(
6166
SELECT
6267
organization_id

coderd/idpsync/organization.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u
9292
return nil // No sync configured, nothing to do
9393
}
9494

95-
expectedOrgs, err := orgSettings.ParseClaims(ctx, tx, params.MergedClaims)
95+
expectedOrgIDs, err := orgSettings.ParseClaims(ctx, tx, params.MergedClaims)
9696
if err != nil {
9797
return xerrors.Errorf("organization claims: %w", err)
9898
}
9999

100+
// Fetch all organizations, even deleted ones. This is to remove a user
101+
// from any deleted organizations they may be in.
100102
existingOrgs, err := tx.GetOrganizationsByUserID(ctx, database.GetOrganizationsByUserIDParams{
101103
UserID: user.ID,
102-
Deleted: false,
104+
Deleted: sql.NullBool{},
103105
})
104106
if err != nil {
105107
return xerrors.Errorf("failed to get user organizations: %w", err)
@@ -109,9 +111,22 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u
109111
return org.ID
110112
})
111113

114+
expectedOrganizations, err := tx.GetOrganizations(ctx, database.GetOrganizationsParams{
115+
IDs: expectedOrgIDs,
116+
// Do not include deleted organizations
117+
Deleted: false,
118+
})
119+
if err != nil {
120+
return xerrors.Errorf("failed to get expected organizations: %w", err)
121+
}
122+
123+
finalExpected := db2sdk.List(expectedOrganizations, func(org database.Organization) uuid.UUID {
124+
return org.ID
125+
})
126+
112127
// Find the difference in the expected and the existing orgs, and
113128
// correct the set of orgs the user is a member of.
114-
add, remove := slice.SymmetricDifference(existingOrgIDs, expectedOrgs)
129+
add, remove := slice.SymmetricDifference(existingOrgIDs, finalExpected)
115130
notExists := make([]uuid.UUID, 0)
116131
for _, orgID := range add {
117132
_, err := tx.InsertOrganizationMember(ctx, database.InsertOrganizationMemberParams{

coderd/users.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ func (api *API) organizationsByUser(rw http.ResponseWriter, r *http.Request) {
13401340

13411341
organizations, err := api.Database.GetOrganizationsByUserID(ctx, database.GetOrganizationsByUserIDParams{
13421342
UserID: user.ID,
1343-
Deleted: false,
1343+
Deleted: sql.NullBool{Bool: false, Valid: true},
13441344
})
13451345
if errors.Is(err, sql.ErrNoRows) {
13461346
err = nil

0 commit comments

Comments
 (0)