-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
80 lines (66 loc) · 2.16 KB
/
profile.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
// Package clib - Content managed by Project Forge, see [projectforge.md] for details.
package clib
import (
"net/http"
"net/url"
"github.com/pkg/errors"
"admini.dev/admini/app"
"admini.dev/admini/app/controller"
"admini.dev/admini/app/controller/csession"
"admini.dev/admini/app/controller/cutil"
"admini.dev/admini/app/lib/theme"
"admini.dev/admini/views/vprofile"
)
func Profile(w http.ResponseWriter, r *http.Request) {
controller.Act("profile", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
return profileAction(w, r, as, ps)
})
}
func ProfileSite(w http.ResponseWriter, r *http.Request) {
controller.ActSite("profile", w, r, func(as *app.State, ps *cutil.PageState) (string, error) {
return profileAction(w, r, as, ps)
})
}
func profileAction(w http.ResponseWriter, r *http.Request, as *app.State, ps *cutil.PageState) (string, error) {
ps.SetTitleAndData("Profile", ps.Profile)
thm := as.Themes.Get(ps.Profile.Theme, ps.Logger)
prvs, err := as.Auth.Providers(ps.Logger)
if err != nil {
return "", errors.Wrap(err, "can't load providers")
}
redir := "/"
ref := r.Header.Get("Referer")
if ref != "" {
u, err := url.Parse(ref)
if err == nil && u != nil && u.Path != cutil.DefaultProfilePath {
redir = u.Path
}
}
ps.DefaultNavIcon = "profile"
page := &vprofile.Profile{Profile: ps.Profile, Theme: thm, Providers: prvs, Referrer: redir}
return controller.Render(r, as, page, ps, "Profile")
}
func ProfileSave(w http.ResponseWriter, r *http.Request) {
controller.Act("profile.save", w, r, func(_ *app.State, ps *cutil.PageState) (string, error) {
frm, err := cutil.ParseForm(r, ps.RequestBody)
if err != nil {
return "", err
}
n := ps.Profile.Clone()
referrerDefault := frm.GetStringOpt("referrer")
if referrerDefault == "" {
referrerDefault = cutil.DefaultProfilePath
}
n.Name = frm.GetStringOpt("name")
n.Mode = frm.GetStringOpt("mode")
n.Theme = frm.GetStringOpt("theme")
if n.Theme == theme.Default.Key {
n.Theme = ""
}
err = csession.SaveProfile(n, w, ps.Session, ps.Logger)
if err != nil {
return "", err
}
return controller.ReturnToReferrer("Saved profile", referrerDefault, ps)
})
}