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