forked from q6r/masteringdecisions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ballot.go
430 lines (373 loc) · 11.4 KB
/
ballot.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package main
import (
"fmt"
"net/http"
"strconv"
"sync"
"time"
"github.com/gin-gonic/gin"
)
var mutex sync.Mutex
// Ballot represent a ballot that belong
// in a decision
type Ballot struct {
BallotID int `db:"ballot_id" json:"ballot_id"`
DecisionID int `db:"decision_id" json:"decision_id"`
Secret string `db:"secret" json:"secret"`
Name string `db:"name" json:"name" binding:"required"`
Email string `db:"email" json:"email" binding:"required"`
Sent bool `db:"sent" json:"sent"`
}
// BallotAllInfo is a struct to hold
// all necessary information of a ballot
type BallotAllInfo struct {
Name string `json:"name"`
Email string `json:"email"`
URLDecision string `json:"url"`
Votes []Vote `json:"votes"`
Ratings []Rating `json:"rating"`
Sent bool `json:"sent"`
}
// HBallotCreate create a ballot that belongs
// to a decision
func HBallotCreate(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "Invalid decision_id"})
return
}
var b Ballot
if err := c.Bind(&b); err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "invalid ballot object"})
return
}
b.DecisionID = did // inherited
mutex.Lock()
defer mutex.Unlock()
err = b.Save()
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
result := gin.H{"ballot": b}
c.Writer.Header().Set("Location", fmt.Sprintf("/ballot/%d", b.BallotID))
ServeResult(c, "ballot_create.js", result)
// Send email to ballot, and set confirmation
// flag on success
title, body := GenerateInviteTemplate(b)
go func(b *Ballot) {
if b == nil {
return
}
if err := Send(body, title, b.Email); err != nil {
b.Sent = false
return
}
b.Sent = true
if _, err := dbmap.Update(b); err != nil {
return
}
}(&b)
}
// HBallotCreateSilent create a ballot without sending
// it an invitation
func HBallotCreateSilent(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "Invalid decision_id"})
return
}
var b Ballot
if err := c.Bind(&b); err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "invalid ballot object"})
return
}
b.DecisionID = did // inherited
mutex.Lock()
defer mutex.Unlock()
err = b.Save()
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
result := gin.H{"ballot": b}
c.Writer.Header().Set("Location", fmt.Sprintf("/ballot/%d", b.BallotID))
ServeResult(c, "ballot_create.js", result)
}
// GenerateInviteTemplate generate the template for email
// invitations
func GenerateInviteTemplate(b Ballot) (title string, body string) {
title = fmt.Sprintf("%s's ballot", b.Name)
body = fmt.Sprintf("<html><body>Hello %s, you have been invited to participate in a decision <a href=\"http://localhost:9999/decision/%d/ballot/%d/login/%s\">click here to vote</a>.</body></html>",
b.Name, b.DecisionID, b.BallotID, b.Secret)
return title, body
}
// HBallotInvite invites a specific ballot via email
// to participate in its decision
func HBallotInvite(c *gin.Context) {
did := c.Param("decision_id")
bid := c.Param("ballot_id")
var b Ballot
err := dbmap.SelectOne(&b,
"SELECT * FROM ballot where ballot_id=$1 and decision_id=$2", bid, did)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find ballot %v for decision %v", bid, did)})
return
}
c.JSON(http.StatusOK, gin.H{"result": "invited"})
// Send email to ballot, and set confirmation
// flag on success
title, body := GenerateInviteTemplate(b)
go func(b *Ballot) {
if b == nil {
return
}
if err := Send(body, title, b.Email); err != nil {
b.Sent = false
return
}
b.Sent = true
if _, err := dbmap.Update(b); err != nil {
return
}
}(&b)
}
// HBallotUpdate updates a ballot
func HBallotUpdate(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
bid, err := strconv.Atoi(c.Param("ballot_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
var b Ballot
err = dbmap.SelectOne(&b, "SELECT * FROM ballot WHERE decision_id=$1 and ballot_id=$2", did, bid)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("ballot %d for decision %d not found", bid, did)})
return
}
var json Ballot
err = c.Bind(&json)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": "Unable to parse ballot object"})
return
}
secret := HashPassword(fmt.Sprintf("b_%d_d_%d", bid, did))
newBallot := Ballot{
BallotID: bid,
DecisionID: did,
Secret: secret,
Name: json.Name,
Email: json.Email,
Sent: b.Sent,
}
_, err = dbmap.Update(&newBallot)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to update ballot %d for decision %d", bid, did)})
return
}
result := gin.H{"ballot": newBallot}
ServeResult(c, "ballot_update.js", result)
}
// HBallotDelete deletes a ballot from a decision
func HBallotDelete(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
bid, err := strconv.Atoi(c.Param("ballot_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
b := &Ballot{BallotID: bid, DecisionID: did}
err = b.Destroy()
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
}
result := gin.H{"result": "deleted"}
ServeResult(c, "ballot_deleted.js", result)
}
// HBallotInfo gets the information for a specific
// ballot in a decision and retusn an json object
// of the found ballot
func HBallotInfo(c *gin.Context) {
did := c.Param("decision_id")
bid := c.Param("ballot_id")
var ballot Ballot
err := dbmap.SelectOne(&ballot, "SELECT * FROM ballot where ballot_id=$1 and decision_id=$2", bid, did)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("Unable to find ballot %v for decision %v", bid, did)})
return
}
result := gin.H{"ballot": ballot}
ServeResult(c, "ballot_info.js", result)
}
// Destroy removes a ballot from the database
// it also removes the dependencies of a ballots
// such as votes
func (b *Ballot) Destroy() error {
if _, err := dbmap.Delete(b); err != nil {
return fmt.Errorf("Unable to delete ballot %#v from database", b)
}
// Remove votes beloning to this ballot
var votes []Vote
_, _ = dbmap.Select(&votes, "SELECT * FROM vote WHERE ballot_id=$1", b.BallotID)
for _, v := range votes {
if err := v.Destroy(); err != nil {
return err
}
}
// Remove ratings belonging to this ballot
var ratings []Rating
_, _ = dbmap.Select(&ratings, "SELECT * FROM rating WHERE ballot_id=$1", b.BallotID)
for _, r := range ratings {
if err := r.Destroy(); err != nil {
return err
}
}
return nil
}
// HBallotLogin is used to login users to their ballot
func HBallotLogin(c *gin.Context) {
did, err := strconv.Atoi(c.Param("decision_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "unable to parse decision id"})
return
}
bid, err := strconv.Atoi(c.Param("ballot_id"))
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "unable to parse ballot id"})
return
}
secret := c.Param("secret")
// Find the ballot
var ballot Ballot
err = dbmap.SelectOne(&ballot, "SELECT * FROM ballot where ballot_id=$1 and decision_id=$2", bid, did)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("Unable to find ballot %v for decision %v", bid, did)})
return
}
if ballot.Secret != secret {
c.JSON(http.StatusForbidden, gin.H{"error": "Secret does not belong to this ballot"})
return
}
// Set the cookies
ballotIDStr := strconv.Itoa(ballot.BallotID)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "unable to parse ballot_id"})
return
}
decisionIDStr := strconv.Itoa(ballot.DecisionID)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "unable to parse ballot_id"})
return
}
expiration := time.Now().Add(365 * 24 * time.Hour)
bcookie := http.Cookie{
Name: "ballot_id",
Value: ballotIDStr,
Path: "/",
Expires: expiration}
dcookie := http.Cookie{
Name: "decision_id",
Value: decisionIDStr,
Path: "/",
Expires: expiration}
http.SetCookie(c.Writer, &bcookie)
http.SetCookie(c.Writer, &dcookie)
c.Redirect(http.StatusSeeOther, "http://localhost:9999/ballot.html")
}
// HBallotWhoami returns the current
// logged in ballot
func HBallotWhoami(c *gin.Context) {
bcookie, err := c.Request.Cookie("ballot_id")
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "unable to find ballot cookie"})
return
}
dcookie, err := c.Request.Cookie("decision_id")
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "unable to find decision cookie"})
return
}
dval, err := strconv.Atoi(dcookie.Value)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "Unable to parse cookie decision id"})
return
}
bval, err := strconv.Atoi(bcookie.Value)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": "Unable to parse cookie ballot id"})
return
}
result := gin.H{"ballot": Ballot{BallotID: bval, DecisionID: dval}}
ServeResult(c, "ballot_whoami.js", result)
}
// HBallotAllInfo show all of the information related to a ballot
func HBallotAllInfo(c *gin.Context) {
did := c.Param("decision_id")
bid := c.Param("ballot_id")
var ballot Ballot
err := dbmap.SelectOne(&ballot, "SELECT * FROM ballot where ballot_id=$1 and decision_id=$2", bid, did)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find ballot %v for decision %v", bid, did)})
return
}
// First get the ballot
var ai BallotAllInfo
ai.Name = ballot.Name
ai.Email = ballot.Email
ai.Sent = ballot.Sent
ai.URLDecision = fmt.Sprintf("/decision/%s", did)
// Get the votes for this ballot
_, err = dbmap.Select(&ai.Votes, "SELECT * FROM vote where ballot_id=$1", bid)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find votes for ballot %v", bid)})
return
}
// Get the ratings for this ballot
_, err = dbmap.Select(&ai.Ratings, "SELECT * FROM rating where ballot_id=$1", bid)
if err != nil {
c.JSON(http.StatusForbidden,
gin.H{"error": fmt.Sprintf("Unable to find ratings for ballot %v", bid)})
return
}
result := gin.H{"ballot": ai}
ServeResult(c, "ballots_all_info.js", result)
}
// Save inserts a ballot into the database
func (b *Ballot) Save() error {
// Check if decision exists or not
var d Decision
err := dbmap.SelectOne(&d, "select * from decision where decision_id=$1", b.DecisionID)
if err != nil {
return fmt.Errorf("Decision %d does not exists, can't create ballot without a decision", b.DecisionID)
}
if err = dbmap.Insert(b); err != nil {
return fmt.Errorf("Unable to insert ballot %#v to database", b)
}
// Now recreate the hash correctly for
// the current ballot after knowing
// the id
if err := dbmap.SelectOne(b, "select * from ballot where ballot_id=(select max(ballot_id) from ballot) and email=$1", b.Email); err != nil {
return fmt.Errorf("Unable to set secret on ballot %#v to database", err)
}
b.Secret = HashPassword(fmt.Sprintf("b_%d_d_%d", b.BallotID, b.DecisionID))
if _, err = dbmap.Update(b); err != nil {
return fmt.Errorf("Unable to update ballot %#v to database", b)
}
return nil
}