-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
61 lines (54 loc) · 1.57 KB
/
util.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
// Package controller - Content managed by Project Forge, see [projectforge.md] for details.
package controller
import (
"fmt"
"strings"
"github.com/pkg/errors"
"projectforge.dev/projectforge/app/controller/csession"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/app/util"
)
func ERsp(msg string, args ...any) (string, error) {
return "", errors.Errorf(msg, args...)
}
func FlashAndRedir(success bool, msg string, redir string, ps *cutil.PageState) (string, error) {
status := util.KeyError
if success {
status = "success"
}
msgFmt := fmt.Sprintf("%s:%s", status, msg)
currStr := ps.Session.GetStringOpt(csession.WebFlashKey)
if currStr == "" {
currStr = msgFmt
} else {
curr := util.StringSplitAndTrim(currStr, ";")
curr = append(curr, msgFmt)
currStr = strings.Join(curr, ";")
}
ps.Session[csession.WebFlashKey] = currStr
if err := csession.SaveSession(ps.W, ps.Session, ps.Logger); err != nil {
return "", errors.Wrap(err, "unable to save flash session")
}
if strings.HasPrefix(redir, "/") {
return redir, nil
}
if strings.HasPrefix(redir, "http") {
ps.Logger.Warn("flash redirect attempted for non-local request")
return "/", nil
}
return redir, nil
}
func ReturnToReferrer(msg string, dflt string, ps *cutil.PageState) (string, error) {
refer := ""
referX, ok := ps.Session[csession.ReferKey]
if ok {
refer, ok = referX.(string)
if ok {
_ = csession.RemoveFromSession(csession.ReferKey, ps.W, ps.Session, ps.Logger)
}
}
if refer == "" {
refer = dflt
}
return FlashAndRedir(true, msg, refer, ps)
}