Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove org users who belong to no teams #24247

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ func DeleteTeam(t *organization.Team) error {
return err
}

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

@wxiaoguang wxiaoguang Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean that this "team member" doesn't belong to other teams in this org?

Hmm ... I see the logic in removeInvalidOrgUser, it counts first.

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 @@ -568,16 +574,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