summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/admin
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2022-03-26 10:04:22 +0100
committerGitHub <noreply@github.com>2022-03-26 17:04:22 +0800
commit59b867dc2dfc1ecb0ee703ff44e1be9c5c53cf86 (patch)
tree7114b991554e6e7dcb4123c0aa365c674d8411a0 /routers/api/v1/admin
parentf36701c702dc67011999cfaaf37e002c13e7a87e (diff)
downloadgitea-59b867dc2dfc1ecb0ee703ff44e1be9c5c53cf86.tar.gz
gitea-59b867dc2dfc1ecb0ee703ff44e1be9c5c53cf86.zip
Add `ContextUser` to http request context (#18798)
This PR adds a middleware which sets a ContextUser (like GetUserByParams before) in a single place which can be used by other methods. For routes which represent a repo or org the respective middlewares set the field too. Also fix a bug in modules/context/org.go during refactoring.
Diffstat (limited to 'routers/api/v1/admin')
-rw-r--r--routers/api/v1/admin/org.go8
-rw-r--r--routers/api/v1/admin/repo.go8
-rw-r--r--routers/api/v1/admin/user.go82
3 files changed, 38 insertions, 60 deletions
diff --git a/routers/api/v1/admin/org.go b/routers/api/v1/admin/org.go
index 4ebfe9863c..e4850ac494 100644
--- a/routers/api/v1/admin/org.go
+++ b/routers/api/v1/admin/org.go
@@ -15,7 +15,6 @@ import (
"code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
- "code.gitea.io/gitea/routers/api/v1/user"
"code.gitea.io/gitea/routers/api/v1/utils"
)
@@ -45,11 +44,8 @@ func CreateOrg(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
+
form := web.GetForm(ctx).(*api.CreateOrgOption)
- u := user.GetUserByParams(ctx)
- if ctx.Written() {
- return
- }
visibility := api.VisibleTypePublic
if form.Visibility != "" {
@@ -67,7 +63,7 @@ func CreateOrg(ctx *context.APIContext) {
Visibility: visibility,
}
- if err := models.CreateOrganization(org, u); err != nil {
+ if err := models.CreateOrganization(org, ctx.ContextUser); err != nil {
if user_model.IsErrUserAlreadyExist(err) ||
db.IsErrNameReserved(err) ||
db.IsErrNameCharsNotAllowed(err) ||
diff --git a/routers/api/v1/admin/repo.go b/routers/api/v1/admin/repo.go
index 467f8a22ff..712ced89c9 100644
--- a/routers/api/v1/admin/repo.go
+++ b/routers/api/v1/admin/repo.go
@@ -9,7 +9,6 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/repo"
- "code.gitea.io/gitea/routers/api/v1/user"
)
// CreateRepo api for creating a repository
@@ -42,11 +41,8 @@ func CreateRepo(ctx *context.APIContext) {
// "$ref": "#/responses/error"
// "422":
// "$ref": "#/responses/validationError"
+
form := web.GetForm(ctx).(*api.CreateRepoOption)
- owner := user.GetUserByParams(ctx)
- if ctx.Written() {
- return
- }
- repo.CreateUserRepo(ctx, owner, *form)
+ repo.CreateUserRepo(ctx, ctx.ContextUser, *form)
}
diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go
index 677950664d..da44c23213 100644
--- a/routers/api/v1/admin/user.go
+++ b/routers/api/v1/admin/user.go
@@ -73,6 +73,7 @@ func CreateUser(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
+
form := web.GetForm(ctx).(*api.CreateUserOption)
u := &user_model.User{
@@ -163,13 +164,10 @@ func EditUser(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
+
form := web.GetForm(ctx).(*api.EditUserOption)
- u := user.GetUserByParams(ctx)
- if ctx.Written() {
- return
- }
- parseAuthSource(ctx, u, form.SourceID, form.LoginName)
+ parseAuthSource(ctx, ctx.ContextUser, form.SourceID, form.LoginName)
if ctx.Written() {
return
}
@@ -193,24 +191,24 @@ func EditUser(ctx *context.APIContext) {
ctx.Error(http.StatusBadRequest, "PasswordPwned", errors.New("PasswordPwned"))
return
}
- if u.Salt, err = user_model.GetUserSalt(); err != nil {
+ if ctx.ContextUser.Salt, err = user_model.GetUserSalt(); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)
return
}
- if err = u.SetPassword(form.Password); err != nil {
+ if err = ctx.ContextUser.SetPassword(form.Password); err != nil {
ctx.InternalServerError(err)
return
}
}
if form.MustChangePassword != nil {
- u.MustChangePassword = *form.MustChangePassword
+ ctx.ContextUser.MustChangePassword = *form.MustChangePassword
}
- u.LoginName = form.LoginName
+ ctx.ContextUser.LoginName = form.LoginName
if form.FullName != nil {
- u.FullName = *form.FullName
+ ctx.ContextUser.FullName = *form.FullName
}
var emailChanged bool
if form.Email != nil {
@@ -225,47 +223,47 @@ func EditUser(ctx *context.APIContext) {
return
}
- emailChanged = !strings.EqualFold(u.Email, email)
- u.Email = email
+ emailChanged = !strings.EqualFold(ctx.ContextUser.Email, email)
+ ctx.ContextUser.Email = email
}
if form.Website != nil {
- u.Website = *form.Website
+ ctx.ContextUser.Website = *form.Website
}
if form.Location != nil {
- u.Location = *form.Location
+ ctx.ContextUser.Location = *form.Location
}
if form.Description != nil {
- u.Description = *form.Description
+ ctx.ContextUser.Description = *form.Description
}
if form.Active != nil {
- u.IsActive = *form.Active
+ ctx.ContextUser.IsActive = *form.Active
}
if len(form.Visibility) != 0 {
- u.Visibility = api.VisibilityModes[form.Visibility]
+ ctx.ContextUser.Visibility = api.VisibilityModes[form.Visibility]
}
if form.Admin != nil {
- u.IsAdmin = *form.Admin
+ ctx.ContextUser.IsAdmin = *form.Admin
}
if form.AllowGitHook != nil {
- u.AllowGitHook = *form.AllowGitHook
+ ctx.ContextUser.AllowGitHook = *form.AllowGitHook
}
if form.AllowImportLocal != nil {
- u.AllowImportLocal = *form.AllowImportLocal
+ ctx.ContextUser.AllowImportLocal = *form.AllowImportLocal
}
if form.MaxRepoCreation != nil {
- u.MaxRepoCreation = *form.MaxRepoCreation
+ ctx.ContextUser.MaxRepoCreation = *form.MaxRepoCreation
}
if form.AllowCreateOrganization != nil {
- u.AllowCreateOrganization = *form.AllowCreateOrganization
+ ctx.ContextUser.AllowCreateOrganization = *form.AllowCreateOrganization
}
if form.ProhibitLogin != nil {
- u.ProhibitLogin = *form.ProhibitLogin
+ ctx.ContextUser.ProhibitLogin = *form.ProhibitLogin
}
if form.Restricted != nil {
- u.IsRestricted = *form.Restricted
+ ctx.ContextUser.IsRestricted = *form.Restricted
}
- if err := user_model.UpdateUser(u, emailChanged); err != nil {
+ if err := user_model.UpdateUser(ctx.ContextUser, emailChanged); err != nil {
if user_model.IsErrEmailAlreadyUsed(err) ||
user_model.IsErrEmailCharIsNotSupported(err) ||
user_model.IsErrEmailInvalid(err) {
@@ -275,9 +273,9 @@ func EditUser(ctx *context.APIContext) {
}
return
}
- log.Trace("Account profile updated by admin (%s): %s", ctx.Doer.Name, u.Name)
+ log.Trace("Account profile updated by admin (%s): %s", ctx.Doer.Name, ctx.ContextUser.Name)
- ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.Doer))
+ ctx.JSON(http.StatusOK, convert.ToUser(ctx.ContextUser, ctx.Doer))
}
// DeleteUser api for deleting a user
@@ -301,17 +299,12 @@ func DeleteUser(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
- u := user.GetUserByParams(ctx)
- if ctx.Written() {
- return
- }
-
- if u.IsOrganization() {
- ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("%s is an organization not a user", u.Name))
+ if ctx.ContextUser.IsOrganization() {
+ ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("%s is an organization not a user", ctx.ContextUser.Name))
return
}
- if err := user_service.DeleteUser(u); err != nil {
+ if err := user_service.DeleteUser(ctx.ContextUser); err != nil {
if models.IsErrUserOwnRepos(err) ||
models.IsErrUserHasOrgs(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err)
@@ -320,7 +313,7 @@ func DeleteUser(ctx *context.APIContext) {
}
return
}
- log.Trace("Account deleted by admin(%s): %s", ctx.Doer.Name, u.Name)
+ log.Trace("Account deleted by admin(%s): %s", ctx.Doer.Name, ctx.ContextUser.Name)
ctx.Status(http.StatusNoContent)
}
@@ -351,12 +344,10 @@ func CreatePublicKey(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
+
form := web.GetForm(ctx).(*api.CreateKeyOption)
- u := user.GetUserByParams(ctx)
- if ctx.Written() {
- return
- }
- user.CreateUserPublicKey(ctx, *form, u.ID)
+
+ user.CreateUserPublicKey(ctx, *form, ctx.ContextUser.ID)
}
// DeleteUserPublicKey api for deleting a user's public key
@@ -386,12 +377,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
- u := user.GetUserByParams(ctx)
- if ctx.Written() {
- return
- }
-
- if err := asymkey_service.DeletePublicKey(u, ctx.ParamsInt64(":id")); err != nil {
+ if err := asymkey_service.DeletePublicKey(ctx.ContextUser, ctx.ParamsInt64(":id")); err != nil {
if asymkey_model.IsErrKeyNotExist(err) {
ctx.NotFound()
} else if asymkey_model.IsErrKeyAccessDenied(err) {
@@ -401,7 +387,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
}
return
}
- log.Trace("Key deleted by admin(%s): %s", ctx.Doer.Name, u.Name)
+ log.Trace("Key deleted by admin(%s): %s", ctx.Doer.Name, ctx.ContextUser.Name)
ctx.Status(http.StatusNoContent)
}