annotate pkg/controllers/user.go @ 822:8926c413db21

Started implementation of test mail end point. This is intended to test the sending of system notification emails. TODO: more sensible content.
author Sascha Wilde <wilde@intevation.de>
date Thu, 27 Sep 2018 18:52:41 +0200
parents 8a0737aa6ab6
children f3adc0f3a20a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
226
63dd5216eee4 Refactored gemma server to be more REST-like.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 218
diff changeset
1 package controllers
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import (
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 "database/sql"
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
5 "fmt"
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 "net/http"
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7
241
3b688fe04c39 No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 240
diff changeset
8 "github.com/gorilla/mux"
3b688fe04c39 No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 240
diff changeset
9
414
c1047fd04a3a Moved project specific Go packages to new pkg folder.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 343
diff changeset
10 "gemma.intevation.de/gemma/pkg/auth"
822
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
11 "gemma.intevation.de/gemma/pkg/misc"
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
12 "gemma.intevation.de/gemma/pkg/models"
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 )
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 const (
343
5b03f420957d Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents: 342
diff changeset
16 createUserSQL = `INSERT INTO users.list_users
5b03f420957d Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents: 342
diff changeset
17 VALUES ($1, $2, $3, $4, NULL, $5)`
5b03f420957d Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents: 342
diff changeset
18 createUserExtentSQL = `INSERT INTO users.list_users
5b03f420957d Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents: 342
diff changeset
19 VALUES ($1, $2, $3, $4,
188
ee3093966a6d ST_SetSRID should be not need as the map extent column does not have any SRID code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 187
diff changeset
20 ST_MakeBox2D(ST_Point($5, $6), ST_Point($7, $8)), $9)`
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
21
327
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
22 updateUserUnprivSQL = `UPDATE users.list_users
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
23 SET (pw, map_extent, email_address)
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
24 = ($2, ST_MakeBox2D(ST_Point($3, $4), ST_Point($5, $6)), $7)
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
25 WHERE username = $1`
307
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
26 updateUserSQL = `UPDATE users.list_users
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
27 SET (rolname, username, pw, country, map_extent, email_address)
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
28 = ($2, $3, $4, $5, NULL, $6)
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
29 WHERE username = $1`
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
30 updateUserExtentSQL = `UPDATE users.list_users
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
31 SET (rolname, username, pw, country, map_extent, email_address)
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
32 = ($2, $3, $4, $5, ST_MakeBox2D(ST_Point($6, $7), ST_Point($8, $9)), $10)
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
33 WHERE username = $1`
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
34
342
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
35 deleteUserSQL = `DELETE FROM users.list_users WHERE username = $1`
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
36
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
37 listUsersSQL = `SELECT
253
322c3d0e05ef The column in sys_admin.list_users is called rolname not rolename.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 250
diff changeset
38 rolname,
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
39 username,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
40 country,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
41 email_address,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
42 ST_XMin(map_extent), ST_YMin(map_extent),
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
43 ST_XMax(map_extent), ST_YMax(map_extent)
279
d89a19c297e0 list_users is now in users schema.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 261
diff changeset
44 FROM users.list_users`
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
45
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
46 listUserSQL = `SELECT
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
47 rolname,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
48 country,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
49 email_address,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
50 ST_XMin(map_extent), ST_YMin(map_extent),
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
51 ST_XMax(map_extent), ST_YMax(map_extent)
279
d89a19c297e0 list_users is now in users schema.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 261
diff changeset
52 FROM users.list_users
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
53 WHERE username = $1`
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 )
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
56 func deleteUser(
288
4befc5868ea6 Mark input in user controllers as unused if they don't need a JSON input.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
57 _ interface{}, req *http.Request,
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
58 db *sql.Conn,
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
59 ) (jr JSONResult, err error) {
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
60
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
61 user := mux.Vars(req)["user"]
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
62 if !models.UserName(user).IsValid() {
425
c49f4c1808b1 Simplified user validation with new UserName type.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 418
diff changeset
63 err = JSONError{http.StatusBadRequest, "error: user invalid"}
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
64 return
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
65 }
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
66
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
67 session, _ := auth.GetSession(req)
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
68 if session.User == user {
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
69 err = JSONError{http.StatusBadRequest, "error: cannot delete yourself"}
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
70 return
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
71 }
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
72
342
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
73 var res sql.Result
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
74
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
75 if res, err = db.ExecContext(req.Context(), deleteUserSQL, user); err != nil {
342
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
76 return
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
77 }
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
78
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
79 if n, err2 := res.RowsAffected(); err2 == nil && n == 0 {
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
80 err = JSONError{
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
81 Code: http.StatusNotFound,
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
82 Message: fmt.Sprintf("Cannot find user %s.", user),
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
83 }
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
84 return
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
85 }
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
86
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
87 // Running in a go routine should not be necessary.
493
8a0737aa6ab6 The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 486
diff changeset
88 go func() { auth.Sessions.Logout(user) }()
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
89
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
90 jr = JSONResult{Code: http.StatusNoContent}
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
91 return
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
92 }
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
93
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
94 func updateUser(
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
95 input interface{},
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
96 req *http.Request,
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
97 db *sql.Conn,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
98 ) (jr JSONResult, err error) {
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
99
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
100 user := models.UserName(mux.Vars(req)["user"])
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
101 if !user.IsValid() {
418
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
102 err = JSONError{http.StatusBadRequest, "error: user invalid"}
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
103 return
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
104 }
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
105
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
106 newUser := input.(*models.User)
307
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
107 var res sql.Result
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
108
326
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
109 if s, _ := auth.GetSession(req); s.Roles.Has("sys_admin") {
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
110 if newUser.Extent == nil {
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
111 res, err = db.ExecContext(
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
112 req.Context(),
326
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
113 updateUserSQL,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
114 user,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
115 newUser.Role,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
116 newUser.User,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
117 newUser.Password,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
118 newUser.Country,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
119 newUser.Email,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
120 )
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
121 } else {
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
122 res, err = db.ExecContext(
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
123 req.Context(),
326
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
124 updateUserExtentSQL,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
125 user,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
126 newUser.Role,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
127 newUser.User,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
128 newUser.Password,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
129 newUser.Country,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
130 newUser.Extent.X1, newUser.Extent.Y1,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
131 newUser.Extent.X2, newUser.Extent.Y2,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
132 newUser.Email,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
133 )
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
134 }
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
135 } else {
328
003243ec5ce5 Don't crash if we doing update as unprivileged user without a bounding box.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 327
diff changeset
136 if newUser.Extent == nil {
003243ec5ce5 Don't crash if we doing update as unprivileged user without a bounding box.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 327
diff changeset
137 err = JSONError{http.StatusBadRequest, "extent is mandatory"}
003243ec5ce5 Don't crash if we doing update as unprivileged user without a bounding box.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 327
diff changeset
138 return
003243ec5ce5 Don't crash if we doing update as unprivileged user without a bounding box.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 327
diff changeset
139 }
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
140 res, err = db.ExecContext(
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
141 req.Context(),
327
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
142 updateUserUnprivSQL,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
143 user,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
144 newUser.Password,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
145 newUser.Extent.X1, newUser.Extent.Y1,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
146 newUser.Extent.X2, newUser.Extent.Y2,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
147 newUser.Email,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
148 )
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
149 }
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
150
311
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
151 if err != nil {
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
152 return
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
153 }
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
154
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
155 if n, err2 := res.RowsAffected(); err2 == nil && n == 0 {
307
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
156 err = JSONError{
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
157 Code: http.StatusNotFound,
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
158 Message: fmt.Sprintf("Cannot find user %s.", user),
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
159 }
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
160 return
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
161 }
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162
261
ab9859981ee3 If a user got renamed kick her/him from the connection pool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 257
diff changeset
163 if user != newUser.User {
ab9859981ee3 If a user got renamed kick her/him from the connection pool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 257
diff changeset
164 // Running in a go routine should not be necessary.
493
8a0737aa6ab6 The connection pool is now only a session store.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 486
diff changeset
165 go func() { auth.Sessions.Logout(string(user)) }()
261
ab9859981ee3 If a user got renamed kick her/him from the connection pool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 257
diff changeset
166 }
ab9859981ee3 If a user got renamed kick her/him from the connection pool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 257
diff changeset
167
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
168 jr = JSONResult{
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
169 Code: http.StatusCreated,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
170 Result: struct {
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
171 Result string `json:"result"`
314
adceb47920fb Cosmetics. Little less structure bloat.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 311
diff changeset
172 }{"success"},
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
173 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
174 return
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
175 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
176
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
177 func createUser(
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
178 input interface{},
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
179 req *http.Request,
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
180 db *sql.Conn,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
181 ) (jr JSONResult, err error) {
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
182
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
183 user := input.(*models.User)
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
185 if user.Extent == nil {
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
186 _, err = db.ExecContext(
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
187 req.Context(),
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
188 createUserSQL,
243
d39f897fae16 Made models for email, user and country driver.Valuer to get rid of some conversions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 241
diff changeset
189 user.Role,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
190 user.User,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
191 user.Password,
243
d39f897fae16 Made models for email, user and country driver.Valuer to get rid of some conversions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 241
diff changeset
192 user.Country,
d39f897fae16 Made models for email, user and country driver.Valuer to get rid of some conversions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 241
diff changeset
193 user.Email,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
194 )
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
195 } else {
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
196 _, err = db.ExecContext(
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
197 req.Context(),
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
198 createUserExtentSQL,
243
d39f897fae16 Made models for email, user and country driver.Valuer to get rid of some conversions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 241
diff changeset
199 user.Role,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
200 user.User,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
201 user.Password,
243
d39f897fae16 Made models for email, user and country driver.Valuer to get rid of some conversions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 241
diff changeset
202 user.Country,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
203 user.Extent.X1, user.Extent.Y1,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
204 user.Extent.X2, user.Extent.Y2,
243
d39f897fae16 Made models for email, user and country driver.Valuer to get rid of some conversions.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 241
diff changeset
205 user.Email,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
206 )
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207 }
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208
187
51addc0533fe More complete show case for creating users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
209 if err != nil {
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
210 return
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 }
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
212
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
213 jr = JSONResult{
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
214 Code: http.StatusCreated,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
215 Result: struct {
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
216 Result string `json:"result"`
314
adceb47920fb Cosmetics. Little less structure bloat.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 311
diff changeset
217 }{"success"},
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
218 }
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
219 return
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
220 }
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
221
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
222 func listUsers(
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
223 _ interface{},
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
224 req *http.Request,
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
225 db *sql.Conn,
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
226 ) (jr JSONResult, err error) {
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
227
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
228 var rows *sql.Rows
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
229
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
230 rows, err = db.QueryContext(req.Context(), listUsersSQL)
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
231 if err != nil {
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
232 return
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
233 }
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
234 defer rows.Close()
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
235
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
236 var users []*models.User
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
237
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
238 for rows.Next() {
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
239 user := &models.User{Extent: &models.BoundingBox{}}
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
240 if err = rows.Scan(
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
241 &user.Role,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
242 &user.User,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
243 &user.Country,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
244 &user.Email,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
245 &user.Extent.X1, &user.Extent.Y1,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
246 &user.Extent.X2, &user.Extent.Y2,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
247 ); err != nil {
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
248 return
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
249 }
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
250 users = append(users, user)
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
251 }
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
252
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
253 jr = JSONResult{
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
254 Result: struct {
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
255 Users []*models.User `json:"users"`
314
adceb47920fb Cosmetics. Little less structure bloat.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 311
diff changeset
256 }{users},
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
257 }
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
258 return
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
259 }
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
260
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
261 func listUser(
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
262 _ interface{},
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
263 req *http.Request,
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
264 db *sql.Conn,
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
265 ) (jr JSONResult, err error) {
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
266
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
267 user := models.UserName(mux.Vars(req)["user"])
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
268 if !user.IsValid() {
418
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
269 err = JSONError{http.StatusBadRequest, "error: user invalid"}
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
270 return
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
271 }
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
272
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
273 result := &models.User{
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
274 User: user,
442
fc37e7072022 Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 425
diff changeset
275 Extent: &models.BoundingBox{},
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
276 }
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
277
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
278 err = db.QueryRowContext(req.Context(), listUserSQL, user).Scan(
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
279 &result.Role,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
280 &result.Country,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
281 &result.Email,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
282 &result.Extent.X1, &result.Extent.Y1,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
283 &result.Extent.X2, &result.Extent.Y2,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
284 )
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
285
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
286 switch {
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
287 case err == sql.ErrNoRows:
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
288 err = JSONError{
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
289 Code: http.StatusNotFound,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
290 Message: fmt.Sprintf("Cannot find user %s.", user),
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
291 }
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
292 return
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
293 case err != nil:
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
294 return
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
295 }
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
296
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
297 jr.Result = result
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
298 return
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
299 }
822
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
300
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
301 func sendTestMail(
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
302 _ interface{},
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
303 req *http.Request,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
304 db *sql.Conn,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
305 ) (jr JSONResult, err error) {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
306
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
307 user := models.UserName(mux.Vars(req)["user"])
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
308 if !user.IsValid() {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
309 err = JSONError{http.StatusBadRequest, "error: user invalid"}
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
310 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
311 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
312
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
313 userData := &models.User{
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
314 User: user,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
315 Extent: &models.BoundingBox{},
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
316 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
317
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
318 err = db.QueryRowContext(req.Context(), listUserSQL, user).Scan(
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
319 &userData.Role,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
320 &userData.Country,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
321 &userData.Email,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
322 &userData.Extent.X1, &userData.Extent.Y1,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
323 &userData.Extent.X2, &userData.Extent.Y2,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
324 )
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
325
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
326 switch {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
327 case err == sql.ErrNoRows:
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
328 err = JSONError{
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
329 Code: http.StatusNotFound,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
330 Message: fmt.Sprintf("Cannot find user %s.", user),
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
331 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
332 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
333 case err != nil:
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
334 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
335 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
336
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
337 var body string
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
338
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
339 if userData.Role == "sys_admin" {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
340 body = "Test for Sys Admin Notification"
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
341 } else if userData.Role == "waterway_admin" {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
342 body = "Test for Water Way Admin Notification"
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
343 } else {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
344 err = JSONError{
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
345 Code: http.StatusBadRequest,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
346 Message: "Test mails can only be generated for admin roles.",
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
347 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
348 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
349 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
350 err = misc.SendMail(string(userData.Email), "Test Email", body)
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
351 if err != nil {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
352 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
353 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
354
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
355 jr.Result = &struct {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
356 Message string `json:"message"`
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
357 }{"Sending test mail."}
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
358
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
359 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
360 }