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

context.go 2.4 kB

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