Skip to content

Commit

Permalink
Remove org users who belong to no teams (#24247) (#24313)
Browse files Browse the repository at this point in the history
Backport #24247 by @yp05327

Fix #24128

Co-authored-by: yp05327 <[email protected]>
Co-authored-by: silverwind <[email protected]>
  • Loading branch information
3 people authored Apr 25, 2023
1 parent 8044d87 commit b1094ff
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@ func DeleteTeam(t *organization.Team) error {
return err
}

for _, tm := range t.Members {
if err := removeInvalidOrgUser(ctx, tm.ID, t.OrgID); err != nil {
return err
}
}

// Update organization number of teams.
if _, err := db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams-1 WHERE id=?", t.OrgID); err != nil {
return err
Expand Down Expand Up @@ -567,16 +573,19 @@ func removeTeamMember(ctx context.Context, team *organization.Team, userID int64
}
}

return removeInvalidOrgUser(ctx, userID, team.OrgID)
}

func removeInvalidOrgUser(ctx context.Context, userID, orgID int64) error {
// Check if the user is a member of any team in the organization.
if count, err := e.Count(&organization.TeamUser{
if count, err := db.GetEngine(ctx).Count(&organization.TeamUser{
UID: userID,
OrgID: team.OrgID,
OrgID: orgID,
}); err != nil {
return err
} else if count == 0 {
return removeOrgUser(ctx, team.OrgID, userID)
return removeOrgUser(ctx, orgID, userID)
}

return nil
}

Expand Down

0 comments on commit b1094ff

Please sign in to comment.