context.go 2.8 KB

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