This repository has been archived by the owner on May 22, 2022. It is now read-only.
forked from fallenhitokiri/leeroyci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
84 lines (65 loc) · 1.97 KB
/
user.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
package web
import (
"errors"
"net/http"
"github.com/gorilla/context"
"github.com/gorilla/schema"
"github.com/francoishill/leeroyci/database"
)
// userSettingsForm is the form used by users to edit their account. Every
// change requires the password to be entered. Admin status cannot be changed.
type userSettingsForm struct {
Email string `schema:"email"`
FirstName string `schema:"first_name"`
LastName string `schema:"last_name"`
Password string `schema:"password"`
NewPassword string `schema:"new_password"`
}
// update updates an existing user account. The admin flag passed is taken from
// the user that was fetched from the DB, it cannot be edited through the form.
func (u userSettingsForm) update(request *http.Request) error {
err := request.ParseForm()
if err != nil {
return err
}
decoder := schema.NewDecoder()
form := new(userSettingsForm)
err = decoder.Decode(form, request.PostForm)
if err != nil {
return err
}
user := context.Get(request, contextUser).(*database.User)
auth := database.ComparePassword(form.Password, user.Password)
if auth == false {
return errors.New("Username and password do not match.")
}
_, err = user.Update(
form.Email,
form.FirstName,
form.LastName,
form.NewPassword,
user.Admin,
)
return err
}
// viewUpdateUser exposes configuration settings for a user account to the
// user. Admin status cannot be changed here.
func viewUpdateUser(w http.ResponseWriter, r *http.Request) {
template := "user/settings.html"
ctx := make(responseContext)
if r.Method == "POST" {
err := userSettingsForm{}.update(r)
if err == nil {
ctx["message"] = "Update successful."
} else {
ctx["error"] = err.Error()
}
}
render(w, r, template, ctx)
}
// viewRegenrateAccessKey regenerates the access key for a user.
func viewRegenrateAccessKey(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, contextUser).(*database.User)
user.NewAccessKey()
http.Redirect(w, r, "/user/settings", 302)
}