summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-05-11 18:09:36 +0800
committerGitHub <noreply@github.com>2022-05-11 12:09:36 +0200
commitcbd45471b1100bffcd2f18719b56a5da5468756b (patch)
tree11fcb818fd4e9d7b6bdb97d1b5bfbe2ed3df5747 /services
parent8e8e936adacea873013b89d534f60207e67f2b05 (diff)
downloadgitea-cbd45471b1100bffcd2f18719b56a5da5468756b.tar.gz
gitea-cbd45471b1100bffcd2f18719b56a5da5468756b.zip
Move access and repo permission to models/perm/access (#19350)
* Move access and repo permission to models/perm/access * Remove unnecessary code
Diffstat (limited to 'services')
-rw-r--r--services/automerge/automerge.go3
-rw-r--r--services/issue/assignee.go11
-rw-r--r--services/issue/commit.go3
-rw-r--r--services/issue/issue.go3
-rw-r--r--services/issue/label.go3
-rw-r--r--services/lfs/server.go3
-rw-r--r--services/pull/check.go5
-rw-r--r--services/pull/edits.go3
-rw-r--r--services/pull/merge.go3
-rw-r--r--services/pull/update.go5
-rw-r--r--services/repository/transfer.go5
-rw-r--r--services/repository/transfer_test.go6
12 files changed, 33 insertions, 20 deletions
diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go
index 85af2659c6..3ce4883aa7 100644
--- a/services/automerge/automerge.go
+++ b/services/automerge/automerge.go
@@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ access_model "code.gitea.io/gitea/models/perm/access"
pull_model "code.gitea.io/gitea/models/pull"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
@@ -224,7 +225,7 @@ func handlePull(pullID int64, sha string) {
return
}
- perm, err := models.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
+ perm, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
if err != nil {
log.Error("GetUserRepoPermission: %v", err)
return
diff --git a/services/issue/assignee.go b/services/issue/assignee.go
index e6169b9c7e..0b6d0045fd 100644
--- a/services/issue/assignee.go
+++ b/services/issue/assignee.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
+ access_model "code.gitea.io/gitea/models/perm/access"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
@@ -80,7 +81,7 @@ func ReviewRequest(issue *models.Issue, doer, reviewer *user_model.User, isAdd b
}
// IsValidReviewRequest Check permission for ReviewRequest
-func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, isAdd bool, issue *models.Issue, permDoer *models.Permission) error {
+func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, isAdd bool, issue *models.Issue, permDoer *access_model.Permission) error {
if reviewer.IsOrganization() {
return models.ErrNotValidReviewRequest{
Reason: "Organization can't be added as reviewer",
@@ -96,14 +97,14 @@ func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User,
}
}
- permReviewer, err := models.GetUserRepoPermission(ctx, issue.Repo, reviewer)
+ permReviewer, err := access_model.GetUserRepoPermission(ctx, issue.Repo, reviewer)
if err != nil {
return err
}
if permDoer == nil {
- permDoer = new(models.Permission)
- *permDoer, err = models.GetUserRepoPermission(ctx, issue.Repo, doer)
+ permDoer = new(access_model.Permission)
+ *permDoer, err = access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
if err != nil {
return err
}
@@ -179,7 +180,7 @@ func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team,
}
}
- permission, err := models.GetUserRepoPermission(ctx, issue.Repo, doer)
+ permission, err := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
if err != nil {
log.Error("Unable to GetUserRepoPermission for %-v in %-v#%d", doer, issue.Repo, issue.Index)
return err
diff --git a/services/issue/commit.go b/services/issue/commit.go
index b5d97e12a8..5140eebed1 100644
--- a/services/issue/commit.go
+++ b/services/issue/commit.go
@@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ 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"
"code.gitea.io/gitea/modules/references"
@@ -131,7 +132,7 @@ func UpdateIssuesCommit(doer *user_model.User, repo *repo_model.Repository, comm
continue
}
- perm, err := models.GetUserRepoPermission(db.DefaultContext, refRepo, doer)
+ perm, err := access_model.GetUserRepoPermission(db.DefaultContext, refRepo, doer)
if err != nil {
return err
}
diff --git a/services/issue/issue.go b/services/issue/issue.go
index 6bc3959979..db304a46b7 100644
--- a/services/issue/issue.go
+++ b/services/issue/issue.go
@@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ 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"
"code.gitea.io/gitea/modules/git"
@@ -172,7 +173,7 @@ func AddAssigneeIfNotAssigned(issue *models.Issue, doer *user_model.User, assign
return nil
}
- valid, err := models.CanBeAssigned(assignee, issue.Repo, issue.IsPull)
+ valid, err := access_model.CanBeAssigned(db.DefaultContext, assignee, issue.Repo, issue.IsPull)
if err != nil {
return err
}
diff --git a/services/issue/label.go b/services/issue/label.go
index 62ccc0ad65..94e52482fb 100644
--- a/services/issue/label.go
+++ b/services/issue/label.go
@@ -7,6 +7,7 @@ package issue
import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ access_model "code.gitea.io/gitea/models/perm/access"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/notification"
)
@@ -54,7 +55,7 @@ func RemoveLabel(issue *models.Issue, doer *user_model.User, label *models.Label
return err
}
- perm, err := models.GetUserRepoPermission(ctx, issue.Repo, doer)
+ perm, err := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
if err != nil {
return err
}
diff --git a/services/lfs/server.go b/services/lfs/server.go
index c095bbfab4..f5c57a7dab 100644
--- a/services/lfs/server.go
+++ b/services/lfs/server.go
@@ -20,6 +20,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/perm"
+ access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
@@ -488,7 +489,7 @@ func authenticate(ctx *context.Context, repository *repo_model.Repository, autho
}
// ctx.IsSigned is unnecessary here, this will be checked in perm.CanAccess
- perm, err := models.GetUserRepoPermission(ctx, repository, ctx.Doer)
+ perm, err := access_model.GetUserRepoPermission(ctx, repository, ctx.Doer)
if err != nil {
log.Error("Unable to GetUserRepoPermission for user %-v in repo %-v Error: %v", ctx.Doer, repository)
return false
diff --git a/services/pull/check.go b/services/pull/check.go
index 6852940b22..d88dd3a550 100644
--- a/services/pull/check.go
+++ b/services/pull/check.go
@@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
@@ -60,7 +61,7 @@ func AddToTaskQueue(pr *models.PullRequest) {
}
// CheckPullMergable check if the pull mergable based on all conditions (branch protection, merge options, ...)
-func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *models.Permission, pr *models.PullRequest, manuallMerge, force bool) error {
+func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *access_model.Permission, pr *models.PullRequest, manuallMerge, force bool) error {
return db.WithTx(func(ctx context.Context) error {
if pr.HasMerged {
return ErrHasMerged
@@ -98,7 +99,7 @@ func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *mode
if err := CheckPullBranchProtections(ctx, pr, false); err != nil {
if models.IsErrDisallowedToMerge(err) {
if force {
- if isRepoAdmin, err2 := models.IsUserRepoAdminCtx(ctx, pr.BaseRepo, doer); err2 != nil {
+ if isRepoAdmin, err2 := access_model.IsUserRepoAdminCtx(ctx, pr.BaseRepo, doer); err2 != nil {
return err2
} else if !isRepoAdmin {
return err
diff --git a/services/pull/edits.go b/services/pull/edits.go
index 68515ec141..11932d9ab8 100644
--- a/services/pull/edits.go
+++ b/services/pull/edits.go
@@ -10,6 +10,7 @@ import (
"errors"
"code.gitea.io/gitea/models"
+ access_model "code.gitea.io/gitea/models/perm/access"
unit_model "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
)
@@ -26,7 +27,7 @@ func SetAllowEdits(ctx context.Context, doer *user_model.User, pr *models.PullRe
return err
}
- permission, err := models.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
+ permission, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
if err != nil {
return err
}
diff --git a/services/pull/merge.go b/services/pull/merge.go
index 0af3cc1613..fcced65cdf 100644
--- a/services/pull/merge.go
+++ b/services/pull/merge.go
@@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ access_model "code.gitea.io/gitea/models/perm/access"
pull_model "code.gitea.io/gitea/models/pull"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
@@ -751,7 +752,7 @@ func getDiffTree(ctx context.Context, repoPath, baseBranch, headBranch string) (
}
// IsUserAllowedToMerge check if user is allowed to merge PR with given permissions and branch protections
-func IsUserAllowedToMerge(ctx context.Context, pr *models.PullRequest, p models.Permission, user *user_model.User) (bool, error) {
+func IsUserAllowedToMerge(ctx context.Context, pr *models.PullRequest, p access_model.Permission, user *user_model.User) (bool, error) {
if user == nil {
return false, nil
}
diff --git a/services/pull/update.go b/services/pull/update.go
index 3c5c1c048c..0ab8ffcd7d 100644
--- a/services/pull/update.go
+++ b/services/pull/update.go
@@ -9,6 +9,7 @@ import (
"fmt"
"code.gitea.io/gitea/models"
+ access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
@@ -83,7 +84,7 @@ func IsUserAllowedToUpdate(ctx context.Context, pull *models.PullRequest, user *
if user == nil {
return false, false, nil
}
- headRepoPerm, err := models.GetUserRepoPermission(ctx, pull.HeadRepo, user)
+ headRepoPerm, err := access_model.GetUserRepoPermission(ctx, pull.HeadRepo, user)
if err != nil {
return false, false, err
}
@@ -115,7 +116,7 @@ func IsUserAllowedToUpdate(ctx context.Context, pull *models.PullRequest, user *
return false, false, nil
}
- baseRepoPerm, err := models.GetUserRepoPermission(ctx, pull.BaseRepo, user)
+ baseRepoPerm, err := access_model.GetUserRepoPermission(ctx, pull.BaseRepo, user)
if err != nil {
return false, false, err
}
diff --git a/services/repository/transfer.go b/services/repository/transfer.go
index 3feeb68f22..ae15383240 100644
--- a/services/repository/transfer.go
+++ b/services/repository/transfer.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
"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"
"code.gitea.io/gitea/modules/log"
@@ -105,7 +106,7 @@ func StartRepositoryTransfer(doer, newOwner *user_model.User, repo *repo_model.R
}
// In case the new owner would not have sufficient access to the repo, give access rights for read
- hasAccess, err := models.HasAccess(newOwner.ID, repo)
+ hasAccess, err := access_model.HasAccess(db.DefaultContext, newOwner.ID, repo)
if err != nil {
return err
}
@@ -113,7 +114,7 @@ func StartRepositoryTransfer(doer, newOwner *user_model.User, repo *repo_model.R
if err := models.AddCollaborator(repo, newOwner); err != nil {
return err
}
- if err := models.ChangeCollaborationAccessMode(repo, newOwner.ID, perm.AccessModeRead); err != nil {
+ if err := repo_model.ChangeCollaborationAccessMode(repo, newOwner.ID, perm.AccessModeRead); err != nil {
return err
}
}
diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go
index 1081c76c7e..8be8c5353d 100644
--- a/services/repository/transfer_test.go
+++ b/services/repository/transfer_test.go
@@ -9,7 +9,9 @@ import (
"testing"
"code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
+ access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
@@ -65,13 +67,13 @@ func TestStartRepositoryTransferSetPermission(t *testing.T) {
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}).(*repo_model.Repository)
repo.Owner = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
- hasAccess, err := models.HasAccess(recipient.ID, repo)
+ hasAccess, err := access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
assert.NoError(t, err)
assert.False(t, hasAccess)
assert.NoError(t, StartRepositoryTransfer(doer, recipient, repo, nil))
- hasAccess, err = models.HasAccess(recipient.ID, repo)
+ hasAccess, err = access_model.HasAccess(db.DefaultContext, recipient.ID, repo)
assert.NoError(t, err)
assert.True(t, hasAccess)