aboutsummaryrefslogtreecommitdiffstats
path: root/models/repo.go
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 /models/repo.go
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 'models/repo.go')
-rw-r--r--models/repo.go51
1 files changed, 10 insertions, 41 deletions
diff --git a/models/repo.go b/models/repo.go
index fb7bbba1e1..598eec7c9e 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -22,6 +22,7 @@ import (
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
+ access_model "code.gitea.io/gitea/models/perm/access"
project_model "code.gitea.io/gitea/models/project"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
@@ -54,7 +55,7 @@ func checkRepoUnitUser(ctx context.Context, repo *repo_model.Repository, user *u
if user != nil && user.IsAdmin {
return true
}
- perm, err := GetUserRepoPermission(ctx, repo, user)
+ perm, err := access_model.GetUserRepoPermission(ctx, repo, user)
if err != nil {
log.Error("GetUserRepoPermission(): %v", err)
return false
@@ -302,38 +303,6 @@ func CanUserDelete(repo *repo_model.Repository, user *user_model.User) (bool, er
return false, nil
}
-// getUsersWithAccessMode returns users that have at least given access mode to the repository.
-func getUsersWithAccessMode(ctx context.Context, repo *repo_model.Repository, mode perm.AccessMode) (_ []*user_model.User, err error) {
- if err = repo.GetOwner(ctx); err != nil {
- return nil, err
- }
-
- e := db.GetEngine(ctx)
- accesses := make([]*Access, 0, 10)
- if err = e.Where("repo_id = ? AND mode >= ?", repo.ID, mode).Find(&accesses); err != nil {
- return nil, err
- }
-
- // Leave a seat for owner itself to append later, but if owner is an organization
- // and just waste 1 unit is cheaper than re-allocate memory once.
- users := make([]*user_model.User, 0, len(accesses)+1)
- if len(accesses) > 0 {
- userIDs := make([]int64, len(accesses))
- for i := 0; i < len(accesses); i++ {
- userIDs[i] = accesses[i].UserID
- }
-
- if err = e.In("id", userIDs).Find(&users); err != nil {
- return nil, err
- }
- }
- if !repo.Owner.IsOrganization() {
- users = append(users, repo.Owner)
- }
-
- return users, nil
-}
-
// SetRepoReadBy sets repo to be visited by given user.
func SetRepoReadBy(repoID, userID int64) error {
return setRepoNotificationStatusReadIfUnread(db.GetEngine(db.DefaultContext), userID, repoID)
@@ -452,18 +421,18 @@ func CreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_
}
}
- if isAdmin, err := IsUserRepoAdminCtx(ctx, repo, doer); err != nil {
+ if isAdmin, err := access_model.IsUserRepoAdminCtx(ctx, repo, doer); err != nil {
return fmt.Errorf("IsUserRepoAdminCtx: %v", err)
} else if !isAdmin {
// Make creator repo admin if it wasn't assigned automatically
if err = addCollaborator(ctx, repo, doer); err != nil {
return fmt.Errorf("AddCollaborator: %v", err)
}
- if err = changeCollaborationAccessMode(db.GetEngine(ctx), repo, doer.ID, perm.AccessModeAdmin); err != nil {
+ if err = repo_model.ChangeCollaborationAccessModeCtx(ctx, repo, doer.ID, perm.AccessModeAdmin); err != nil {
return fmt.Errorf("ChangeCollaborationAccessMode: %v", err)
}
}
- } else if err = recalculateAccesses(ctx, repo); err != nil {
+ } else if err = access_model.RecalculateAccesses(ctx, repo); err != nil {
// Organization automatically called this in addRepository method.
return fmt.Errorf("recalculateAccesses: %v", err)
}
@@ -551,7 +520,7 @@ func UpdateRepositoryCtx(ctx context.Context, repo *repo_model.Repository, visib
}
if repo.Owner.IsOrganization() {
// Organization repository need to recalculate access table when visibility is changed.
- if err = recalculateTeamAccesses(ctx, repo, 0); err != nil {
+ if err = access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
return fmt.Errorf("recalculateTeamAccesses: %v", err)
}
}
@@ -659,7 +628,7 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
return err
}
for _, t := range teams {
- if !hasRepository(ctx, t, repoID) {
+ if !organization.HasTeamRepo(ctx, t.OrgID, t.ID, repoID) {
continue
} else if err = removeRepository(ctx, t, repo, false); err != nil {
return err
@@ -683,9 +652,9 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
}
if err := db.DeleteBeans(ctx,
- &Access{RepoID: repo.ID},
+ &access_model.Access{RepoID: repo.ID},
&Action{RepoID: repo.ID},
- &Collaboration{RepoID: repoID},
+ &repo_model.Collaboration{RepoID: repoID},
&Comment{RefRepoID: repoID},
&CommitStatus{RepoID: repoID},
&DeletedBranch{RepoID: repoID},
@@ -1212,7 +1181,7 @@ func DeleteDeployKey(ctx context.Context, doer *user_model.User, id int64) error
if err != nil {
return fmt.Errorf("GetRepositoryByID: %v", err)
}
- has, err := IsUserRepoAdminCtx(ctx, repo, doer)
+ has, err := access_model.IsUserRepoAdminCtx(ctx, repo, doer)
if err != nil {
return fmt.Errorf("GetUserRepoPermission: %v", err)
} else if !has {