context.go 2.8 KB

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