You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package context
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/context"
  10. )
  11. // UserAssignmentWeb returns a middleware to handle context-user assignment for web routes
  12. func UserAssignmentWeb() func(ctx *context.Context) {
  13. return func(ctx *context.Context) {
  14. errorFn := func(status int, title string, obj any) {
  15. err, ok := obj.(error)
  16. if !ok {
  17. err = fmt.Errorf("%s", obj)
  18. }
  19. if status == http.StatusNotFound {
  20. ctx.NotFound(title, err)
  21. } else {
  22. ctx.ServerError(title, err)
  23. }
  24. }
  25. ctx.ContextUser = userAssignment(ctx.Base, ctx.Doer, errorFn)
  26. ctx.Data["ContextUser"] = ctx.ContextUser
  27. }
  28. }
  29. // UserIDAssignmentAPI returns a middleware to handle context-user assignment for api routes
  30. func UserIDAssignmentAPI() func(ctx *context.APIContext) {
  31. return func(ctx *context.APIContext) {
  32. userID := ctx.ParamsInt64(":user-id")
  33. if ctx.IsSigned && ctx.Doer.ID == userID {
  34. ctx.ContextUser = ctx.Doer
  35. } else {
  36. var err error
  37. ctx.ContextUser, err = user_model.GetUserByID(ctx, userID)
  38. if err != nil {
  39. if user_model.IsErrUserNotExist(err) {
  40. ctx.Error(http.StatusNotFound, "GetUserByID", err)
  41. } else {
  42. ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
  43. }
  44. }
  45. }
  46. }
  47. }
  48. // UserAssignmentAPI returns a middleware to handle context-user assignment for api routes
  49. func UserAssignmentAPI() func(ctx *context.APIContext) {
  50. return func(ctx *context.APIContext) {
  51. ctx.ContextUser = userAssignment(ctx.Base, ctx.Doer, ctx.Error)
  52. }
  53. }
  54. func userAssignment(ctx *context.Base, doer *user_model.User, errCb func(int, string, any)) (contextUser *user_model.User) {
  55. username := ctx.Params(":username")
  56. if doer != nil && doer.LowerName == strings.ToLower(username) {
  57. contextUser = doer
  58. } else {
  59. var err error
  60. contextUser, err = user_model.GetUserByName(ctx, username)
  61. if err != nil {
  62. if user_model.IsErrUserNotExist(err) {
  63. if redirectUserID, err := user_model.LookupUserRedirect(ctx, username); err == nil {
  64. context.RedirectToUser(ctx, username, redirectUserID)
  65. } else if user_model.IsErrUserRedirectNotExist(err) {
  66. errCb(http.StatusNotFound, "GetUserByName", err)
  67. } else {
  68. errCb(http.StatusInternalServerError, "LookupUserRedirect", err)
  69. }
  70. } else {
  71. errCb(http.StatusInternalServerError, "GetUserByName", err)
  72. }
  73. }
  74. }
  75. return contextUser
  76. }