miniflux/server/core/context.go

102 lines
2.6 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// 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 (
2017-11-22 03:37:08 +01:00
"log"
"net/http"
2017-11-20 06:10:04 +01:00
"github.com/miniflux/miniflux2/model"
2017-11-28 06:30:04 +01:00
"github.com/miniflux/miniflux2/server/middleware"
2017-11-20 06:10:04 +01:00
"github.com/miniflux/miniflux2/server/route"
"github.com/miniflux/miniflux2/storage"
"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 {
2017-11-28 06:30:04 +01:00
if v := c.request.Context().Value(middleware.IsAdminUserContextKey); v != nil {
2017-11-20 06:10:04 +01:00
return v.(bool)
}
return false
}
2017-11-22 03:37:08 +01:00
// UserTimezone returns the timezone used by the logged user.
func (c *Context) UserTimezone() string {
2017-11-28 06:30:04 +01:00
if v := c.request.Context().Value(middleware.UserTimezoneContextKey); v != nil {
2017-11-20 06:10:04 +01:00
return v.(string)
}
return "UTC"
}
// IsAuthenticated returns a boolean if the user is authenticated.
func (c *Context) IsAuthenticated() bool {
2017-11-28 06:30:04 +01:00
if v := c.request.Context().Value(middleware.IsAuthenticatedContextKey); v != nil {
2017-11-20 06:10:04 +01:00
return v.(bool)
}
return false
}
2017-11-22 03:37:08 +01:00
// UserID returns the UserID of the logged user.
func (c *Context) UserID() int64 {
2017-11-28 06:30:04 +01:00
if v := c.request.Context().Value(middleware.UserIDContextKey); v != nil {
2017-11-20 06:10:04 +01:00
return v.(int64)
}
return 0
}
2017-11-22 03:37:08 +01:00
// LoggedUser returns all properties related to the logged user.
func (c *Context) LoggedUser() *model.User {
2017-11-20 06:10:04 +01:00
if c.user == nil {
var err error
2017-11-28 06:30:04 +01:00
c.user, err = c.store.UserByID(c.UserID())
2017-11-20 06:10:04 +01:00
if err != nil {
log.Fatalln(err)
}
if c.user == nil {
log.Fatalln("Unable to find user from context")
}
}
return c.user
}
2017-11-22 03:37:08 +01:00
// UserLanguage get the locale used by the current logged user.
func (c *Context) UserLanguage() string {
user := c.LoggedUser()
2017-11-20 06:10:04 +01:00
return user.Language
}
2017-11-22 03:37:08 +01:00
// CsrfToken returns the current CSRF token.
func (c *Context) CsrfToken() string {
if v := c.request.Context().Value(middleware.TokenContextKey); v != nil {
2017-11-20 06:10:04 +01:00
return v.(string)
}
log.Println("No CSRF token in context!")
return ""
}
2017-11-22 03:37:08 +01:00
// Route returns the path for the given arguments.
func (c *Context) Route(name string, args ...interface{}) string {
2017-11-28 06:30:04 +01:00
return route.Path(c.router, name, args...)
2017-11-20 06:10:04 +01:00
}
// 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}
}