diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-28 19:58:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-28 12:58:28 +0100 |
commit | 1fee11d69a437ec7e68b3f6b645827592fe77292 (patch) | |
tree | 1fca452f96746d9f7cc8c31723e3c365155cf96d /routers/api | |
parent | 24a8d54bfb908a2371cb808a5243b3290dd0664a (diff) | |
download | gitea-1fee11d69a437ec7e68b3f6b645827592fe77292.tar.gz gitea-1fee11d69a437ec7e68b3f6b645827592fe77292.zip |
Move accessmode into models/perm (#17828)
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/org/org.go | 9 | ||||
-rw-r--r-- | routers/api/v1/org/team.go | 13 | ||||
-rw-r--r-- | routers/api/v1/repo/collaborators.go | 4 | ||||
-rw-r--r-- | routers/api/v1/repo/fork.go | 3 | ||||
-rw-r--r-- | routers/api/v1/repo/hook.go | 8 | ||||
-rw-r--r-- | routers/api/v1/repo/key.go | 3 | ||||
-rw-r--r-- | routers/api/v1/repo/migrate.go | 3 | ||||
-rw-r--r-- | routers/api/v1/repo/release.go | 3 | ||||
-rw-r--r-- | routers/api/v1/repo/repo.go | 5 | ||||
-rw-r--r-- | routers/api/v1/repo/transfer.go | 5 | ||||
-rw-r--r-- | routers/api/v1/user/key.go | 3 | ||||
-rw-r--r-- | routers/api/v1/user/repo.go | 3 |
12 files changed, 36 insertions, 26 deletions
diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index 84065363f0..133cce3416 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -155,16 +156,16 @@ func GetUserOrgsPermissions(ctx *context.APIContext) { return } - if authorizeLevel > models.AccessModeNone { + if authorizeLevel > perm.AccessModeNone { op.CanRead = true } - if authorizeLevel > models.AccessModeRead { + if authorizeLevel > perm.AccessModeRead { op.CanWrite = true } - if authorizeLevel > models.AccessModeWrite { + if authorizeLevel > perm.AccessModeWrite { op.IsAdmin = true } - if authorizeLevel > models.AccessModeAdmin { + if authorizeLevel > perm.AccessModeAdmin { op.IsOwner = true } diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index e78e487895..371f1dc86d 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -9,6 +9,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/perm" unit_model "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -170,12 +171,12 @@ func CreateTeam(ctx *context.APIContext) { Description: form.Description, IncludesAllRepositories: form.IncludesAllRepositories, CanCreateOrgRepo: form.CanCreateOrgRepo, - Authorize: models.ParseAccessMode(form.Permission), + Authorize: perm.ParseAccessMode(form.Permission), } unitTypes := unit_model.FindUnitTypes(form.Units...) - if team.Authorize < models.AccessModeOwner { + if team.Authorize < perm.AccessModeOwner { var units = make([]*models.TeamUnit, 0, len(form.Units)) for _, tp := range unitTypes { units = append(units, &models.TeamUnit{ @@ -245,7 +246,7 @@ func EditTeam(ctx *context.APIContext) { isIncludeAllChanged := false if !team.IsOwnerTeam() && len(form.Permission) != 0 { // Validate permission level. - auth := models.ParseAccessMode(form.Permission) + auth := perm.ParseAccessMode(form.Permission) if team.Authorize != auth { isAuthChanged = true @@ -258,7 +259,7 @@ func EditTeam(ctx *context.APIContext) { } } - if team.Authorize < models.AccessModeOwner { + if team.Authorize < perm.AccessModeOwner { if len(form.Units) > 0 { var units = make([]*models.TeamUnit, 0, len(form.Units)) unitTypes := unit_model.FindUnitTypes(form.Units...) @@ -561,7 +562,7 @@ func AddTeamRepository(ctx *context.APIContext) { if access, err := models.AccessLevel(ctx.User, repo); err != nil { ctx.Error(http.StatusInternalServerError, "AccessLevel", err) return - } else if access < models.AccessModeAdmin { + } else if access < perm.AccessModeAdmin { ctx.Error(http.StatusForbidden, "", "Must have admin-level access to the repository") return } @@ -611,7 +612,7 @@ func RemoveTeamRepository(ctx *context.APIContext) { if access, err := models.AccessLevel(ctx.User, repo); err != nil { ctx.Error(http.StatusInternalServerError, "AccessLevel", err) return - } else if access < models.AccessModeAdmin { + } else if access < perm.AccessModeAdmin { ctx.Error(http.StatusForbidden, "", "Must have admin-level access to the repository") return } diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go index fcc8f9a90f..80794fa444 100644 --- a/routers/api/v1/repo/collaborators.go +++ b/routers/api/v1/repo/collaborators.go @@ -9,7 +9,7 @@ import ( "errors" "net/http" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -177,7 +177,7 @@ func AddCollaborator(ctx *context.APIContext) { } if form.Permission != nil { - if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil { + if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, perm.ParseAccessMode(*form.Permission)); err != nil { ctx.Error(http.StatusInternalServerError, "ChangeCollaborationAccessMode", err) return } diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index 528b8fdecc..33321e6a02 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -10,6 +10,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -135,5 +136,5 @@ func CreateFork(ctx *context.APIContext) { } //TODO change back to 201 - ctx.JSON(http.StatusAccepted, convert.ToRepo(fork, models.AccessModeOwner)) + ctx.JSON(http.StatusAccepted, convert.ToRepo(fork, perm.AccessModeOwner)) } diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 2969b92502..fdcaf5e389 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -8,7 +8,7 @@ package repo import ( "net/http" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -162,9 +162,9 @@ func TestHook(ctx *context.APIContext) { After: ctx.Repo.Commit.ID.String(), Commits: []*api.PayloadCommit{commit}, HeadCommit: commit, - Repo: convert.ToRepo(ctx.Repo.Repository, models.AccessModeNone), - Pusher: convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone), - Sender: convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone), + Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone), + Pusher: convert.ToUserWithAccessMode(ctx.User, perm.AccessModeNone), + Sender: convert.ToUserWithAccessMode(ctx.User, perm.AccessModeNone), }); err != nil { ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err) return diff --git a/routers/api/v1/repo/key.go b/routers/api/v1/repo/key.go index c20a4776cc..ad94da5b2d 100644 --- a/routers/api/v1/repo/key.go +++ b/routers/api/v1/repo/key.go @@ -11,6 +11,7 @@ import ( "net/url" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/setting" @@ -21,7 +22,7 @@ import ( // appendPrivateInformation appends the owner and key type information to api.PublicKey func appendPrivateInformation(apiKey *api.DeployKey, key *models.DeployKey, repository *models.Repository) (*api.DeployKey, error) { - apiKey.ReadOnly = key.Mode == models.AccessModeRead + apiKey.ReadOnly = key.Mode == perm.AccessModeRead if repository.ID == key.RepoID { apiKey.Repository = convert.ToRepo(repository, key.Mode) } else { diff --git a/routers/api/v1/repo/migrate.go b/routers/api/v1/repo/migrate.go index bc594419f7..dba44efb21 100644 --- a/routers/api/v1/repo/migrate.go +++ b/routers/api/v1/repo/migrate.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -207,7 +208,7 @@ func Migrate(ctx *context.APIContext) { } log.Trace("Repository migrated: %s/%s", repoOwner.Name, form.RepoName) - ctx.JSON(http.StatusCreated, convert.ToRepo(repo, models.AccessModeAdmin)) + ctx.JSON(http.StatusCreated, convert.ToRepo(repo, perm.AccessModeAdmin)) } func handleMigrateError(ctx *context.APIContext, repoOwner *user_model.User, remoteAddr string, err error) { diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index 08ebde4e0c..299eaddbc8 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -115,7 +116,7 @@ func ListReleases(ctx *context.APIContext) { opts := models.FindReleasesOptions{ ListOptions: listOptions, - IncludeDrafts: ctx.Repo.AccessMode >= models.AccessModeWrite || ctx.Repo.UnitAccessMode(unit.TypeReleases) >= models.AccessModeWrite, + IncludeDrafts: ctx.Repo.AccessMode >= perm.AccessModeWrite || ctx.Repo.UnitAccessMode(unit.TypeReleases) >= perm.AccessModeWrite, IncludeTags: false, IsDraft: ctx.FormOptionalBool("draft"), IsPreRelease: ctx.FormOptionalBool("pre-release"), diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index a74e13d3ab..2f37e10984 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/perm" unit_model "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -276,7 +277,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre ctx.Error(http.StatusInternalServerError, "GetRepositoryByID", err) } - ctx.JSON(http.StatusCreated, convert.ToRepo(repo, models.AccessModeOwner)) + ctx.JSON(http.StatusCreated, convert.ToRepo(repo, perm.AccessModeOwner)) } // Create one repository of mine @@ -420,7 +421,7 @@ func Generate(ctx *context.APIContext) { } log.Trace("Repository generated [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name) - ctx.JSON(http.StatusCreated, convert.ToRepo(repo, models.AccessModeOwner)) + ctx.JSON(http.StatusCreated, convert.ToRepo(repo, perm.AccessModeOwner)) } // CreateOrgRepoDeprecated create one repository of the organization diff --git a/routers/api/v1/repo/transfer.go b/routers/api/v1/repo/transfer.go index b3913bcd34..0a698383e5 100644 --- a/routers/api/v1/repo/transfer.go +++ b/routers/api/v1/repo/transfer.go @@ -9,6 +9,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -113,10 +114,10 @@ func Transfer(ctx *context.APIContext) { if ctx.Repo.Repository.Status == models.RepositoryPendingTransfer { log.Trace("Repository transfer initiated: %s -> %s", ctx.Repo.Repository.FullName(), newOwner.Name) - ctx.JSON(http.StatusCreated, convert.ToRepo(ctx.Repo.Repository, models.AccessModeAdmin)) + ctx.JSON(http.StatusCreated, convert.ToRepo(ctx.Repo.Repository, perm.AccessModeAdmin)) return } log.Trace("Repository transferred: %s -> %s", ctx.Repo.Repository.FullName(), newOwner.Name) - ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx.Repo.Repository, models.AccessModeAdmin)) + ctx.JSON(http.StatusAccepted, convert.ToRepo(ctx.Repo.Repository, perm.AccessModeAdmin)) } diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index 719e1c0bdc..32291264bc 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -37,7 +38,7 @@ func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defa } else { apiKey.KeyType = "unknown" } - apiKey.ReadOnly = key.Mode == models.AccessModeRead + apiKey.ReadOnly = key.Mode == perm.AccessModeRead return apiKey, nil } diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 0b0b522c15..760032460e 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -37,7 +38,7 @@ func listUserRepos(ctx *context.APIContext, u *user_model.User, private bool) { ctx.Error(http.StatusInternalServerError, "AccessLevel", err) return } - if ctx.IsSigned && ctx.User.IsAdmin || access >= models.AccessModeRead { + if ctx.IsSigned && ctx.User.IsAdmin || access >= perm.AccessModeRead { apiRepos = append(apiRepos, convert.ToRepo(repos[i], access)) } } |