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.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. Repo struct {
  28. IsValid bool
  29. IsOwner bool
  30. Repository *models.Repository
  31. Owner *models.User
  32. }
  33. }
  34. // Query querys form parameter.
  35. func (ctx *Context) Query(name string) string {
  36. ctx.Req.ParseForm()
  37. return ctx.Req.Form.Get(name)
  38. }
  39. // func (ctx *Context) Param(name string) string {
  40. // return ctx.p[name]
  41. // }
  42. // HasError returns true if error occurs in form validation.
  43. func (ctx *Context) HasError() bool {
  44. hasErr, ok := ctx.Data["HasError"]
  45. if !ok {
  46. return false
  47. }
  48. return hasErr.(bool)
  49. }
  50. // RenderWithErr used for page has form validation but need to prompt error to users.
  51. func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) {
  52. ctx.Data["HasError"] = true
  53. ctx.Data["ErrorMsg"] = msg
  54. auth.AssignForm(form, ctx.Data)
  55. ctx.Render.HTML(200, tpl, ctx.Data)
  56. }
  57. // Handle handles and logs error by given status.
  58. func (ctx *Context) Handle(status int, title string, err error) {
  59. ctx.Data["ErrorMsg"] = err
  60. log.Error("%s: %v", title, err)
  61. ctx.Render.HTML(status, fmt.Sprintf("status/%d", status), ctx.Data)
  62. }
  63. // InitContext initializes a classic context for a request.
  64. func InitContext() martini.Handler {
  65. return func(res http.ResponseWriter, r *http.Request, c martini.Context,
  66. session sessions.Session, rd render.Render) {
  67. data := base.TmplData{}
  68. ctx := &Context{
  69. c: c,
  70. // p: p,
  71. Req: r,
  72. Res: res,
  73. Session: session,
  74. Data: data,
  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. data["IsSigned"] = ctx.IsSigned
  82. if user != nil {
  83. data["SignedUser"] = user
  84. data["SignedUserId"] = user.Id
  85. data["SignedUserName"] = user.LowerName
  86. }
  87. c.Map(ctx)
  88. c.Map(data)
  89. c.Next()
  90. }
  91. }