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.

collaborator.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repository
  5. import (
  6. "context"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/perm"
  9. access_model "code.gitea.io/gitea/models/perm/access"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. user_model "code.gitea.io/gitea/models/user"
  12. )
  13. func addCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_model.User) error {
  14. collaboration := &repo_model.Collaboration{
  15. RepoID: repo.ID,
  16. UserID: u.ID,
  17. }
  18. has, err := db.GetByBean(ctx, collaboration)
  19. if err != nil {
  20. return err
  21. } else if has {
  22. return nil
  23. }
  24. collaboration.Mode = perm.AccessModeWrite
  25. if err = db.Insert(ctx, collaboration); err != nil {
  26. return err
  27. }
  28. return access_model.RecalculateUserAccess(ctx, repo, u.ID)
  29. }
  30. // AddCollaborator adds new collaboration to a repository with default access mode.
  31. func AddCollaborator(repo *repo_model.Repository, u *user_model.User) error {
  32. return db.WithTx(func(ctx context.Context) error {
  33. return addCollaborator(ctx, repo, u)
  34. })
  35. }