-
Notifications
You must be signed in to change notification settings - Fork 0
/
poems.go
241 lines (236 loc) · 8.17 KB
/
poems.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
package adminfrontend
import (
"fmt"
"net/http"
"strconv"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"golang.org/x/net/xsrftoken"
"webimizer.dev/poem/admin"
"webimizer.dev/poem/poems"
"webimizer.dev/poem/runtime"
"webimizer.dev/webimizer"
)
type poemsTemplateParams struct {
CategoriesTitle string // categories page link title
PoemsTitle string // poems page link title
PageTitle string // page title
HomeTitle string // home page title
DomainTitle string // domain title
LogoutTitle string // logout title
CopyrightText string // footer copyright text
UserEmail string // current user email
Message string // form error message
Categories map[int32]*poems.Category // Categories map
Poems map[int32]*poems.Poem // Poems map
SubmitButton string // poem create form submit button text
ActionUrl string // poem create form action url
XsrfToken string // poem create form xsrf token hidden field
}
func (p *adminFrontendCmd) renderPoemsPage(session *sessions.Session, rw http.ResponseWriter, r *http.Request) {
var (
hashKey = []byte(p.hashKey)
cryptoKey = []byte(p.cryptoKey)
)
xsrf := xsrftoken.Generate(p.hashKey, session.ID, "add_poem")
session.Values["xsrf_token"] = xsrf
secureXsrf, err := securecookie.New(*reverseBytes(hashKey), *reverseBytes(cryptoKey)).Encode("xsrf_token", xsrf)
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
rw.Header().Set("Cache-Control", "no-store, must-revalidate")
rw.Header().Set("Pragma", "no-cache")
obj := &poemsTemplateParams{
HomeTitle: "Dashboard",
LogoutTitle: "Logout",
CategoriesTitle: "Categories",
PoemsTitle: "Poems",
DomainTitle: "Domain",
PageTitle: "Poems | Poem CMS",
CopyrightText: "Copyright © 2022 Vaclovas Lapinskis",
UserEmail: session.Values["email"].(string),
SubmitButton: "Add new poem",
ActionUrl: "/poems",
XsrfToken: secureXsrf,
}
categories, err := p.grpcGetCategories(&poems.CategoriesRequest{
Status: poems.CategoriesRequest_PUBLISHED,
UserId: session.Values["userId"].(int64)})
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
obj.Categories = categories.Categories
poems, err := p.grpcGetPoems(&poems.PoemsRequest{
UserId: session.Values["userId"].(int64)})
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
obj.Poems = poems.Poems
for _, v := range session.Flashes() {
obj.Message = v.(string)
}
err = session.Save(r, rw)
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
output, err := runtime.TemplateParse(templates, "template/poems.html", obj)
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
fmt.Fprint(rw, output)
}
func (p *adminFrontendCmd) addPoemsPageHandler() {
http.Handle("/poems", webimizer.HttpHandlerStruct{
Handler: webimizer.HttpHandler(func(rw http.ResponseWriter, r *http.Request) {
var (
hashKey = []byte(p.hashKey)
cryptoKey = []byte(p.cryptoKey)
)
session, err := store.Get(r, "sid")
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
if v, found := session.Values["userLoggedIn"].(bool); found && v {
webimizer.Get(rw, r, func(rw http.ResponseWriter, r *http.Request) {
p.renderPoemsPage(session, rw, r)
})
webimizer.Post(rw, r, func(rw http.ResponseWriter, r *http.Request) {
err = r.ParseForm()
if err != nil {
errorMsg(rw, err, http.StatusBadRequest)
return
}
action := r.FormValue("action")
if action == "" {
errorMsg(rw, fmt.Errorf("action parameter is required"), http.StatusBadRequest)
return
}
token := r.FormValue("xsrf_token")
title := r.FormValue("title")
text := r.FormValue("text")
category_id := r.FormValue("category_id")
poem_id := r.FormValue("poem_id")
if (token == "" || title == "" || category_id == "" || text == "") && action == "create" {
session.AddFlash("Please enter all form fields")
p.renderPoemsPage(session, rw, r)
return
}
if (token == "" || title == "" || category_id == "" || text == "" || poem_id == "") && action == "update" {
session.AddFlash("Please enter all form fields")
p.renderPoemsPage(session, rw, r)
return
}
if (token == "" || poem_id == "") && action == "delete" {
session.AddFlash("Please enter all form fields")
p.renderPoemsPage(session, rw, r)
return
}
var realToken string
securecookie.New(*reverseBytes(hashKey), *reverseBytes(cryptoKey)).Decode("xsrf_token", token, &realToken)
if realToken == "" {
errorMsg(rw, fmt.Errorf("cannot decode xsrf_token value"), http.StatusBadRequest)
return
}
if _, exists := session.Values["xsrf_token"]; !exists {
errorMsg(rw, fmt.Errorf("no xsrf_token value stored in current session"), http.StatusBadRequest)
return
}
sessToken := session.Values["xsrf_token"].(string)
if sessToken != realToken {
errorMsg(rw, fmt.Errorf("xsrf_token value is not valid for this session"), http.StatusBadRequest)
return
}
valid := xsrftoken.Valid(realToken, p.hashKey, session.ID, "add_poem")
if valid {
switch action {
case "create":
categoryId, err := strconv.ParseInt(category_id, 10, 32)
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
response, err := p.grpcAddPoem(&admin.AdminPoem{
Title: title,
Text: text,
CategoryId: int32(categoryId),
UserId: session.Values["userId"].(int64)})
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
if !response.Success {
session.AddFlash("Cannot add new poem")
}
p.renderPoemsPage(session, rw, r)
break
case "delete":
poemId, err := strconv.ParseInt(poem_id, 10, 32)
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
response, err := p.grpcDeletePoem(&admin.DeletePoemRequest{
PoemId: int32(poemId),
UserId: session.Values["userId"].(int64)})
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
if !response.Success {
session.AddFlash("Cannot delete poem")
}
p.renderPoemsPage(session, rw, r)
break
case "update":
categoryId, err := strconv.ParseInt(category_id, 10, 32)
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
poemId, err := strconv.ParseInt(poem_id, 10, 32)
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
response, err := p.grpcEditPoem(&admin.AdminPoemEdit{
PoemId: int32(poemId),
Title: title,
Text: text,
CategoryId: int32(categoryId),
UserId: session.Values["userId"].(int64)})
if err != nil {
errorMsg(rw, err, http.StatusInternalServerError)
return
}
if !response.Success {
session.AddFlash("Cannot update poem")
}
p.renderPoemsPage(session, rw, r)
break
default:
break
}
} else {
errorMsg(rw, fmt.Errorf("xsrf_token expired"), http.StatusBadRequest)
return
}
})
return
} else {
session.Save(r, rw)
rw.Header().Set("Cache-Control", "no-store, must-revalidate")
rw.Header().Set("Pragma", "no-cache")
http.Redirect(rw, r, "/login", http.StatusFound)
return
}
}), // webimizer.HttpHandler call only if method is allowed
NotAllowHandler: webimizer.HttpNotAllowHandler(httpNotAllowFunc), // webimizer.HtttpNotAllowHandler call if method is not allowed
AllowedMethods: []string{"GET", "POST"}, // define allowed methods
}.Build())
}