diff options
author | 2017-11-19 21:10:04 -0800 | |
---|---|---|
committer | 2017-11-19 22:01:46 -0800 | |
commit | 8ffb773f43c8dc54801ca1d111854e7e881c93c9 (patch) | |
tree | 38133a2fc612597a75fed1d13e5b4042f58a2b7e /server/core/context.go | |
download | v2-8ffb773f43c8dc54801ca1d111854e7e881c93c9.tar.gz v2-8ffb773f43c8dc54801ca1d111854e7e881c93c9.tar.zst v2-8ffb773f43c8dc54801ca1d111854e7e881c93c9.zip |
First commit
Diffstat (limited to 'server/core/context.go')
-rw-r--r-- | server/core/context.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/server/core/context.go b/server/core/context.go new file mode 100644 index 00000000..c9d2dc2e --- /dev/null +++ b/server/core/context.go @@ -0,0 +1,99 @@ +// Copyright 2017 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package core + +import ( + "github.com/miniflux/miniflux2/model" + "github.com/miniflux/miniflux2/server/route" + "github.com/miniflux/miniflux2/storage" + "log" + "net/http" + + "github.com/gorilla/mux" +) + +// Context contains helper functions related to the current request. +type Context struct { + writer http.ResponseWriter + request *http.Request + store *storage.Storage + router *mux.Router + user *model.User +} + +// IsAdminUser checks if the logged user is administrator. +func (c *Context) IsAdminUser() bool { + if v := c.request.Context().Value("IsAdminUser"); v != nil { + return v.(bool) + } + return false +} + +// GetUserTimezone returns the timezone used by the logged user. +func (c *Context) GetUserTimezone() string { + if v := c.request.Context().Value("UserTimezone"); v != nil { + return v.(string) + } + return "UTC" +} + +// IsAuthenticated returns a boolean if the user is authenticated. +func (c *Context) IsAuthenticated() bool { + if v := c.request.Context().Value("IsAuthenticated"); v != nil { + return v.(bool) + } + return false +} + +// GetUserID returns the UserID of the logged user. +func (c *Context) GetUserID() int64 { + if v := c.request.Context().Value("UserId"); v != nil { + return v.(int64) + } + return 0 +} + +// GetLoggedUser returns all properties related to the logged user. +func (c *Context) GetLoggedUser() *model.User { + if c.user == nil { + var err error + c.user, err = c.store.GetUserById(c.GetUserID()) + if err != nil { + log.Fatalln(err) + } + + if c.user == nil { + log.Fatalln("Unable to find user from context") + } + } + + return c.user +} + +// GetUserLanguage get the locale used by the current logged user. +func (c *Context) GetUserLanguage() string { + user := c.GetLoggedUser() + return user.Language +} + +// GetCsrfToken returns the current CSRF token. +func (c *Context) GetCsrfToken() string { + if v := c.request.Context().Value("CsrfToken"); v != nil { + return v.(string) + } + + log.Println("No CSRF token in context!") + return "" +} + +// GetRoute returns the path for the given arguments. +func (c *Context) GetRoute(name string, args ...interface{}) string { + return route.GetRoute(c.router, name, args...) +} + +// NewContext creates a new Context. +func NewContext(w http.ResponseWriter, r *http.Request, store *storage.Storage, router *mux.Router) *Context { + return &Context{writer: w, request: r, store: store, router: router} +} |