Skip to content

Commit 01e6831

Browse files
committed
strip duplicated auth in lfs model
1 parent beb0581 commit 01e6831

File tree

3 files changed

+6
-61
lines changed

3 files changed

+6
-61
lines changed

models/git/lfs_lock.go

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import (
1111
"time"
1212

1313
"code.gitea.io/gitea/models/db"
14-
"code.gitea.io/gitea/models/perm"
15-
access_model "code.gitea.io/gitea/models/perm/access"
1614
repo_model "code.gitea.io/gitea/models/repo"
17-
"code.gitea.io/gitea/models/unit"
1815
user_model "code.gitea.io/gitea/models/user"
1916
"code.gitea.io/gitea/modules/setting"
2017
"code.gitea.io/gitea/modules/util"
@@ -69,12 +66,8 @@ func (l *LFSLock) LoadOwner(ctx context.Context) error {
6966
}
7067

7168
// CreateLFSLock creates a new lock.
72-
func CreateLFSLock(ctx context.Context, repo *repo_model.Repository, lock *LFSLock, taskID int64) (*LFSLock, error) {
69+
func CreateLFSLock(ctx context.Context, repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) {
7370
return db.WithTx2(ctx, func(ctx context.Context) (*LFSLock, error) {
74-
if err := CheckLFSAccessForRepo(ctx, lock.OwnerID, repo, perm.AccessModeWrite, taskID); err != nil {
75-
return nil, err
76-
}
77-
7871
lock.Path = util.PathJoinRel(lock.Path)
7972
lock.RepoID = repo.ID
8073

@@ -158,17 +151,13 @@ func CountLFSLockByRepoID(ctx context.Context, repoID int64) (int64, error) {
158151
}
159152

160153
// DeleteLFSLockByID deletes a lock by given ID.
161-
func DeleteLFSLockByID(ctx context.Context, id int64, repo *repo_model.Repository, u *user_model.User, force bool, taskID int64) (*LFSLock, error) {
154+
func DeleteLFSLockByID(ctx context.Context, id int64, repo *repo_model.Repository, u *user_model.User, force bool) (*LFSLock, error) {
162155
return db.WithTx2(ctx, func(ctx context.Context) (*LFSLock, error) {
163156
lock, err := GetLFSLockByID(ctx, id)
164157
if err != nil {
165158
return nil, err
166159
}
167160

168-
if err := CheckLFSAccessForRepo(ctx, u.ID, repo, perm.AccessModeWrite, taskID); err != nil {
169-
return nil, err
170-
}
171-
172161
if !force && u.ID != lock.OwnerID {
173162
return nil, errors.New("user doesn't own lock and force flag is not set")
174163
}
@@ -180,35 +169,3 @@ func DeleteLFSLockByID(ctx context.Context, id int64, repo *repo_model.Repositor
180169
return lock, nil
181170
})
182171
}
183-
184-
// CheckLFSAccessForRepo check needed access mode base on action
185-
func CheckLFSAccessForRepo(ctx context.Context, ownerID int64, repo *repo_model.Repository, mode perm.AccessMode, taskID int64) error {
186-
if ownerID == 0 {
187-
return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode}
188-
}
189-
if ownerID == user_model.ActionsUserID {
190-
if taskID == 0 {
191-
return ErrLFSUnauthorizedAction{repo.ID, user_model.ActionsUserName, mode}
192-
}
193-
perm, err := access_model.GetActionsUserRepoPermission(ctx, repo, user_model.NewActionsUser(), taskID)
194-
if err != nil {
195-
return err
196-
}
197-
if !perm.CanAccess(mode, unit.TypeCode) {
198-
return ErrLFSUnauthorizedAction{repo.ID, user_model.ActionsUserName, mode}
199-
}
200-
return nil
201-
}
202-
u, err := user_model.GetUserByID(ctx, ownerID)
203-
if err != nil {
204-
return err
205-
}
206-
perm, err := access_model.GetUserRepoPermission(ctx, repo, u)
207-
if err != nil {
208-
return err
209-
}
210-
if !perm.CanAccess(mode, unit.TypeCode) {
211-
return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode}
212-
}
213-
return nil
214-
}

routers/web/repo/setting/lfs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func LFSLockFile(ctx *context.Context) {
198198
_, err := git_model.CreateLFSLock(ctx, ctx.Repo.Repository, &git_model.LFSLock{
199199
Path: lockPath,
200200
OwnerID: ctx.Doer.ID,
201-
}, 0)
201+
})
202202
if err != nil {
203203
if git_model.IsErrLFSLockAlreadyExist(err) {
204204
ctx.Flash.Error(ctx.Tr("repo.settings.lfs_lock_already_exists", originalPath))
@@ -217,7 +217,7 @@ func LFSUnlock(ctx *context.Context) {
217217
ctx.NotFound(nil)
218218
return
219219
}
220-
_, err := git_model.DeleteLFSLockByID(ctx, ctx.PathParamInt64("lid"), ctx.Repo.Repository, ctx.Doer, true, 0)
220+
_, err := git_model.DeleteLFSLockByID(ctx, ctx.PathParamInt64("lid"), ctx.Repo.Repository, ctx.Doer, true)
221221
if err != nil {
222222
ctx.ServerError("LFSUnlock", err)
223223
return

services/lfs/locks.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,10 @@ func PostLockHandler(ctx *context.Context) {
175175
return
176176
}
177177

178-
var taskID int64
179-
// Passing a non zero Actions Task ID as parameter if creating lock using Actions Job Token
180-
if ctx.Data["IsActionsToken"] == true {
181-
taskID = ctx.Data["ActionsTaskID"].(int64)
182-
}
183-
184178
lock, err := git_model.CreateLFSLock(ctx, repository, &git_model.LFSLock{
185179
Path: req.Path,
186180
OwnerID: ctx.Doer.ID,
187-
}, taskID)
181+
})
188182
if err != nil {
189183
if git_model.IsErrLFSLockAlreadyExist(err) {
190184
ctx.JSON(http.StatusConflict, api.LFSLockError{
@@ -321,13 +315,7 @@ func UnLockHandler(ctx *context.Context) {
321315
return
322316
}
323317

324-
var taskID int64
325-
// Passing a non zero Actions Task ID as parameter if deleting lock using Actions Job Token
326-
if ctx.Data["IsActionsToken"] == true {
327-
taskID = ctx.Data["ActionsTaskID"].(int64)
328-
}
329-
330-
lock, err := git_model.DeleteLFSLockByID(ctx, ctx.PathParamInt64("lid"), repository, ctx.Doer, req.Force, taskID)
318+
lock, err := git_model.DeleteLFSLockByID(ctx, ctx.PathParamInt64("lid"), repository, ctx.Doer, req.Force)
331319
if err != nil {
332320
if git_model.IsErrLFSUnauthorizedAction(err) {
333321
ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)

0 commit comments

Comments
 (0)