user.go 810 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2018 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "gopkg.in/macaron.v1"
  7. "gogs.io/gogs/internal/db"
  8. "gogs.io/gogs/internal/db/errors"
  9. )
  10. // ParamsUser is the wrapper type of the target user defined by URL parameter, namely ':username'.
  11. type ParamsUser struct {
  12. *db.User
  13. }
  14. // InjectParamsUser returns a handler that retrieves target user based on URL parameter ':username',
  15. // and injects it as *ParamsUser.
  16. func InjectParamsUser() macaron.Handler {
  17. return func(c *Context) {
  18. user, err := db.GetUserByName(c.Params(":username"))
  19. if err != nil {
  20. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  21. return
  22. }
  23. c.Map(&ParamsUser{user})
  24. }
  25. }