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