Cookies: Provide a mechanism for per user control over cookies (#61566)

This commit is contained in:
Emil Tullstedt
2023-02-21 11:19:07 +01:00
committed by GitHub
parent 5eaaf9b9b7
commit 0caacb3333
13 changed files with 207 additions and 97 deletions
+39 -16
View File
@@ -7,9 +7,16 @@ import (
"errors"
"fmt"
"time"
"github.com/grafana/grafana/pkg/util/errutil"
)
var ErrPrefNotFound = errors.New("preference not found")
var ErrUnknownCookieType = errutil.NewBase(
errutil.StatusBadRequest,
"preferences.unknownCookieType",
errutil.WithPublicMessage("Got an unknown cookie preference type. Expected a set containing one or more of 'functional', 'performance', or 'analytics'}"),
)
type Preference struct {
ID int64 `xorm:"pk autoincr 'id'" db:"id"`
@@ -27,6 +34,15 @@ type Preference struct {
JSONData *PreferenceJSONData `xorm:"json_data" db:"json_data"`
}
func (p Preference) Cookies(typ string) bool {
if p.JSONData == nil || p.JSONData.CookiePreferences == nil {
return false
}
_, ok := p.JSONData.CookiePreferences[typ]
return ok
}
type GetPreferenceWithDefaultsQuery struct {
Teams []int64
OrgID int64
@@ -44,13 +60,14 @@ type SavePreferenceCommand struct {
OrgID int64
TeamID int64
HomeDashboardID int64 `json:"homeDashboardId,omitempty"`
HomeDashboardUID *string `json:"homeDashboardUID,omitempty"`
Timezone string `json:"timezone,omitempty"`
WeekStart string `json:"weekStart,omitempty"`
Theme string `json:"theme,omitempty"`
Language string `json:"language,omitempty"`
QueryHistory *QueryHistoryPreference `json:"queryHistory,omitempty"`
HomeDashboardID int64 `json:"homeDashboardId,omitempty"`
HomeDashboardUID *string `json:"homeDashboardUID,omitempty"`
Timezone string `json:"timezone,omitempty"`
WeekStart string `json:"weekStart,omitempty"`
Theme string `json:"theme,omitempty"`
Language string `json:"language,omitempty"`
QueryHistory *QueryHistoryPreference `json:"queryHistory,omitempty"`
CookiePreferences []CookieType `json:"cookiePreferences,omitempty"`
}
type PatchPreferenceCommand struct {
@@ -58,18 +75,20 @@ type PatchPreferenceCommand struct {
OrgID int64
TeamID int64
HomeDashboardID *int64 `json:"homeDashboardId,omitempty"`
HomeDashboardUID *string `json:"homeDashboardUID,omitempty"`
Timezone *string `json:"timezone,omitempty"`
WeekStart *string `json:"weekStart,omitempty"`
Theme *string `json:"theme,omitempty"`
Language *string `json:"language,omitempty"`
QueryHistory *QueryHistoryPreference `json:"queryHistory,omitempty"`
HomeDashboardID *int64 `json:"homeDashboardId,omitempty"`
HomeDashboardUID *string `json:"homeDashboardUID,omitempty"`
Timezone *string `json:"timezone,omitempty"`
WeekStart *string `json:"weekStart,omitempty"`
Theme *string `json:"theme,omitempty"`
Language *string `json:"language,omitempty"`
QueryHistory *QueryHistoryPreference `json:"queryHistory,omitempty"`
CookiePreferences []CookieType `json:"cookiePreferences,omitempty"`
}
type PreferenceJSONData struct {
Language string `json:"language"`
QueryHistory QueryHistoryPreference `json:"queryHistory"`
Language string `json:"language"`
QueryHistory QueryHistoryPreference `json:"queryHistory"`
CookiePreferences map[string]struct{} `json:"cookiePreferences"`
}
type QueryHistoryPreference struct {
@@ -112,3 +131,7 @@ func (j *PreferenceJSONData) ToDB() ([]byte, error) {
}
func (p Preference) TableName() string { return "preferences" }
// swagger:model
// Enum: analytics,performance,functional
type CookieType string