context.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 core
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/helper"
  8. "github.com/miniflux/miniflux/locale"
  9. "github.com/miniflux/miniflux/logger"
  10. "github.com/miniflux/miniflux/model"
  11. "github.com/miniflux/miniflux/server/middleware"
  12. "github.com/miniflux/miniflux/server/route"
  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. user := c.LoggedUser()
  71. return user.Language
  72. }
  73. // Translate translates a message in the current language.
  74. func (c *Context) Translate(message string, args ...interface{}) string {
  75. return c.translator.GetLanguage(c.UserLanguage()).Get(message, args...)
  76. }
  77. // CSRF returns the current CSRF token.
  78. func (c *Context) CSRF() string {
  79. return c.getContextStringValue(middleware.CSRFContextKey)
  80. }
  81. // SessionID returns the current session ID.
  82. func (c *Context) SessionID() string {
  83. return c.getContextStringValue(middleware.SessionIDContextKey)
  84. }
  85. // UserSessionToken returns the current user session token.
  86. func (c *Context) UserSessionToken() string {
  87. return c.getContextStringValue(middleware.UserSessionTokenContextKey)
  88. }
  89. // OAuth2State returns the current OAuth2 state.
  90. func (c *Context) OAuth2State() string {
  91. return c.getContextStringValue(middleware.OAuth2StateContextKey)
  92. }
  93. // GenerateOAuth2State generate a new OAuth2 state.
  94. func (c *Context) GenerateOAuth2State() string {
  95. state := helper.GenerateRandomString(32)
  96. c.store.UpdateSessionField(c.SessionID(), "oauth2_state", state)
  97. return state
  98. }
  99. // SetFlashMessage defines a new flash message.
  100. func (c *Context) SetFlashMessage(message string) {
  101. c.store.UpdateSessionField(c.SessionID(), "flash_message", message)
  102. }
  103. // FlashMessage returns the flash message and remove it.
  104. func (c *Context) FlashMessage() string {
  105. message := c.getContextStringValue(middleware.FlashMessageContextKey)
  106. c.store.UpdateSessionField(c.SessionID(), "flash_message", "")
  107. return message
  108. }
  109. // SetFlashErrorMessage defines a new flash error message.
  110. func (c *Context) SetFlashErrorMessage(message string) {
  111. c.store.UpdateSessionField(c.SessionID(), "flash_error_message", message)
  112. }
  113. // FlashErrorMessage returns the error flash message and remove it.
  114. func (c *Context) FlashErrorMessage() string {
  115. message := c.getContextStringValue(middleware.FlashMessageContextKey)
  116. c.store.UpdateSessionField(c.SessionID(), "flash_error_message", "")
  117. return message
  118. }
  119. func (c *Context) getContextStringValue(key *middleware.ContextKey) string {
  120. if v := c.request.Context().Value(key); v != nil {
  121. return v.(string)
  122. }
  123. logger.Error("[Core:Context] Missing key: %s", key)
  124. return ""
  125. }
  126. // Route returns the path for the given arguments.
  127. func (c *Context) Route(name string, args ...interface{}) string {
  128. return route.Path(c.router, name, args...)
  129. }
  130. // NewContext creates a new Context.
  131. func NewContext(w http.ResponseWriter, r *http.Request, store *storage.Storage, router *mux.Router, translator *locale.Translator) *Context {
  132. return &Context{writer: w, request: r, store: store, router: router, translator: translator}
  133. }