context.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/logger"
  8. "github.com/miniflux/miniflux/model"
  9. "github.com/miniflux/miniflux/server/middleware"
  10. "github.com/miniflux/miniflux/server/route"
  11. "github.com/miniflux/miniflux/storage"
  12. "github.com/gorilla/mux"
  13. )
  14. // Context contains helper functions related to the current request.
  15. type Context struct {
  16. writer http.ResponseWriter
  17. request *http.Request
  18. store *storage.Storage
  19. router *mux.Router
  20. user *model.User
  21. }
  22. // IsAdminUser checks if the logged user is administrator.
  23. func (c *Context) IsAdminUser() bool {
  24. if v := c.request.Context().Value(middleware.IsAdminUserContextKey); v != nil {
  25. return v.(bool)
  26. }
  27. return false
  28. }
  29. // UserTimezone returns the timezone used by the logged user.
  30. func (c *Context) UserTimezone() string {
  31. if v := c.request.Context().Value(middleware.UserTimezoneContextKey); v != nil {
  32. return v.(string)
  33. }
  34. return "UTC"
  35. }
  36. // IsAuthenticated returns a boolean if the user is authenticated.
  37. func (c *Context) IsAuthenticated() bool {
  38. if v := c.request.Context().Value(middleware.IsAuthenticatedContextKey); v != nil {
  39. return v.(bool)
  40. }
  41. return false
  42. }
  43. // UserID returns the UserID of the logged user.
  44. func (c *Context) UserID() int64 {
  45. if v := c.request.Context().Value(middleware.UserIDContextKey); v != nil {
  46. return v.(int64)
  47. }
  48. return 0
  49. }
  50. // LoggedUser returns all properties related to the logged user.
  51. func (c *Context) LoggedUser() *model.User {
  52. if c.user == nil {
  53. var err error
  54. c.user, err = c.store.UserByID(c.UserID())
  55. if err != nil {
  56. logger.Fatal("[Context] %v", err)
  57. }
  58. if c.user == nil {
  59. logger.Fatal("Unable to find user from context")
  60. }
  61. }
  62. return c.user
  63. }
  64. // UserLanguage get the locale used by the current logged user.
  65. func (c *Context) UserLanguage() string {
  66. user := c.LoggedUser()
  67. return user.Language
  68. }
  69. // CsrfToken returns the current CSRF token.
  70. func (c *Context) CsrfToken() string {
  71. if v := c.request.Context().Value(middleware.TokenContextKey); v != nil {
  72. return v.(string)
  73. }
  74. logger.Error("No CSRF token in context!")
  75. return ""
  76. }
  77. // Route returns the path for the given arguments.
  78. func (c *Context) Route(name string, args ...interface{}) string {
  79. return route.Path(c.router, name, args...)
  80. }
  81. // NewContext creates a new Context.
  82. func NewContext(w http.ResponseWriter, r *http.Request, store *storage.Storage, router *mux.Router) *Context {
  83. return &Context{writer: w, request: r, store: store, router: router}
  84. }