-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprofile.go
147 lines (121 loc) · 3.34 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
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
package main
import (
"net"
"net/http"
"strconv"
"strings"
)
/*
* Serve '/profile' page
* with profile settings and long term actions
*/
func profileHandler(w http.ResponseWriter, r *http.Request) {
// Get client's IP
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Error().Msg("Can't get IP for profile page: " + err.Error())
return
}
// Check existing session
username, err := sessions.exists(w, r)
if err != nil {
log.Error().
Str("ip", ip).
Msg(err.Error())
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
// Get account from a database
account, err := db.getAccount(username)
if err != nil {
log.Error().
Str("ip", ip).
Str("username", username).
Msg("Can't GetAccount for profile page: " + err.Error())
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
}
// All are admins in service's development mode
if config.Environment != "prod" {
account.Admin = true
}
// Merge some settings
account.Uploads.MaxSize = config.Upload.MaxSize
templateData := &TemplateData{
Account: account,
Collectors: collectors,
NonGlobalExist: nonGlobalExist,
}
renderTemplate(w, "profile", templateData, nil)
log.Info().
Str("ip", ip).
Str("username", username).
Msg("Profile page requested")
}
/*
* Handle 'options' websocket command to save personal settings.
*
* Receives a comma-separated values:
* 1. New nodes stabilization time in milliseconds
* 2. Limit value as a part of each query to the data sources
*/
func (a *Account) optionsHandler(data string) {
parts := strings.Split(data, ",")
stabilization, err := strconv.Atoi(parts[0])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse stabilization time value: " + err.Error())
a.send("error", "Invalid <strong>stabilization time</strong> value given, integer expected.", "Can't save!")
return
}
limit, err := strconv.Atoi(parts[1])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse limit value: " + err.Error())
a.send("error", "Invalid <strong>limit</strong> value given, integer expected.", "Can't save!")
return
}
showLimited, err := strconv.ParseBool(parts[2])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse showLimited value: " + err.Error())
a.send("error", "Invalid <strong>showLimited</strong> value given, boolean expected.", "Can't save!")
return
}
debug, err := strconv.ParseBool(parts[3])
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't parse debug value: " + err.Error())
a.send("error", "Invalid <strong>debug</strong> value given, boolean expected.", "Can't save!")
return
}
// Update options
options := &Options{
StabilizationTime: stabilization,
Limit: limit,
ShowLimited: showLimited,
Debug: debug,
}
err = a.update("options", options)
if err != nil {
log.Error().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Can't update profile options: " + err.Error())
a.send("error", err.Error(), "Can't update profile options!")
return
}
a.send("ok", "", "")
log.Info().
Str("ip", a.Session.IP).
Str("username", a.Username).
Msg("Profile options updated")
}