2019-08-02 18:06:28 +02: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 (
|
|
|
|
"fmt"
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-08-02 18:06:28 +02:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IsUserOrgOwner returns true if user is in the owner team of given organization.
|
2021-11-28 15:11:58 +01:00
|
|
|
func IsUserOrgOwner(users user_model.UserList, orgID int64) map[int64]bool {
|
2019-08-02 18:06:28 +02:00
|
|
|
results := make(map[int64]bool, len(users))
|
|
|
|
for _, user := range users {
|
2021-03-14 19:52:12 +01:00
|
|
|
results[user.ID] = false // Set default to false
|
2019-08-02 18:06:28 +02:00
|
|
|
}
|
2021-11-28 15:11:58 +01:00
|
|
|
ownerMaps, err := loadOrganizationOwners(db.GetEngine(db.DefaultContext), users, orgID)
|
2019-08-02 18:06:28 +02:00
|
|
|
if err == nil {
|
|
|
|
for _, owner := range ownerMaps {
|
|
|
|
results[owner.UID] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2021-11-28 15:11:58 +01:00
|
|
|
func loadOrganizationOwners(e db.Engine, users user_model.UserList, orgID int64) (map[int64]*TeamUser, error) {
|
2019-08-02 18:06:28 +02:00
|
|
|
if len(users) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ownerTeam, err := getOwnerTeam(e, orgID)
|
|
|
|
if err != nil {
|
2019-09-23 22:08:03 +02:00
|
|
|
if IsErrTeamNotExist(err) {
|
2019-08-02 18:06:28 +02:00
|
|
|
log.Error("Organization does not have owner team: %d", orgID)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-28 15:11:58 +01:00
|
|
|
userIDs := users.GetUserIDs()
|
2019-08-02 18:06:28 +02:00
|
|
|
ownerMaps := make(map[int64]*TeamUser)
|
|
|
|
err = e.In("uid", userIDs).
|
|
|
|
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
|
|
|
|
}
|