2014-06-24 22:44:48 -06:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-02-18 09:00:27 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2014-06-24 22:44:48 -06:00
|
|
|
|
|
|
|
package models
|
|
|
|
|
2014-06-27 01:37:01 -06:00
|
|
|
import (
|
2021-11-18 10:42:27 -07:00
|
|
|
"context"
|
2015-02-23 00:15:53 -07:00
|
|
|
"fmt"
|
2015-09-07 11:58:23 -06:00
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 00:29:02 -06:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-05-11 04:09:36 -06:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2014-06-27 01:37:01 -06:00
|
|
|
)
|
|
|
|
|
2023-11-03 08:01:45 -06:00
|
|
|
// RemoveOrgUser removes user from given organization.
|
|
|
|
func RemoveOrgUser(ctx context.Context, orgID, userID int64) error {
|
2022-03-29 00:29:02 -06:00
|
|
|
ou := new(organization.OrgUser)
|
2014-08-15 04:29:41 -06:00
|
|
|
|
2023-11-03 08:01:45 -06:00
|
|
|
has, err := db.GetEngine(ctx).
|
2016-11-10 08:16:32 -07:00
|
|
|
Where("uid=?", userID).
|
|
|
|
And("org_id=?", orgID).
|
|
|
|
Get(ou)
|
2014-08-15 04:29:41 -06:00
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("get org-user: %w", err)
|
2014-08-15 04:29:41 -06:00
|
|
|
} else if !has {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
org, err := organization.GetOrgByID(ctx, orgID)
|
2014-08-24 07:09:05 -06:00
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("GetUserByID [%d]: %w", orgID, err)
|
2014-08-24 07:09:05 -06:00
|
|
|
}
|
2016-07-24 00:32:46 -06:00
|
|
|
|
2014-08-16 02:21:17 -06:00
|
|
|
// Check if the user to delete is the last member in owner team.
|
2022-03-29 00:29:02 -06:00
|
|
|
if isOwner, err := organization.IsOrganizationOwner(ctx, orgID, userID); err != nil {
|
2017-12-21 00:43:26 -07:00
|
|
|
return err
|
|
|
|
} else if isOwner {
|
2022-03-29 00:29:02 -06:00
|
|
|
t, err := organization.GetOwnerTeam(ctx, org.ID)
|
2014-08-16 02:21:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if t.NumMembers == 1 {
|
2022-12-02 19:48:26 -07:00
|
|
|
if err := t.LoadMembers(ctx); err != nil {
|
2017-12-12 15:26:31 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if t.Members[0].ID == userID {
|
2022-03-29 00:29:02 -06:00
|
|
|
return organization.ErrLastOrgOwner{UID: userID}
|
2017-12-12 15:26:31 -07:00
|
|
|
}
|
2014-08-16 02:21:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 08:01:45 -06:00
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
|
|
|
if _, err := db.GetEngine(ctx).ID(ou.ID).Delete(ou); err != nil {
|
2014-08-15 04:29:41 -06:00
|
|
|
return err
|
2022-05-20 08:08:52 -06:00
|
|
|
} else if _, err = db.Exec(ctx, "UPDATE `user` SET num_members=num_members-1 WHERE id=?", orgID); err != nil {
|
2014-08-15 04:29:41 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-24 00:32:46 -06:00
|
|
|
// Delete all repository accesses and unwatch them.
|
2022-03-29 00:29:02 -06:00
|
|
|
env, err := organization.AccessibleReposEnv(ctx, org, userID)
|
2017-01-25 08:41:38 -07:00
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("AccessibleReposEnv: %w", err)
|
2017-01-25 08:41:38 -07:00
|
|
|
}
|
|
|
|
repoIDs, err := env.RepoIDs(1, org.NumRepos)
|
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("GetUserRepositories [%d]: %w", userID, err)
|
2017-01-25 08:41:38 -07:00
|
|
|
}
|
|
|
|
for _, repoID := range repoIDs {
|
2022-05-20 08:08:52 -06:00
|
|
|
if err = repo_model.WatchRepo(ctx, userID, repoID, false); err != nil {
|
2014-08-27 02:39:36 -06:00
|
|
|
return err
|
2014-08-24 07:09:05 -06:00
|
|
|
}
|
|
|
|
}
|
2016-08-10 12:06:51 -06:00
|
|
|
|
|
|
|
if len(repoIDs) > 0 {
|
2023-11-03 08:01:45 -06:00
|
|
|
if _, err = db.GetEngine(ctx).
|
2017-01-24 09:16:36 -07:00
|
|
|
Where("user_id = ?", userID).
|
2016-11-10 08:16:32 -07:00
|
|
|
In("repo_id", repoIDs).
|
2022-05-11 04:09:36 -06:00
|
|
|
Delete(new(access_model.Access)); err != nil {
|
2016-08-10 12:06:51 -06:00
|
|
|
return err
|
|
|
|
}
|
2016-07-24 00:32:46 -06:00
|
|
|
}
|
2014-08-24 07:09:05 -06:00
|
|
|
|
2022-06-25 16:50:12 -06:00
|
|
|
// Delete member in their teams.
|
2022-03-29 00:29:02 -06:00
|
|
|
teams, err := organization.GetUserOrgTeams(ctx, org.ID, userID)
|
2014-08-24 07:09:05 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-17 19:51:39 -06:00
|
|
|
for _, t := range teams {
|
2021-11-19 04:41:40 -07:00
|
|
|
if err = removeTeamMember(ctx, t, userID); err != nil {
|
2014-08-24 07:09:05 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 04:41:40 -07:00
|
|
|
return committer.Commit()
|
2014-08-15 04:29:41 -06:00
|
|
|
}
|