diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-20 22:08:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 22:08:52 +0800 |
commit | fd7d83ace60258acf7139c4c787aa8af75b7ba8c (patch) | |
tree | 50038348ec10485f72344f3ac80324e04abc1283 /models/perm | |
parent | d81e31ad7826a81fc7139f329f250594610a274b (diff) | |
download | gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.tar.gz gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.zip |
Move almost all functions' parameter db.Engine to context.Context (#19748)
* Move almost all functions' parameter db.Engine to context.Context
* remove some unnecessary wrap functions
Diffstat (limited to 'models/perm')
-rw-r--r-- | models/perm/access/access.go | 17 | ||||
-rw-r--r-- | models/perm/access/repo_permission.go | 18 |
2 files changed, 12 insertions, 23 deletions
diff --git a/models/perm/access/access.go b/models/perm/access/access.go index 75a3a93a3b..7647519025 100644 --- a/models/perm/access/access.go +++ b/models/perm/access/access.go @@ -30,7 +30,7 @@ func init() { db.RegisterModel(new(Access)) } -func accessLevel(e db.Engine, user *user_model.User, repo *repo_model.Repository) (perm.AccessMode, error) { +func accessLevel(ctx context.Context, user *user_model.User, repo *repo_model.Repository) (perm.AccessMode, error) { mode := perm.AccessModeNone var userID int64 restricted := false @@ -53,7 +53,7 @@ func accessLevel(e db.Engine, user *user_model.User, repo *repo_model.Repository } a := &Access{UserID: userID, RepoID: repo.ID} - if has, err := e.Get(a); !has || err != nil { + if has, err := db.GetByBean(ctx, a); !has || err != nil { return mode, err } return a.Mode, nil @@ -84,7 +84,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 refreshAccesses(e db.Engine, repo *repo_model.Repository, accessMap map[int64]*userAccess) (err error) { +func refreshAccesses(ctx context.Context, repo *repo_model.Repository, accessMap map[int64]*userAccess) (err error) { minMode := perm.AccessModeRead if !repo.IsPrivate { minMode = perm.AccessModeWrite @@ -104,14 +104,14 @@ func refreshAccesses(e db.Engine, repo *repo_model.Repository, accessMap map[int } // Delete old accesses and insert new ones for repository. - if _, err = e.Delete(&Access{RepoID: repo.ID}); err != nil { + if _, err = db.DeleteByBean(ctx, &Access{RepoID: repo.ID}); err != nil { return fmt.Errorf("delete old accesses: %v", err) } if len(newAccesses) == 0 { return nil } - if _, err = e.Insert(newAccesses); err != nil { + if err = db.Insert(ctx, newAccesses); err != nil { return fmt.Errorf("insert new accesses: %v", err) } return nil @@ -144,8 +144,6 @@ func RecalculateTeamAccesses(ctx context.Context, repo *repo_model.Repository, i return fmt.Errorf("owner is not an organization: %d", repo.OwnerID) } - e := db.GetEngine(ctx) - if err = refreshCollaboratorAccesses(ctx, repo.ID, accessMap); err != nil { return fmt.Errorf("refreshCollaboratorAccesses: %v", err) } @@ -176,7 +174,7 @@ func RecalculateTeamAccesses(ctx context.Context, repo *repo_model.Repository, i } } - return refreshAccesses(e, repo, accessMap) + return refreshAccesses(ctx, repo, accessMap) } // RecalculateUserAccess recalculates new access for a single user @@ -235,10 +233,9 @@ func RecalculateAccesses(ctx context.Context, repo *repo_model.Repository) error return RecalculateTeamAccesses(ctx, repo, 0) } - e := db.GetEngine(ctx) accessMap := make(map[int64]*userAccess, 20) if err := refreshCollaboratorAccesses(ctx, repo.ID, accessMap); err != nil { return fmt.Errorf("refreshCollaboratorAccesses: %v", err) } - return refreshAccesses(e, repo, accessMap) + return refreshAccesses(ctx, repo, accessMap) } diff --git a/models/perm/access/repo_permission.go b/models/perm/access/repo_permission.go index 090c78ff2c..6bc1c82703 100644 --- a/models/perm/access/repo_permission.go +++ b/models/perm/access/repo_permission.go @@ -168,8 +168,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use return } - e := db.GetEngine(ctx) - var is bool if user != nil { is, err = repo_model.IsCollaborator(ctx, repo.ID, user.ID) @@ -208,7 +206,7 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use } // plain user - perm.AccessMode, err = accessLevel(e, user, repo) + perm.AccessMode, err = accessLevel(ctx, user, repo) if err != nil { return } @@ -288,7 +286,7 @@ func IsUserRealRepoAdmin(repo *repo_model.Repository, user *user_model.User) (bo return false, err } - accessMode, err := accessLevel(db.GetEngine(db.DefaultContext), user, repo) + accessMode, err := accessLevel(db.DefaultContext, user, repo) if err != nil { return false, err } @@ -297,12 +295,7 @@ func IsUserRealRepoAdmin(repo *repo_model.Repository, user *user_model.User) (bo } // IsUserRepoAdmin return true if user has admin right of a repo -func IsUserRepoAdmin(repo *repo_model.Repository, user *user_model.User) (bool, error) { - return IsUserRepoAdminCtx(db.DefaultContext, repo, user) -} - -// IsUserRepoAdminCtx return true if user has admin right of a repo -func IsUserRepoAdminCtx(ctx context.Context, repo *repo_model.Repository, user *user_model.User) (bool, error) { +func IsUserRepoAdmin(ctx context.Context, repo *repo_model.Repository, user *user_model.User) (bool, error) { if user == nil || repo == nil { return false, nil } @@ -310,8 +303,7 @@ func IsUserRepoAdminCtx(ctx context.Context, repo *repo_model.Repository, user * return true, nil } - e := db.GetEngine(ctx) - mode, err := accessLevel(e, user, repo) + mode, err := accessLevel(ctx, user, repo) if err != nil { return false, err } @@ -377,7 +369,7 @@ func HasAccess(ctx context.Context, userID int64, repo *repo_model.Repository) ( var user *user_model.User var err error if userID > 0 { - user, err = user_model.GetUserByIDEngine(db.GetEngine(ctx), userID) + user, err = user_model.GetUserByIDCtx(ctx, userID) if err != nil { return false, err } |