context.go 2.5 KB

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