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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. }
  27. }
  28. // UserIDAssignmentAPI returns a middleware to handle context-user assignment for api routes
  29. func UserIDAssignmentAPI() func(ctx *context.APIContext) {
  30. return func(ctx *context.APIContext) {
  31. userID := ctx.ParamsInt64(":user-id")
  32. if ctx.IsSigned && ctx.Doer.ID == userID {
  33. ctx.ContextUser = ctx.Doer
  34. } else {
  35. var err error
  36. ctx.ContextUser, err = user_model.GetUserByID(ctx, userID)
  37. if err != nil {
  38. if user_model.IsErrUserNotExist(err) {
  39. ctx.Error(http.StatusNotFound, "GetUserByID", err)
  40. } else {
  41. ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
  42. }
  43. }
  44. }
  45. }
  46. }
  47. // UserAssignmentAPI returns a middleware to handle context-user assignment for api routes
  48. func UserAssignmentAPI() func(ctx *context.APIContext) {
  49. return func(ctx *context.APIContext) {
  50. ctx.ContextUser = userAssignment(ctx.Base, ctx.Doer, ctx.Error)
  51. }
  52. }
  53. func userAssignment(ctx *context.Base, doer *user_model.User, errCb func(int, string, any)) (contextUser *user_model.User) {
  54. username := ctx.Params(":username")
  55. if doer != nil && doer.LowerName == strings.ToLower(username) {
  56. contextUser = doer
  57. } else {
  58. var err error
  59. contextUser, err = user_model.GetUserByName(ctx, username)
  60. if err != nil {
  61. if user_model.IsErrUserNotExist(err) {
  62. if redirectUserID, err := user_model.LookupUserRedirect(username); err == nil {
  63. context.RedirectToUser(ctx, username, redirectUserID)
  64. } else if user_model.IsErrUserRedirectNotExist(err) {
  65. errCb(http.StatusNotFound, "GetUserByName", err)
  66. } else {
  67. errCb(http.StatusInternalServerError, "LookupUserRedirect", err)
  68. }
  69. } else {
  70. errCb(http.StatusInternalServerError, "GetUserByName", err)
  71. }
  72. }
  73. }
  74. return contextUser
  75. }