diff options
author | Giteabot <teabot@gitea.io> | 2023-06-22 11:01:42 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-22 17:01:42 +0200 |
commit | 203fe2841dd13bcccf5f526de0c07313f7a24dc1 (patch) | |
tree | 570d66832bb3c53d83290819b963bbad5b9ae535 /routers/api/v1/user | |
parent | 056829749e4e955861531cbb1ae60d47a28128f0 (diff) | |
download | gitea-203fe2841dd13bcccf5f526de0c07313f7a24dc1.tar.gz gitea-203fe2841dd13bcccf5f526de0c07313f7a24dc1.zip |
Fix `Permission` in API returned repository struct (#25388) (#25441)
Backport #25388 by @wolfogre
The old code generates `structs.Repository.Permissions` with only
`access.Permission.AccessMode`, however, it should check the units too,
or the value could be incorrect. For example,
`structs.Repository.Permissions.Push` could be false even the doer has
write access to code unit.
Should fix
https://github.com/renovatebot/renovate/issues/14059#issuecomment-1047961128
(Not reported by it, I just found it when I was looking into this bug)
---
Review tips:
The major changes are
- `modules/structs/repo.go`
https://github.com/go-gitea/gitea/pull/25388/files#diff-870406f6857117f8b03611c43fca0ab9ed6d6e76a2d0069a7c1f17e8fa9092f7
- `services/convert/repository.go`
https://github.com/go-gitea/gitea/pull/25388/files#diff-7736f6d2ae894c9edb7729a80ab89aa183b888a26a811a0c1fdebd18726a7101
And other changes are passive.
Co-authored-by: Jason Song <i@wolfogre.com>
Diffstat (limited to 'routers/api/v1/user')
-rw-r--r-- | routers/api/v1/user/repo.go | 15 | ||||
-rw-r--r-- | routers/api/v1/user/star.go | 4 | ||||
-rw-r--r-- | routers/api/v1/user/watch.go | 4 |
3 files changed, 12 insertions, 11 deletions
diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 7a8978cc4e..86af8cb440 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models/perm" access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" + unit_model "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" api "code.gitea.io/gitea/modules/structs" @@ -38,13 +39,13 @@ func listUserRepos(ctx *context.APIContext, u *user_model.User, private bool) { apiRepos := make([]*api.Repository, 0, len(repos)) for i := range repos { - access, err := access_model.AccessLevel(ctx, ctx.Doer, repos[i]) + permission, err := access_model.GetUserRepoPermission(ctx, repos[i], ctx.Doer) if err != nil { - ctx.Error(http.StatusInternalServerError, "AccessLevel", err) + ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err) return } - if ctx.IsSigned && ctx.Doer.IsAdmin || access >= perm.AccessModeRead { - apiRepos = append(apiRepos, convert.ToRepo(ctx, repos[i], access)) + if ctx.IsSigned && ctx.Doer.IsAdmin || permission.UnitAccessMode(unit_model.TypeCode) >= perm.AccessModeRead { + apiRepos = append(apiRepos, convert.ToRepo(ctx, repos[i], permission)) } } @@ -123,11 +124,11 @@ func ListMyRepos(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadOwner", err) return } - accessMode, err := access_model.AccessLevel(ctx, ctx.Doer, repo) + permission, err := access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) if err != nil { - ctx.Error(http.StatusInternalServerError, "AccessLevel", err) + ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err) } - results[i] = convert.ToRepo(ctx, repo, accessMode) + results[i] = convert.ToRepo(ctx, repo, permission) } ctx.SetLinkHeader(int(count), opts.ListOptions.PageSize) diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index ad5a8bee33..9399ad2b4d 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -28,11 +28,11 @@ func getStarredRepos(ctx std_context.Context, user *user_model.User, private boo repos := make([]*api.Repository, len(starredRepos)) for i, starred := range starredRepos { - access, err := access_model.AccessLevel(ctx, user, starred) + permission, err := access_model.GetUserRepoPermission(ctx, starred, user) if err != nil { return nil, err } - repos[i] = convert.ToRepo(ctx, starred, access) + repos[i] = convert.ToRepo(ctx, starred, permission) } return repos, nil } diff --git a/routers/api/v1/user/watch.go b/routers/api/v1/user/watch.go index 211f36459a..172d9d5cc5 100644 --- a/routers/api/v1/user/watch.go +++ b/routers/api/v1/user/watch.go @@ -26,11 +26,11 @@ func getWatchedRepos(ctx std_context.Context, user *user_model.User, private boo repos := make([]*api.Repository, len(watchedRepos)) for i, watched := range watchedRepos { - access, err := access_model.AccessLevel(ctx, user, watched) + permission, err := access_model.GetUserRepoPermission(ctx, watched, user) if err != nil { return nil, 0, err } - repos[i] = convert.ToRepo(ctx, watched, access) + repos[i] = convert.ToRepo(ctx, watched, permission) } return repos, total, nil } |