aboutsummaryrefslogtreecommitdiffhomepage
path: root/http/context/context.go
blob: 7a434b5fd3697bd9af741e1f1a2bc4f4d44f0c77 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2018 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 context // import "miniflux.app/http/context"

import (
	"net/http"

	"miniflux.app/middleware"
)

// Context contains helper functions related to the current request.
type Context struct {
	request *http.Request
}

// IsAdminUser checks if the logged user is administrator.
func (c *Context) IsAdminUser() bool {
	return c.getContextBoolValue(middleware.IsAdminUserContextKey)
}

// IsAuthenticated returns a boolean if the user is authenticated.
func (c *Context) IsAuthenticated() bool {
	return c.getContextBoolValue(middleware.IsAuthenticatedContextKey)
}

// UserID returns the UserID of the logged user.
func (c *Context) UserID() int64 {
	return c.getContextIntValue(middleware.UserIDContextKey)
}

// UserTimezone returns the timezone used by the logged user.
func (c *Context) UserTimezone() string {
	value := c.getContextStringValue(middleware.UserTimezoneContextKey)
	if value == "" {
		value = "UTC"
	}
	return value
}

// UserLanguage get the locale used by the current logged user.
func (c *Context) UserLanguage() string {
	language := c.getContextStringValue(middleware.UserLanguageContextKey)
	if language == "" {
		language = "en_US"
	}
	return language
}

// UserTheme get the theme used by the current logged user.
func (c *Context) UserTheme() string {
	theme := c.getContextStringValue(middleware.UserThemeContextKey)
	if theme == "" {
		theme = "default"
	}
	return theme
}

// CSRF returns the current CSRF token.
func (c *Context) CSRF() string {
	return c.getContextStringValue(middleware.CSRFContextKey)
}

// SessionID returns the current session ID.
func (c *Context) SessionID() string {
	return c.getContextStringValue(middleware.SessionIDContextKey)
}

// UserSessionToken returns the current user session token.
func (c *Context) UserSessionToken() string {
	return c.getContextStringValue(middleware.UserSessionTokenContextKey)
}

// OAuth2State returns the current OAuth2 state.
func (c *Context) OAuth2State() string {
	return c.getContextStringValue(middleware.OAuth2StateContextKey)
}

// FlashMessage returns the message message if any.
func (c *Context) FlashMessage() string {
	return c.getContextStringValue(middleware.FlashMessageContextKey)
}

// FlashErrorMessage returns the message error message if any.
func (c *Context) FlashErrorMessage() string {
	return c.getContextStringValue(middleware.FlashErrorMessageContextKey)
}

// PocketRequestToken returns the Pocket Request Token if any.
func (c *Context) PocketRequestToken() string {
	return c.getContextStringValue(middleware.PocketRequestTokenContextKey)
}

func (c *Context) getContextStringValue(key middleware.ContextKey) string {
	if v := c.request.Context().Value(key); v != nil {
		return v.(string)
	}

	return ""
}

func (c *Context) getContextBoolValue(key middleware.ContextKey) bool {
	if v := c.request.Context().Value(key); v != nil {
		return v.(bool)
	}

	return false
}

func (c *Context) getContextIntValue(key middleware.ContextKey) int64 {
	if v := c.request.Context().Value(key); v != nil {
		return v.(int64)
	}

	return 0
}

// New creates a new Context.
func New(r *http.Request) *Context {
	return &Context{r}
}