2022-05-11 04:09:36 -06:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-05-11 04:09:36 -06:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-06-12 09:51:54 -06:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2022-05-11 04:09:36 -06:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Collaboration represent the relation between an individual and a repository.
|
|
|
|
type Collaboration struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
|
|
|
UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
|
|
|
Mode perm.AccessMode `xorm:"DEFAULT 2 NOT NULL"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Collaboration))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collaborator represents a user with collaboration details.
|
|
|
|
type Collaborator struct {
|
|
|
|
*user_model.User
|
|
|
|
Collaboration *Collaboration
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCollaborators returns the collaborators for a repository
|
|
|
|
func GetCollaborators(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*Collaborator, error) {
|
2022-05-20 08:08:52 -06:00
|
|
|
collaborations, err := getCollaborations(ctx, repoID, listOptions)
|
2022-05-11 04:09:36 -06:00
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return nil, fmt.Errorf("getCollaborations: %w", err)
|
2022-05-11 04:09:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
collaborators := make([]*Collaborator, 0, len(collaborations))
|
|
|
|
for _, c := range collaborations {
|
2022-12-02 19:48:26 -07:00
|
|
|
user, err := user_model.GetUserByID(ctx, c.UserID)
|
2022-05-11 04:09:36 -06:00
|
|
|
if err != nil {
|
|
|
|
if user_model.IsErrUserNotExist(err) {
|
|
|
|
log.Warn("Inconsistent DB: User: %d is listed as collaborator of %-v but does not exist", c.UserID, repoID)
|
|
|
|
user = user_model.NewGhostUser()
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
collaborators = append(collaborators, &Collaborator{
|
|
|
|
User: user,
|
|
|
|
Collaboration: c,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return collaborators, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountCollaborators returns total number of collaborators for a repository
|
|
|
|
func CountCollaborators(repoID int64) (int64, error) {
|
|
|
|
return db.GetEngine(db.DefaultContext).Where("repo_id = ? ", repoID).Count(&Collaboration{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCollaboration get collaboration for a repository id with a user id
|
|
|
|
func GetCollaboration(ctx context.Context, repoID, uid int64) (*Collaboration, error) {
|
|
|
|
collaboration := &Collaboration{
|
|
|
|
RepoID: repoID,
|
|
|
|
UserID: uid,
|
|
|
|
}
|
|
|
|
has, err := db.GetEngine(ctx).Get(collaboration)
|
|
|
|
if !has {
|
|
|
|
collaboration = nil
|
|
|
|
}
|
|
|
|
return collaboration, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCollaborator check if a user is a collaborator of a repository
|
|
|
|
func IsCollaborator(ctx context.Context, repoID, userID int64) (bool, error) {
|
|
|
|
return db.GetEngine(ctx).Get(&Collaboration{RepoID: repoID, UserID: userID})
|
|
|
|
}
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
func getCollaborations(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*Collaboration, error) {
|
2022-05-11 04:09:36 -06:00
|
|
|
if listOptions.Page == 0 {
|
|
|
|
collaborations := make([]*Collaboration, 0, 8)
|
2022-05-20 08:08:52 -06:00
|
|
|
return collaborations, db.GetEngine(ctx).Find(&collaborations, &Collaboration{RepoID: repoID})
|
2022-05-11 04:09:36 -06:00
|
|
|
}
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
e := db.GetEngine(ctx)
|
|
|
|
|
2022-05-11 04:09:36 -06:00
|
|
|
e = db.SetEnginePagination(e, &listOptions)
|
|
|
|
|
|
|
|
collaborations := make([]*Collaboration, 0, listOptions.PageSize)
|
|
|
|
return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repoID})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeCollaborationAccessMode sets new access mode for the collaboration.
|
2022-12-09 19:46:31 -07:00
|
|
|
func ChangeCollaborationAccessMode(ctx context.Context, repo *Repository, uid int64, mode perm.AccessMode) error {
|
2022-05-11 04:09:36 -06:00
|
|
|
// Discard invalid input
|
|
|
|
if mode <= perm.AccessModeNone || mode > perm.AccessModeOwner {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-07 18:34:58 -07:00
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
2022-12-09 19:46:31 -07:00
|
|
|
e := db.GetEngine(ctx)
|
2022-05-11 04:09:36 -06:00
|
|
|
|
2022-12-09 19:46:31 -07:00
|
|
|
collaboration := &Collaboration{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
UserID: uid,
|
|
|
|
}
|
|
|
|
has, err := e.Get(collaboration)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get collaboration: %w", err)
|
|
|
|
} else if !has {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-11 04:09:36 -06:00
|
|
|
|
2022-12-09 19:46:31 -07:00
|
|
|
if collaboration.Mode == mode {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
collaboration.Mode = mode
|
|
|
|
|
|
|
|
if _, err = e.
|
|
|
|
ID(collaboration.ID).
|
|
|
|
Cols("mode").
|
|
|
|
Update(collaboration); err != nil {
|
|
|
|
return fmt.Errorf("update collaboration: %w", err)
|
|
|
|
} else if _, err = e.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
|
|
|
|
return fmt.Errorf("update access table: %w", err)
|
|
|
|
}
|
2022-05-11 04:09:36 -06:00
|
|
|
|
2022-12-09 19:46:31 -07:00
|
|
|
return nil
|
|
|
|
})
|
2022-05-11 04:09:36 -06:00
|
|
|
}
|
2022-06-12 09:51:54 -06:00
|
|
|
|
|
|
|
// IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository
|
|
|
|
func IsOwnerMemberCollaborator(repo *Repository, userID int64) (bool, error) {
|
|
|
|
if repo.OwnerID == userID {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
teamMember, err := db.GetEngine(db.DefaultContext).Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id").
|
|
|
|
Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id").
|
|
|
|
Where("team_repo.repo_id = ?", repo.ID).
|
|
|
|
And("team_unit.`type` = ?", unit.TypeCode).
|
|
|
|
And("team_user.uid = ?", userID).Table("team_user").Exist()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if teamMember {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.GetEngine(db.DefaultContext).Get(&Collaboration{RepoID: repo.ID, UserID: userID})
|
|
|
|
}
|