context.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2018 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 context
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/middleware"
  8. )
  9. // Context contains helper functions related to the current request.
  10. type Context struct {
  11. request *http.Request
  12. }
  13. // IsAdminUser checks if the logged user is administrator.
  14. func (c *Context) IsAdminUser() bool {
  15. return c.getContextBoolValue(middleware.IsAdminUserContextKey)
  16. }
  17. // IsAuthenticated returns a boolean if the user is authenticated.
  18. func (c *Context) IsAuthenticated() bool {
  19. return c.getContextBoolValue(middleware.IsAuthenticatedContextKey)
  20. }
  21. // UserID returns the UserID of the logged user.
  22. func (c *Context) UserID() int64 {
  23. return c.getContextIntValue(middleware.UserIDContextKey)
  24. }
  25. // UserTimezone returns the timezone used by the logged user.
  26. func (c *Context) UserTimezone() string {
  27. value := c.getContextStringValue(middleware.UserTimezoneContextKey)
  28. if value == "" {
  29. value = "UTC"
  30. }
  31. return value
  32. }
  33. // UserLanguage get the locale used by the current logged user.
  34. func (c *Context) UserLanguage() string {
  35. language := c.getContextStringValue(middleware.UserLanguageContextKey)
  36. if language == "" {
  37. language = "en_US"
  38. }
  39. return language
  40. }
  41. // UserTheme get the theme used by the current logged user.
  42. func (c *Context) UserTheme() string {
  43. theme := c.getContextStringValue(middleware.UserThemeContextKey)
  44. if theme == "" {
  45. theme = "default"
  46. }
  47. return theme
  48. }
  49. // CSRF returns the current CSRF token.
  50. func (c *Context) CSRF() string {
  51. return c.getContextStringValue(middleware.CSRFContextKey)
  52. }
  53. // SessionID returns the current session ID.
  54. func (c *Context) SessionID() string {
  55. return c.getContextStringValue(middleware.SessionIDContextKey)
  56. }
  57. // UserSessionToken returns the current user session token.
  58. func (c *Context) UserSessionToken() string {
  59. return c.getContextStringValue(middleware.UserSessionTokenContextKey)
  60. }
  61. // OAuth2State returns the current OAuth2 state.
  62. func (c *Context) OAuth2State() string {
  63. return c.getContextStringValue(middleware.OAuth2StateContextKey)
  64. }
  65. // FlashMessage returns the message message if any.
  66. func (c *Context) FlashMessage() string {
  67. return c.getContextStringValue(middleware.FlashMessageContextKey)
  68. }
  69. // FlashErrorMessage returns the message error message if any.
  70. func (c *Context) FlashErrorMessage() string {
  71. return c.getContextStringValue(middleware.FlashErrorMessageContextKey)
  72. }
  73. // PocketRequestToken returns the Pocket Request Token if any.
  74. func (c *Context) PocketRequestToken() string {
  75. return c.getContextStringValue(middleware.PocketRequestTokenContextKey)
  76. }
  77. func (c *Context) getContextStringValue(key *middleware.ContextKey) string {
  78. if v := c.request.Context().Value(key); v != nil {
  79. return v.(string)
  80. }
  81. return ""
  82. }
  83. func (c *Context) getContextBoolValue(key *middleware.ContextKey) bool {
  84. if v := c.request.Context().Value(key); v != nil {
  85. return v.(bool)
  86. }
  87. return false
  88. }
  89. func (c *Context) getContextIntValue(key *middleware.ContextKey) int64 {
  90. if v := c.request.Context().Value(key); v != nil {
  91. return v.(int64)
  92. }
  93. return 0
  94. }
  95. // New creates a new Context.
  96. func New(r *http.Request) *Context {
  97. return &Context{r}
  98. }