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.8 kB

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