2022-06-06 10:01:49 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-06-06 10:01:49 +02:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2023-09-14 19:09:32 +02:00
|
|
|
"context"
|
|
|
|
|
2022-06-06 10:01:49 +02:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-12-14 02:39:05 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-06 10:01:49 +02:00
|
|
|
)
|
|
|
|
|
2024-12-14 02:39:05 +01:00
|
|
|
func CanUserForkBetweenOwners(id1, id2 int64) bool {
|
|
|
|
if id1 != id2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return setting.Repository.AllowForkIntoSameOwner
|
|
|
|
}
|
|
|
|
|
2022-06-06 10:01:49 +02:00
|
|
|
// CanUserForkRepo returns true if specified user can fork repository.
|
2023-09-14 19:09:32 +02:00
|
|
|
func CanUserForkRepo(ctx context.Context, user *user_model.User, repo *repo_model.Repository) (bool, error) {
|
2022-06-06 10:01:49 +02:00
|
|
|
if user == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
2024-12-14 02:39:05 +01:00
|
|
|
if CanUserForkBetweenOwners(repo.OwnerID, user.ID) && !repo_model.HasForkedRepo(ctx, user.ID, repo.ID) {
|
2022-06-06 10:01:49 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
2023-09-14 19:09:32 +02:00
|
|
|
ownedOrgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx, user.ID)
|
2022-06-06 10:01:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, org := range ownedOrgs {
|
2023-09-14 19:09:32 +02:00
|
|
|
if repo.OwnerID != org.ID && !repo_model.HasForkedRepo(ctx, org.ID, repo.ID) {
|
2022-06-06 10:01:49 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|