mirror of
https://github.com/go-gitea/gitea
synced 2024-12-22 21:07:52 +01:00
76659b1114
Part of #27065 This reduces the usage of `db.DefaultContext`. I think I've got enough files for the first PR. When this is merged, I will continue working on this. Considering how many files this PR affect, I hope it won't take to long to merge, so I don't end up in the merge conflict hell. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
33 lines
873 B
Go
33 lines
873 B
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"code.gitea.io/gitea/models/organization"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
)
|
|
|
|
// CanUserForkRepo returns true if specified user can fork repository.
|
|
func CanUserForkRepo(ctx context.Context, user *user_model.User, repo *repo_model.Repository) (bool, error) {
|
|
if user == nil {
|
|
return false, nil
|
|
}
|
|
if repo.OwnerID != user.ID && !repo_model.HasForkedRepo(ctx, user.ID, repo.ID) {
|
|
return true, nil
|
|
}
|
|
ownedOrgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx, user.ID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
for _, org := range ownedOrgs {
|
|
if repo.OwnerID != org.ID && !repo_model.HasForkedRepo(ctx, org.ID, repo.ID) {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|