diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 09:27:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 09:27:50 +0800 |
commit | 719bddcd76610a63dadc8555760072957a11cf30 (patch) | |
tree | 0df26092fba7e3e21444fe493e6b349473b6b0cb /models/access.go | |
parent | fb8166c6c6b652a0e6fa98681780a6a71090faf3 (diff) | |
download | gitea-719bddcd76610a63dadc8555760072957a11cf30.tar.gz gitea-719bddcd76610a63dadc8555760072957a11cf30.zip |
Move repository model into models/repo (#17933)
* Some refactors related repository model
* Move more methods out of repository
* Move repository into models/repo
* Fix test
* Fix test
* some improvements
* Remove unnecessary function
Diffstat (limited to 'models/access.go')
-rw-r--r-- | models/access.go | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/models/access.go b/models/access.go index f7252019aa..6a97bcffcf 100644 --- a/models/access.go +++ b/models/access.go @@ -6,10 +6,12 @@ package models import ( + "context" "fmt" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/perm" + repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" ) @@ -27,7 +29,7 @@ func init() { db.RegisterModel(new(Access)) } -func accessLevel(e db.Engine, user *user_model.User, repo *Repository) (perm.AccessMode, error) { +func accessLevel(e db.Engine, user *user_model.User, repo *repo_model.Repository) (perm.AccessMode, error) { mode := perm.AccessModeNone var userID int64 restricted := false @@ -81,7 +83,7 @@ func updateUserAccess(accessMap map[int64]*userAccess, user *user_model.User, mo } // FIXME: do cross-comparison so reduce deletions and additions to the minimum? -func (repo *Repository) refreshAccesses(e db.Engine, accessMap map[int64]*userAccess) (err error) { +func refreshAccesses(e db.Engine, repo *repo_model.Repository, accessMap map[int64]*userAccess) (err error) { minMode := perm.AccessModeRead if !repo.IsPrivate { minMode = perm.AccessModeWrite @@ -115,8 +117,8 @@ func (repo *Repository) refreshAccesses(e db.Engine, accessMap map[int64]*userAc } // refreshCollaboratorAccesses retrieves repository collaborations with their access modes. -func (repo *Repository) refreshCollaboratorAccesses(e db.Engine, accessMap map[int64]*userAccess) error { - collaborators, err := repo.getCollaborators(e, db.ListOptions{}) +func refreshCollaboratorAccesses(e db.Engine, repoID int64, accessMap map[int64]*userAccess) error { + collaborators, err := getCollaborators(e, repoID, db.ListOptions{}) if err != nil { return fmt.Errorf("getCollaborations: %v", err) } @@ -132,16 +134,18 @@ func (repo *Repository) refreshCollaboratorAccesses(e db.Engine, accessMap map[i // recalculateTeamAccesses recalculates new accesses for teams of an organization // except the team whose ID is given. It is used to assign a team ID when // remove repository from that team. -func (repo *Repository) recalculateTeamAccesses(e db.Engine, ignTeamID int64) (err error) { +func recalculateTeamAccesses(ctx context.Context, repo *repo_model.Repository, ignTeamID int64) (err error) { accessMap := make(map[int64]*userAccess, 20) - if err = repo.getOwner(e); err != nil { + if err = repo.GetOwner(ctx); err != nil { return err } else if !repo.Owner.IsOrganization() { return fmt.Errorf("owner is not an organization: %d", repo.OwnerID) } - if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil { + e := db.GetEngine(ctx) + + if err = refreshCollaboratorAccesses(e, repo.ID, accessMap); err != nil { return fmt.Errorf("refreshCollaboratorAccesses: %v", err) } @@ -171,26 +175,27 @@ func (repo *Repository) recalculateTeamAccesses(e db.Engine, ignTeamID int64) (e } } - return repo.refreshAccesses(e, accessMap) + return refreshAccesses(e, repo, accessMap) } // recalculateUserAccess recalculates new access for a single user // Usable if we know access only affected one user -func (repo *Repository) recalculateUserAccess(e db.Engine, uid int64) (err error) { +func recalculateUserAccess(ctx context.Context, repo *repo_model.Repository, uid int64) (err error) { minMode := perm.AccessModeRead if !repo.IsPrivate { minMode = perm.AccessModeWrite } accessMode := perm.AccessModeNone - collaborator, err := repo.getCollaboration(e, uid) + e := db.GetEngine(ctx) + collaborator, err := getCollaboration(e, repo.ID, uid) if err != nil { return err } else if collaborator != nil { accessMode = collaborator.Mode } - if err = repo.getOwner(e); err != nil { + if err = repo.GetOwner(ctx); err != nil { return err } else if repo.Owner.IsOrganization() { var teams []Team @@ -223,19 +228,20 @@ func (repo *Repository) recalculateUserAccess(e db.Engine, uid int64) (err error return nil } -func (repo *Repository) recalculateAccesses(e db.Engine) error { +func recalculateAccesses(ctx context.Context, repo *repo_model.Repository) error { if repo.Owner.IsOrganization() { - return repo.recalculateTeamAccesses(e, 0) + return recalculateTeamAccesses(ctx, repo, 0) } + e := db.GetEngine(ctx) accessMap := make(map[int64]*userAccess, 20) - if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil { + if err := refreshCollaboratorAccesses(e, repo.ID, accessMap); err != nil { return fmt.Errorf("refreshCollaboratorAccesses: %v", err) } - return repo.refreshAccesses(e, accessMap) + return refreshAccesses(e, repo, accessMap) } // RecalculateAccesses recalculates all accesses for repository. -func (repo *Repository) RecalculateAccesses() error { - return repo.recalculateAccesses(db.GetEngine(db.DefaultContext)) +func RecalculateAccesses(repo *repo_model.Repository) error { + return recalculateAccesses(db.DefaultContext, repo) } |