747be949a1
- Ensure that the doer and blocked user cannot add each other as collaborators to repositories. - The Web UI gets an detailed message of the specific situation, the API gets an generic Forbidden code. - Unit tests has been added. - Integration testing for Web and API has been added. - This commit doesn't introduce removing each other as collaborators on the block action, due to the complexity of database calls that needs to be figured out. That deserves its own commit and test code.
41 lines
1 KiB
Go
41 lines
1 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/models/perm"
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
)
|
|
|
|
func AddCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_model.User) error {
|
|
if user_model.IsBlocked(ctx, repo.OwnerID, u.ID) || user_model.IsBlocked(ctx, u.ID, repo.OwnerID) {
|
|
return user_model.ErrBlockedByUser
|
|
}
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
collaboration := &repo_model.Collaboration{
|
|
RepoID: repo.ID,
|
|
UserID: u.ID,
|
|
}
|
|
|
|
has, err := db.GetByBean(ctx, collaboration)
|
|
if err != nil {
|
|
return err
|
|
} else if has {
|
|
return nil
|
|
}
|
|
collaboration.Mode = perm.AccessModeWrite
|
|
|
|
if err = db.Insert(ctx, collaboration); err != nil {
|
|
return err
|
|
}
|
|
|
|
return access_model.RecalculateUserAccess(ctx, repo, u.ID)
|
|
})
|
|
}
|