You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/organization"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. user_model "code.gitea.io/gitea/models/user"
  9. )
  10. // CanUserForkRepo returns true if specified user can fork repository.
  11. func CanUserForkRepo(ctx context.Context, user *user_model.User, repo *repo_model.Repository) (bool, error) {
  12. if user == nil {
  13. return false, nil
  14. }
  15. if repo.OwnerID != user.ID && !repo_model.HasForkedRepo(ctx, user.ID, repo.ID) {
  16. return true, nil
  17. }
  18. ownedOrgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx, user.ID)
  19. if err != nil {
  20. return false, err
  21. }
  22. for _, org := range ownedOrgs {
  23. if repo.OwnerID != org.ID && !repo_model.HasForkedRepo(ctx, org.ID, repo.ID) {
  24. return true, nil
  25. }
  26. }
  27. return false, nil
  28. }