mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	Refactor Actions Token Access (#35688)
* use a single function to do Action Tokens Permission checks * allows easier customization * add basic tests * lfs file locks should work now --------- Signed-off-by: ChristopherHX <christopher.homberger@web.de> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		| @@ -11,10 +11,7 @@ import ( | ||||
| 	"time" | ||||
|  | ||||
| 	"code.gitea.io/gitea/models/db" | ||||
| 	"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" | ||||
| 	"code.gitea.io/gitea/modules/setting" | ||||
| 	"code.gitea.io/gitea/modules/util" | ||||
| @@ -71,10 +68,6 @@ func (l *LFSLock) LoadOwner(ctx context.Context) error { | ||||
| // CreateLFSLock creates a new lock. | ||||
| func CreateLFSLock(ctx context.Context, repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) { | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*LFSLock, error) { | ||||
| 		if err := CheckLFSAccessForRepo(ctx, lock.OwnerID, repo, perm.AccessModeWrite); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 		lock.Path = util.PathJoinRel(lock.Path) | ||||
| 		lock.RepoID = repo.ID | ||||
|  | ||||
| @@ -165,10 +158,6 @@ func DeleteLFSLockByID(ctx context.Context, id int64, repo *repo_model.Repositor | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 		if err := CheckLFSAccessForRepo(ctx, u.ID, repo, perm.AccessModeWrite); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 		if !force && u.ID != lock.OwnerID { | ||||
| 			return nil, errors.New("user doesn't own lock and force flag is not set") | ||||
| 		} | ||||
| @@ -180,22 +169,3 @@ func DeleteLFSLockByID(ctx context.Context, id int64, repo *repo_model.Repositor | ||||
| 		return lock, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // CheckLFSAccessForRepo check needed access mode base on action | ||||
| func CheckLFSAccessForRepo(ctx context.Context, ownerID int64, repo *repo_model.Repository, mode perm.AccessMode) error { | ||||
| 	if ownerID == 0 { | ||||
| 		return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode} | ||||
| 	} | ||||
| 	u, err := user_model.GetUserByID(ctx, ownerID) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	perm, err := access_model.GetUserRepoPermission(ctx, repo, u) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	if !perm.CanAccess(mode, unit.TypeCode) { | ||||
| 		return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode} | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user