From 110fc57cbcf293c19ed7017d8ea528b4bbbd7396 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 12 Jun 2022 23:51:54 +0800 Subject: Move some code into models/git (#19879) * Move access and repo permission to models/perm/access * fix test * Move some git related files into sub package models/git * Fix build * fix git test * move lfs to sub package * move more git related functions to models/git * Move functions sequence * Some improvements per @KN4CK3R and @delvh --- models/repo/collaboration.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'models/repo') diff --git a/models/repo/collaboration.go b/models/repo/collaboration.go index 09397dd172..be05eba74c 100644 --- a/models/repo/collaboration.go +++ b/models/repo/collaboration.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/perm" + "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" @@ -149,3 +150,23 @@ func ChangeCollaborationAccessMode(repo *Repository, uid int64, mode perm.Access return committer.Commit() } + +// 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}) +} -- cgit v1.2.3