Mercurial > gemma
comparison cmd/tokenserver/user.go @ 186:fe3a88f00b0a
Experimental user creation support.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 19 Jul 2018 18:33:52 +0200 |
parents | |
children | 51addc0533fe |
comparison
equal
deleted
inserted
replaced
185:a9d9c2b1d08c | 186:fe3a88f00b0a |
---|---|
1 package main | |
2 | |
3 import ( | |
4 "database/sql" | |
5 "encoding/json" | |
6 "log" | |
7 "net/http" | |
8 | |
9 "gemma.intevation.de/gemma/auth" | |
10 ) | |
11 | |
12 type BoundingBox struct { | |
13 X1 float64 `json:"x1"` | |
14 Y1 float64 `json:"y1"` | |
15 X2 float64 `json:"x2"` | |
16 Y2 float64 `json:"y2"` | |
17 } | |
18 | |
19 type User struct { | |
20 User string `json:"user"` | |
21 Role string `json:"role"` | |
22 Password string `json:"password"` | |
23 Email string `json:"email"` | |
24 Country string `json:"country"` | |
25 Extent *BoundingBox `json:"extent"` | |
26 } | |
27 | |
28 const ( | |
29 createUserSQL = `SELECT create_user($1, $2, $3, $4, $5, $6)` | |
30 ) | |
31 | |
32 func createUser(rw http.ResponseWriter, req *http.Request) { | |
33 | |
34 var user User | |
35 | |
36 defer req.Body.Close() | |
37 if err := json.NewDecoder(req.Body).Decode(&user); err != nil { | |
38 log.Printf("err: %v\n", err) | |
39 http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) | |
40 return | |
41 } | |
42 | |
43 token, _ := auth.GetToken(req) | |
44 err := auth.ConnPool.Do(token, func(db *sql.DB) error { | |
45 _, err := db.Exec( | |
46 createUserSQL, | |
47 user.Role, | |
48 user.User, | |
49 user.Password, | |
50 user.Country, | |
51 nil, | |
52 ) | |
53 return err | |
54 }) | |
55 | |
56 if err != nil { | |
57 log.Printf("err: %v\n", err) | |
58 http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | |
59 return | |
60 } | |
61 | |
62 var result = struct { | |
63 Result string `json:"result"` | |
64 }{ | |
65 Result: "success", | |
66 } | |
67 | |
68 rw.Header().Set("Content-Type", "application/json") | |
69 if err := json.NewEncoder(rw).Encode(&result); err != nil { | |
70 log.Printf("error: %v\n", err) | |
71 } | |
72 } |