-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
366 lines (316 loc) · 9.22 KB
/
settings.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright (c) 2023 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package settings
import (
"fmt"
"html"
"net/http"
"sort"
"strings"
"github.com/go-chi/chi/v5"
"github.com/maruel/natural"
"github.com/microcosm-cc/bluemonday"
"github.com/urfave/cli/v2"
"github.com/go-corelibs/x-text/message"
beContext "github.com/go-enjin/be/pkg/context"
berrs "github.com/go-enjin/be/pkg/errors"
"github.com/go-enjin/be/pkg/feature"
"github.com/go-enjin/be/pkg/log"
"github.com/go-enjin/be/pkg/maps"
"github.com/go-enjin/be/pkg/menu"
"github.com/go-enjin/be/pkg/request"
"github.com/go-enjin/be/pkg/userbase"
"github.com/go-enjin/be/types/site"
)
const (
SettingPanelNonceName = "site-settings--nonce"
SettingPanelNonceKey = "site-settings--form"
)
var (
_ Feature = (*CFeature)(nil)
_ MakeFeature = (*CFeature)(nil)
)
const Tag feature.Tag = "site-settings"
type Feature interface {
feature.SiteFeature
}
type MakeFeature interface {
feature.SiteMakeFeature[MakeFeature]
Make() Feature
}
type CFeature struct {
site.CSiteFeature[MakeFeature]
}
func New() MakeFeature {
return NewTagged(Tag)
}
func NewTagged(tag feature.Tag) MakeFeature {
f := new(CFeature)
f.Init(f)
f.PackageTag = Tag
f.FeatureTag = tag
f.SetSiteFeatureKey("settings")
f.SetSiteFeatureIcon("fa-solid fa-gear")
f.SetSiteFeatureLabel(func(printer *message.Printer) (label string) {
label = printer.Sprintf("Settings")
return
})
f.CSiteFeature.Construct(f)
return f
}
func (f *CFeature) Init(this interface{}) {
f.CSiteFeature.Init(this)
return
}
func (f *CFeature) Make() (feat Feature) {
return f
}
func (f *CFeature) Build(b feature.Buildable) (err error) {
if err = f.CSiteFeature.Build(b); err != nil {
return
}
return
}
func (f *CFeature) Startup(ctx *cli.Context) (err error) {
if err = f.CSiteFeature.Startup(ctx); err != nil {
return
}
return
}
func (f *CFeature) Shutdown() {
f.CFeature.Shutdown()
}
func (f *CFeature) UserActions() (list feature.Actions) {
list = f.CSiteFeature.UserActions()
return
}
func (f *CFeature) SiteFeatureMenu(r *http.Request) (m menu.Menu) {
info := f.SiteFeatureInfo(r)
m = menu.Menu{
{
Text: info.Label,
Href: f.SiteFeaturePath(),
Icon: info.Icon,
},
}
return
}
func (f *CFeature) SiteSettings(r *http.Request) (settings map[string]beContext.Fields, order []string) {
settings = make(map[string]beContext.Fields)
for _, sf := range f.Site().SiteFeatures() {
if fields := sf.SiteSettingsFields(r); fields.Len() > 0 {
settings[sf.SiteFeatureKey()] = fields
}
}
parsers := f.Enjin.PageContextParsers()
for group, fields := range settings {
for key, field := range fields {
if field.Tab == "" {
field.Tab = "page"
}
if field.Category == "" {
field.Category = "general"
}
if fn, ok := parsers[field.Format]; ok {
settings[group][key].Parse = fn
} else {
panic(fmt.Errorf("invalid field.Format: %q; possible values: %+v", field.Format, maps.SortedKeys(parsers)))
}
}
}
order = maps.Keys(settings)
sort.Slice(order, func(i, j int) (less bool) {
a, b := order[i], order[j]
if less = a == "profile" && b != "profile"; less {
return
}
less = natural.Less(a, b)
return
})
return
}
func (f *CFeature) RouteSiteFeature(r chi.Router) {
r.Post("/", f.ReceiveSettingsChanges)
r.Get("/", f.ServeSettingsPage)
for _, sf := range f.Site().SiteFeatures() {
sfName := sf.SiteFeatureKey()
settingsPath := f.SiteFeaturePath()
if s, h := sf.SiteSettingsPanel(settingsPath + "/" + sfName); s != nil || h != nil {
sfPath := "/" + sfName
log.DebugF("%q feature routing settings page for: %q", f.Tag(), sfPath)
r.Route(sfPath, func(r chi.Router) {
r.Post("/*", h)
r.Get("/*", s)
})
}
}
}
func (f *CFeature) ServeSettingsPage(w http.ResponseWriter, r *http.Request) {
// TODO: route settings panels changes if r.URL.Path != settings path
au := userbase.GetCurrentUser(r)
matter := au.GetSettings()
f.RenderSettingsWith(matter, w, r)
}
func (f *CFeature) RenderSettingsWith(matter beContext.Context, w http.ResponseWriter, r *http.Request) {
t := f.SiteFeatureTheme()
printer := message.GetPrinter(r)
var order []string
panels := make(map[string]string)
settingsPath := f.SiteFeaturePath()
for _, sf := range f.Site().SiteFeatures() {
if s, _ := sf.SiteSettingsPanel(settingsPath); s != nil {
name := sf.SiteFeatureLabel(printer)
order = append(order, name)
panels[name] = settingsPath + "/" + sf.SiteFeatureKey()
}
}
settings, keys := f.SiteSettings(r)
ctx := beContext.Context{
"Title": printer.Sprintf(`Site Settings`),
"UserSettings": matter,
"SiteSettings": settings,
"SiteSettingsKeys": keys,
"SiteSettingsPanelKeys": order,
"SiteSettingsPanelLookup": panels,
"FormAction": f.SiteFeaturePath(),
"Nonces": feature.Nonces{
{Name: SettingPanelNonceName, Key: SettingPanelNonceKey},
},
}
if err := f.Site().PrepareAndServePage("site", "settings", f.SiteFeaturePath(), t, w, r, ctx); err != nil {
log.ErrorRF(r, "error preparing %v feature page: %v", f.Tag(), err)
f.Enjin.ServeInternalServerError(w, r)
return
}
}
func (f *CFeature) ReceiveSettingsChanges(w http.ResponseWriter, r *http.Request) {
// TODO: route settings panels changes if r.URL.Path != settings path
eid := userbase.GetCurrentEID(r)
printer := message.GetPrinter(r)
if nonce := request.SafeQueryFormValue(r, SettingPanelNonceName); nonce != "" {
if !f.Enjin.VerifyNonce(SettingPanelNonceKey, nonce) {
f.Site().PushErrorNotice(eid, true, berrs.FormExpiredError(printer))
f.Enjin.ServeRedirect(f.SiteFeaturePath(), w, r)
return
}
} else {
f.Site().PushErrorNotice(eid, true, berrs.IncompleteFormError(printer))
f.Enjin.ServeRedirect(f.SiteFeaturePath(), w, r)
return
}
action, _ := feature.ParseEditorOpKey(r.PostFormValue("submit"))
if action = strings.ToLower(strings.TrimSpace(action)); action == "cancel" {
f.Site().PushInfoNotice(eid, true, printer.Sprintf("Changes discarded."))
f.Enjin.ServeRedirect(f.SiteFeaturePath(), w, r)
return
}
form := map[string]interface{}{}
for _, k := range maps.SortedKeys(r.Form) {
v := r.Form[k]
for i := 0; i < len(v); i++ {
v[i] = html.UnescapeString(v[i])
}
switch len(v) {
case 0: // nop
case 1:
_ = maps.Set(form, k, v[0])
case 2:
if v[0] == v[1] {
_ = maps.Set(form, k, v[0])
} else {
_ = maps.Set(form, k, v)
}
default:
_ = maps.Set(form, k, v)
}
}
var formMatter beContext.Context
if bc, ok := form["matter"].(beContext.Context); ok {
formMatter = bc
} else if fm, ok := form["matter"].(map[string]interface{}); ok {
formMatter = fm
} else {
// nop
}
strict := bluemonday.StrictPolicy()
var parseCustom func(v interface{}) (parsed interface{})
parseCustom = func(v interface{}) (parsed interface{}) {
if value, ok := v.(string); ok {
value = strict.Sanitize(value)
parsed = html.UnescapeString(value)
} else if list, ok := v.([]interface{}); ok {
var items []interface{}
for _, item := range list {
items = append(items, parseCustom(item))
}
parsed = items
} else if dictionary, ok := v.(map[string]interface{}); ok {
cleaned := make(map[string]interface{})
for dk, dv := range dictionary {
cleaned[dk] = parseCustom(dv)
}
parsed = cleaned
}
return
}
errs := make(map[string]error)
settings, settingGroups := f.SiteSettings(r)
lookup := func(key string) (field *beContext.Field, ok bool) {
for _, group := range settingGroups {
if field, ok = settings[group].Lookup(key); ok {
return
}
}
return
}
matter := beContext.Context{}
for k, v := range formMatter.AsDeepKeyed() {
if field, ok := lookup(k); ok {
if field.Input != "checkbox" {
if vi, ee := field.Parse(field, v); ee != nil {
errs[field.Key] = ee
} else {
//field.Value = vi
_ = maps.Set(matter, k, vi)
}
}
} else {
log.WarnRF(r, "strict policy for custom field: %q", k)
_ = maps.Set(matter, k, parseCustom(v))
}
}
for _, group := range settingGroups {
for _, field := range settings[group] {
if field.Input == "checkbox" {
fv := r.FormValue(".matter." + field.Key)
_ = maps.Set(matter, "."+field.Key, fv != "")
}
}
}
var err error
var au feature.User
if au, err = f.Site().SiteUsers().RetrieveUser(r, eid); err != nil {
au = userbase.GetCurrentUser(r)
}
uCtx := au.GetSettings()
uCtx.KebabKeys()
uCtx.ApplySpecific(matter)
if err = f.Site().SiteUsers().SetUserContext(r, eid, beContext.Context{"settings": uCtx}); err != nil {
log.ErrorRF(r, "error saving user settings changes: %v - %v", eid, err)
f.Site().PushErrorNotice(eid, true, berrs.UnexpectedError(printer))
f.RenderSettingsWith(matter, w, r)
return
}
f.Enjin.ServeRedirect(f.SiteFeaturePath(), w, r)
}