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.
|
2014-06-25 10:14:48 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
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"
|
2014-06-27 13:07:01 +05:30
|
|
|
"strings"
|
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"
|
2022-06-16 04:54:10 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-11 12:33:30 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2018-01-08 13:18:37 +05:30
|
|
|
|
2019-06-23 20:52:43 +05:30
|
|
|
"xorm.io/builder"
|
2014-06-27 13:07:01 +05:30
|
|
|
)
|
|
|
|
|
2021-06-14 17:48:09 +05:30
|
|
|
// MinimalOrg represents a simple orgnization with only needed columns
|
2022-03-29 11:59:02 +05:30
|
|
|
type MinimalOrg = organization.Organization
|
2021-06-14 17:48:09 +05:30
|
|
|
|
|
|
|
// GetUserOrgsList returns one user's all orgs list
|
2021-11-24 15:19:20 +05:30
|
|
|
func GetUserOrgsList(user *user_model.User) ([]*MinimalOrg, error) {
|
|
|
|
schema, err := db.TableInfo(new(user_model.User))
|
2021-09-01 11:01:42 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
outputCols := []string{
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"full_name",
|
|
|
|
"visibility",
|
|
|
|
"avatar",
|
|
|
|
"avatar_email",
|
|
|
|
"use_custom_avatar",
|
|
|
|
}
|
|
|
|
|
|
|
|
groupByCols := &strings.Builder{}
|
|
|
|
for _, col := range outputCols {
|
|
|
|
fmt.Fprintf(groupByCols, "`%s`.%s,", schema.Name, col)
|
|
|
|
}
|
|
|
|
groupByStr := groupByCols.String()
|
|
|
|
groupByStr = groupByStr[0 : len(groupByStr)-1]
|
|
|
|
|
2021-11-21 21:11:00 +05:30
|
|
|
sess := db.GetEngine(db.DefaultContext)
|
|
|
|
sess = sess.Select(groupByStr+", count(distinct repo_id) as org_count").
|
2021-06-14 17:48:09 +05:30
|
|
|
Table("user").
|
2021-09-01 11:01:42 +05:30
|
|
|
Join("INNER", "team", "`team`.org_id = `user`.id").
|
|
|
|
Join("INNER", "team_user", "`team`.id = `team_user`.team_id").
|
|
|
|
Join("LEFT", builder.
|
|
|
|
Select("id as repo_id, owner_id as repo_owner_id").
|
|
|
|
From("repository").
|
2022-06-16 04:54:10 +05:30
|
|
|
Where(repo_model.AccessibleRepositoryCondition(user, unit.TypeInvalid)), "`repository`.repo_owner_id = `team`.org_id").
|
2021-09-01 11:01:42 +05:30
|
|
|
Where("`team_user`.uid = ?", user.ID).
|
|
|
|
GroupBy(groupByStr)
|
|
|
|
|
|
|
|
type OrgCount struct {
|
2022-03-29 11:59:02 +05:30
|
|
|
organization.Organization `xorm:"extends"`
|
|
|
|
OrgCount int
|
2021-09-01 11:01:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
orgCounts := make([]*OrgCount, 0, 10)
|
|
|
|
|
|
|
|
if err := sess.
|
|
|
|
Asc("`user`.name").
|
|
|
|
Find(&orgCounts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
orgs := make([]*MinimalOrg, len(orgCounts))
|
|
|
|
for i, orgCount := range orgCounts {
|
2021-11-19 17:11:40 +05:30
|
|
|
orgCount.Organization.NumRepos = orgCount.OrgCount
|
|
|
|
orgs[i] = &orgCount.Organization
|
2021-09-01 11:01:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return orgs, nil
|
2021-06-14 17:48:09 +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 {
|
2015-03-18 07:21:39 +05:30
|
|
|
return fmt.Errorf("get org-user: %v", 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 {
|
2016-07-24 12:02:46 +05:30
|
|
|
return fmt.Errorf("GetUserByID [%d]: %v", 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 {
|
|
|
|
return fmt.Errorf("AccessibleReposEnv: %v", err)
|
|
|
|
}
|
|
|
|
repoIDs, err := env.RepoIDs(1, org.NumRepos)
|
|
|
|
if err != nil {
|
2017-01-24 21:46:36 +05:30
|
|
|
return fmt.Errorf("GetUserRepositories [%d]: %v", 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 {
|
2021-11-19 17:11:40 +05:30
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
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
|
|
|
}
|