-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.go
208 lines (175 loc) · 5.19 KB
/
views.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
package profile
import (
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/readeck/readeck/internal/auth"
"github.com/readeck/readeck/internal/auth/tokens"
"github.com/readeck/readeck/internal/auth/users"
"github.com/readeck/readeck/internal/server"
"github.com/readeck/readeck/pkg/form"
)
// profileViews is an HTTP handler for the user profile web views
type profileViews struct {
chi.Router
*profileAPI
}
// newProfileViews returns an new instance of ProfileViews
func newProfileViews(api *profileAPI) *profileViews {
r := api.srv.AuthenticatedRouter()
v := &profileViews{r, api}
r.With(api.srv.WithPermission("read")).Group(func(r chi.Router) {
r.Get("/", v.userProfile)
r.Get("/password", v.userPassword)
r.With(api.withTokenList).Get("/tokens", v.tokenList)
r.With(api.withToken).Get("/tokens/{uid}", v.tokenInfo)
})
r.With(api.srv.WithPermission("write")).Group(func(r chi.Router) {
r.Post("/", v.userProfile)
r.Post("/password", v.userPassword)
r.Post("/tokens", v.tokenCreate)
r.With(api.withToken).Post("/tokens/{uid}", v.tokenInfo)
r.With(api.withToken).Post("/tokens/{uid}/delete", v.tokenDelete)
})
return v
}
// userProfile handles GET and POST requests on /profile.
func (v *profileViews) userProfile(w http.ResponseWriter, r *http.Request) {
user := auth.GetRequestUser(r)
pf := &users.ProfileForm{}
f := form.NewForm(pf)
if r.Method == http.MethodGet {
pf.Username = &user.Username
pf.Email = &user.Email
}
if r.Method == http.MethodPost {
form.Bind(f, r)
if f.IsValid() {
if _, err := v.UpdateProfile(user, pf); err != nil {
v.srv.AddFlash(w, r, "error", "Error while updating profile")
} else {
// Set the new seed in the session.
// We needn't save the session since AddFlash does it already.
sess := v.srv.GetSession(r)
sess.Values["s"] = user.Seed
v.srv.AddFlash(w, r, "success", "Profile updated")
}
v.srv.Redirect(w, r, "profile")
return
}
}
ctx := server.TC{
"Form": f,
}
v.srv.RenderTemplate(w, r, 200, "profile/index", ctx)
}
// userPassword handles GET and POST requests on /profile/password
func (v *profileViews) userPassword(w http.ResponseWriter, r *http.Request) {
pf := &users.PasswordForm{}
f := form.NewForm(pf)
if r.Method == http.MethodPost {
user := auth.GetRequestUser(r)
pf.SetUser(f, user)
form.Bind(f, r)
if f.IsValid() {
if err := v.UpdatePassword(user, pf); err != nil {
v.srv.AddFlash(w, r, "error", "Error while updating your password")
} else {
// Set the new seed in the session.
// We needn't save the session since AddFlash does it already.
sess := v.srv.GetSession(r)
sess.Values["s"] = user.Seed
v.srv.AddFlash(w, r, "success", "Your password was changed.")
}
v.srv.Redirect(w, r, "password")
return
}
}
ctx := server.TC{
"Form": f,
}
v.srv.RenderTemplate(w, r, 200, "profile/password", ctx)
}
func (v *profileViews) tokenList(w http.ResponseWriter, r *http.Request) {
tl := r.Context().Value(ctxTokenListKey{}).(tokenList)
ctx := server.TC{
"Pagination": tl.Pagination,
"Tokens": tl.Items,
}
v.srv.RenderTemplate(w, r, 200, "profile/token_list", ctx)
}
func (v *profileViews) tokenCreate(w http.ResponseWriter, r *http.Request) {
user := auth.GetRequestUser(r)
t := &tokens.Token{
UserID: &user.ID,
IsEnabled: true,
Application: "internal",
}
if err := tokens.Tokens.Create(t); err != nil {
v.srv.Log(r).WithError(err).Error("server error")
v.srv.AddFlash(w, r, "error", "An error append while creating your token.")
v.srv.Redirect(w, r, "tokens")
return
}
v.srv.AddFlash(w, r, "success", "New token created.")
v.srv.Redirect(w, r, ".", t.UID)
}
func (v *profileViews) tokenInfo(w http.ResponseWriter, r *http.Request) {
ti := r.Context().Value(ctxtTokenKey{}).(tokenItem)
tf := &tokenForm{}
f := form.NewForm(tf)
if r.Method == http.MethodGet {
tf.Expires = ti.Token.Expires
tf.IsEnabled = ti.Token.IsEnabled
}
if r.Method == http.MethodPost {
form.Bind(f, r)
if f.IsValid() {
if tf.Expires != nil && tf.Expires.IsZero() {
tf.Expires = nil
}
ti.Token.IsEnabled = tf.IsEnabled
ti.Token.Expires = tf.Expires
if err := ti.Token.Save(); err != nil {
v.srv.Log(r).WithError(err).Error("server error")
v.srv.AddFlash(w, r, "error", "Error while updating token")
} else {
v.srv.AddFlash(w, r, "success", "Token was updated.")
}
v.srv.Redirect(w, r, ti.UID)
return
}
}
jwt, err := tokens.NewJwtToken(ti.UID)
if err != nil {
v.srv.Status(w, r, http.StatusInternalServerError)
return
}
ctx := server.TC{
"Token": ti,
"JWT": jwt,
"Form": f,
}
v.srv.RenderTemplate(w, r, 200, "profile/token", ctx)
}
func (v *profileViews) tokenDelete(w http.ResponseWriter, r *http.Request) {
df := &deleteForm{}
f := form.NewForm(df)
form.Bind(f, r)
ti := r.Context().Value(ctxtTokenKey{}).(tokenItem)
defer func() {
v.srv.Redirect(w, r, "..", ti.UID)
}()
if df.Cancel {
tokenTimers.Stop(ti.Token.ID)
return
}
tokenTimers.Start(ti.Token.ID, 20*time.Second, func() {
log := v.srv.Log(r).WithField("token", ti.UID)
if err := ti.Token.Delete(); err != nil {
log.WithError(err).Error("removing token")
return
}
log.Debug("token removed")
})
}