2019-08-02 11:06:28 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2022-03-29 01:29:02 -05:00
|
|
|
"context"
|
2019-08-02 11:06:28 -05:00
|
|
|
"fmt"
|
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 01:29:02 -05:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-24 03:49:20 -06:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-08-02 11:06:28 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IsUserOrgOwner returns true if user is in the owner team of given organization.
|
2021-11-28 08:11:58 -06:00
|
|
|
func IsUserOrgOwner(users user_model.UserList, orgID int64) map[int64]bool {
|
2019-08-02 11:06:28 -05:00
|
|
|
results := make(map[int64]bool, len(users))
|
|
|
|
for _, user := range users {
|
2021-03-14 13:52:12 -05:00
|
|
|
results[user.ID] = false // Set default to false
|
2019-08-02 11:06:28 -05:00
|
|
|
}
|
2022-03-29 01:29:02 -05:00
|
|
|
ownerMaps, err := loadOrganizationOwners(db.DefaultContext, users, orgID)
|
2019-08-02 11:06:28 -05:00
|
|
|
if err == nil {
|
|
|
|
for _, owner := range ownerMaps {
|
|
|
|
results[owner.UID] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2022-03-29 01:29:02 -05:00
|
|
|
func loadOrganizationOwners(ctx context.Context, users user_model.UserList, orgID int64) (map[int64]*organization.TeamUser, error) {
|
2019-08-02 11:06:28 -05:00
|
|
|
if len(users) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-03-29 01:29:02 -05:00
|
|
|
ownerTeam, err := organization.GetOwnerTeam(ctx, orgID)
|
2019-08-02 11:06:28 -05:00
|
|
|
if err != nil {
|
2022-03-29 01:29:02 -05:00
|
|
|
if organization.IsErrTeamNotExist(err) {
|
2019-08-02 11:06:28 -05:00
|
|
|
log.Error("Organization does not have owner team: %d", orgID)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-28 08:11:58 -06:00
|
|
|
userIDs := users.GetUserIDs()
|
2022-03-29 01:29:02 -05:00
|
|
|
ownerMaps := make(map[int64]*organization.TeamUser)
|
|
|
|
err = db.GetEngine(ctx).In("uid", userIDs).
|
2019-08-02 11:06:28 -05:00
|
|
|
And("org_id=?", orgID).
|
|
|
|
And("team_id=?", ownerTeam.ID).
|
|
|
|
Find(&ownerMaps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("find team users: %v", err)
|
|
|
|
}
|
|
|
|
return ownerMaps, nil
|
|
|
|
}
|