aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/core/context.go
blob: 50496b33adb8012b014db1485cb97732cafe44e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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 (
	"net/http"

	"github.com/miniflux/miniflux/logger"
	"github.com/miniflux/miniflux/model"
	"github.com/miniflux/miniflux/server/middleware"
	"github.com/miniflux/miniflux/server/route"
	"github.com/miniflux/miniflux/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 {
	if v := c.request.Context().Value(middleware.IsAdminUserContextKey); v != nil {
		return v.(bool)
	}
	return false
}

// UserTimezone returns the timezone used by the logged user.
func (c *Context) UserTimezone() string {
	if v := c.request.Context().Value(middleware.UserTimezoneContextKey); 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(middleware.IsAuthenticatedContextKey); v != nil {
		return v.(bool)
	}
	return false
}

// UserID returns the UserID of the logged user.
func (c *Context) UserID() int64 {
	if v := c.request.Context().Value(middleware.UserIDContextKey); v != nil {
		return v.(int64)
	}
	return 0
}

// LoggedUser returns all properties related to the logged user.
func (c *Context) LoggedUser() *model.User {
	if c.user == nil {
		var err error
		c.user, err = c.store.UserByID(c.UserID())
		if err != nil {
			logger.Fatal("[Context] %v", err)
		}

		if c.user == nil {
			logger.Fatal("Unable to find user from context")
		}
	}

	return c.user
}

// UserLanguage get the locale used by the current logged user.
func (c *Context) UserLanguage() string {
	user := c.LoggedUser()
	return user.Language
}

// CsrfToken returns the current CSRF token.
func (c *Context) CsrfToken() string {
	if v := c.request.Context().Value(middleware.TokenContextKey); v != nil {
		return v.(string)
	}

	logger.Error("No CSRF token in context!")
	return ""
}

// Route returns the path for the given arguments.
func (c *Context) Route(name string, args ...interface{}) string {
	return route.Path(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}
}