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