context.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package handler
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/crypto"
  8. "github.com/miniflux/miniflux/http/route"
  9. "github.com/miniflux/miniflux/locale"
  10. "github.com/miniflux/miniflux/logger"
  11. "github.com/miniflux/miniflux/middleware"
  12. "github.com/miniflux/miniflux/model"
  13. "github.com/miniflux/miniflux/storage"
  14. "github.com/gorilla/mux"
  15. )
  16. // Context contains helper functions related to the current request.
  17. type Context struct {
  18. writer http.ResponseWriter
  19. request *http.Request
  20. store *storage.Storage
  21. router *mux.Router
  22. user *model.User
  23. translator *locale.Translator
  24. }
  25. // IsAdminUser checks if the logged user is administrator.
  26. func (c *Context) IsAdminUser() bool {
  27. if v := c.request.Context().Value(middleware.IsAdminUserContextKey); v != nil {
  28. return v.(bool)
  29. }
  30. return false
  31. }
  32. // UserTimezone returns the timezone used by the logged user.
  33. func (c *Context) UserTimezone() string {
  34. value := c.getContextStringValue(middleware.UserTimezoneContextKey)
  35. if value == "" {
  36. value = "UTC"
  37. }
  38. return value
  39. }
  40. // IsAuthenticated returns a boolean if the user is authenticated.
  41. func (c *Context) IsAuthenticated() bool {
  42. if v := c.request.Context().Value(middleware.IsAuthenticatedContextKey); v != nil {
  43. return v.(bool)
  44. }
  45. return false
  46. }
  47. // UserID returns the UserID of the logged user.
  48. func (c *Context) UserID() int64 {
  49. if v := c.request.Context().Value(middleware.UserIDContextKey); v != nil {
  50. return v.(int64)
  51. }
  52. return 0
  53. }
  54. // LoggedUser returns all properties related to the logged user.
  55. func (c *Context) LoggedUser() *model.User {
  56. if c.user == nil {
  57. var err error
  58. c.user, err = c.store.UserByID(c.UserID())
  59. if err != nil {
  60. logger.Fatal("[Context] %v", err)
  61. }
  62. if c.user == nil {
  63. logger.Fatal("Unable to find user from context")
  64. }
  65. }
  66. return c.user
  67. }
  68. // UserLanguage get the locale used by the current logged user.
  69. func (c *Context) UserLanguage() string {
  70. if c.IsAuthenticated() {
  71. user := c.LoggedUser()
  72. return user.Language
  73. }
  74. return c.getContextStringValue(middleware.UserLanguageContextKey)
  75. }
  76. // Translate translates a message in the current language.
  77. func (c *Context) Translate(message string, args ...interface{}) string {
  78. return c.translator.GetLanguage(c.UserLanguage()).Get(message, args...)
  79. }
  80. // CSRF returns the current CSRF token.
  81. func (c *Context) CSRF() string {
  82. return c.getContextStringValue(middleware.CSRFContextKey)
  83. }
  84. // SessionID returns the current session ID.
  85. func (c *Context) SessionID() string {
  86. return c.getContextStringValue(middleware.SessionIDContextKey)
  87. }
  88. // UserSessionToken returns the current user session token.
  89. func (c *Context) UserSessionToken() string {
  90. return c.getContextStringValue(middleware.UserSessionTokenContextKey)
  91. }
  92. // OAuth2State returns the current OAuth2 state.
  93. func (c *Context) OAuth2State() string {
  94. return c.getContextStringValue(middleware.OAuth2StateContextKey)
  95. }
  96. // GenerateOAuth2State generate a new OAuth2 state.
  97. func (c *Context) GenerateOAuth2State() string {
  98. state := crypto.GenerateRandomString(32)
  99. c.store.UpdateSessionField(c.SessionID(), "oauth2_state", state)
  100. return state
  101. }
  102. // SetFlashMessage defines a new flash message.
  103. func (c *Context) SetFlashMessage(message string) {
  104. c.store.UpdateSessionField(c.SessionID(), "flash_message", message)
  105. }
  106. // FlashMessage returns the flash message and remove it.
  107. func (c *Context) FlashMessage() string {
  108. message := c.getContextStringValue(middleware.FlashMessageContextKey)
  109. c.store.UpdateSessionField(c.SessionID(), "flash_message", "")
  110. return message
  111. }
  112. // SetFlashErrorMessage defines a new flash error message.
  113. func (c *Context) SetFlashErrorMessage(message string) {
  114. c.store.UpdateSessionField(c.SessionID(), "flash_error_message", message)
  115. }
  116. // FlashErrorMessage returns the error flash message and remove it.
  117. func (c *Context) FlashErrorMessage() string {
  118. message := c.getContextStringValue(middleware.FlashErrorMessageContextKey)
  119. c.store.UpdateSessionField(c.SessionID(), "flash_error_message", "")
  120. return message
  121. }
  122. func (c *Context) getContextStringValue(key *middleware.ContextKey) string {
  123. if v := c.request.Context().Value(key); v != nil {
  124. return v.(string)
  125. }
  126. return ""
  127. }
  128. // Route returns the path for the given arguments.
  129. func (c *Context) Route(name string, args ...interface{}) string {
  130. return route.Path(c.router, name, args...)
  131. }
  132. // NewContext creates a new Context.
  133. func NewContext(r *http.Request, store *storage.Storage, router *mux.Router, translator *locale.Translator) *Context {
  134. return &Context{request: r, store: store, router: router, translator: translator}
  135. }