context.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2014 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 middleware
  5. import (
  6. "fmt"
  7. "net/http"
  8. "time"
  9. "github.com/codegangsta/martini"
  10. "github.com/martini-contrib/sessions"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/log"
  14. )
  15. // Context represents context of a request.
  16. type Context struct {
  17. *Render
  18. c martini.Context
  19. p martini.Params
  20. Req *http.Request
  21. Res http.ResponseWriter
  22. Session sessions.Session
  23. User *models.User
  24. IsSigned bool
  25. Repo struct {
  26. IsValid bool
  27. IsOwner bool
  28. Repository *models.Repository
  29. Owner *models.User
  30. }
  31. }
  32. // Query querys form parameter.
  33. func (ctx *Context) Query(name string) string {
  34. ctx.Req.ParseForm()
  35. return ctx.Req.Form.Get(name)
  36. }
  37. // func (ctx *Context) Param(name string) string {
  38. // return ctx.p[name]
  39. // }
  40. // HasError returns true if error occurs in form validation.
  41. func (ctx *Context) HasError() bool {
  42. hasErr, ok := ctx.Data["HasError"]
  43. if !ok {
  44. return false
  45. }
  46. return hasErr.(bool)
  47. }
  48. // RenderWithErr used for page has form validation but need to prompt error to users.
  49. func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) {
  50. ctx.Data["HasError"] = true
  51. ctx.Data["ErrorMsg"] = msg
  52. auth.AssignForm(form, ctx.Data)
  53. ctx.HTML(200, tpl, ctx.Data)
  54. }
  55. // Handle handles and logs error by given status.
  56. func (ctx *Context) Handle(status int, title string, err error) {
  57. log.Error("%s: %v", title, err)
  58. if martini.Dev == martini.Prod {
  59. ctx.HTML(500, "status/500", ctx.Data)
  60. return
  61. }
  62. ctx.Data["ErrorMsg"] = err
  63. ctx.HTML(status, fmt.Sprintf("status/%d", status), ctx.Data)
  64. }
  65. // InitContext initializes a classic context for a request.
  66. func InitContext() martini.Handler {
  67. return func(res http.ResponseWriter, r *http.Request, c martini.Context,
  68. session sessions.Session, rd *Render) {
  69. ctx := &Context{
  70. c: c,
  71. // p: p,
  72. Req: r,
  73. Res: res,
  74. Session: session,
  75. Render: rd,
  76. }
  77. // Get user from session if logined.
  78. user := auth.SignedInUser(session)
  79. ctx.User = user
  80. ctx.IsSigned = user != nil
  81. ctx.Data["IsSigned"] = ctx.IsSigned
  82. if user != nil {
  83. ctx.Data["SignedUser"] = user
  84. ctx.Data["SignedUserId"] = user.Id
  85. ctx.Data["SignedUserName"] = user.LowerName
  86. }
  87. ctx.Data["PageStartTime"] = time.Now()
  88. c.Map(ctx)
  89. c.Next()
  90. }
  91. }