context.go 2.8 KB

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