diff options
author | Giteabot <teabot@gitea.io> | 2023-04-24 22:50:32 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-24 22:50:32 -0400 |
commit | b1094ff28c3412b27d572dff6f19997c8f155f87 (patch) | |
tree | c6088389a36f684ff4c16bdabb78ccaa87c6a56e | |
parent | 8044d87c18c911e55552b2b24ab8b38fca008c3e (diff) | |
download | gitea-b1094ff28c3412b27d572dff6f19997c8f155f87.tar.gz gitea-b1094ff28c3412b27d572dff6f19997c8f155f87.zip |
Remove org users who belong to no teams (#24247) (#24313)
Backport #24247 by @yp05327
Fix #24128
Co-authored-by: yp05327 <576951401@qq.com>
Co-authored-by: silverwind <me@silverwind.io>
-rw-r--r-- | models/org_team.go | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/models/org_team.go b/models/org_team.go index be3b63b52e..eea704daad 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -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 @@ -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 } |