-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.go
86 lines (62 loc) · 1.51 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
85
86
package main
import (
"bugzilla"
"net/http"
)
func UserCurrentHandler(w http.ResponseWriter, r *http.Request) {
noCache(w)
client, err := Bz()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
u, err := client.Users().CheckCurrentLogin()
if err != nil {
http.Error(w, "Not logged in", http.StatusNoContent)
return
}
JsonResponse(w, u)
}
func UserLoginHandler(w http.ResponseWriter, r *http.Request) {
noCache(w)
user := r.FormValue("user")
password := r.FormValue("password")
if len(user) == 0 || len(password) == 0 {
http.Error(w, "User or password not provided", http.StatusBadRequest)
return
}
client, err := Bz()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
u, err := client.Users().Login(user, password)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go SaveCookies()
JsonResponse(w, u)
}
func UserLogoutHandler(w http.ResponseWriter, r *http.Request) {
noCache(w)
if bugzilla.CurrentUser() == nil {
return
}
client, err := Bz()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := client.Users().Logout(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go SaveCookies()
bz = nil
}
func init() {
router.HandleFunc("/api/user/current", UserCurrentHandler)
router.HandleFunc("/api/user/login", UserLoginHandler)
router.HandleFunc("/api/user/logout", UserLogoutHandler)
}