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