Skip to content

Commit 8cc60ee

Browse files
yp05327silverwind
andauthored
Remove org users who belong to no teams (#24247)
Fix #24128 --------- Co-authored-by: silverwind <me@silverwind.io>
1 parent fe007b6 commit 8cc60ee

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

models/org_team.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,12 @@ func DeleteTeam(t *organization.Team) error {
419419
return err
420420
}
421421

422+
for _, tm := range t.Members {
423+
if err := removeInvalidOrgUser(ctx, tm.ID, t.OrgID); err != nil {
424+
return err
425+
}
426+
}
427+
422428
// Update organization number of teams.
423429
if _, err := db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams-1 WHERE id=?", t.OrgID); err != nil {
424430
return err
@@ -568,16 +574,19 @@ func removeTeamMember(ctx context.Context, team *organization.Team, userID int64
568574
}
569575
}
570576

577+
return removeInvalidOrgUser(ctx, userID, team.OrgID)
578+
}
579+
580+
func removeInvalidOrgUser(ctx context.Context, userID, orgID int64) error {
571581
// Check if the user is a member of any team in the organization.
572-
if count, err := e.Count(&organization.TeamUser{
582+
if count, err := db.GetEngine(ctx).Count(&organization.TeamUser{
573583
UID: userID,
574-
OrgID: team.OrgID,
584+
OrgID: orgID,
575585
}); err != nil {
576586
return err
577587
} else if count == 0 {
578-
return removeOrgUser(ctx, team.OrgID, userID)
588+
return removeOrgUser(ctx, orgID, userID)
579589
}
580-
581590
return nil
582591
}
583592

0 commit comments

Comments
 (0)