Mercurial > gemma
annotate pkg/controllers/user.go @ 5457:fb3e127bccb4 uiimprovements
fixed linting errors
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Thu, 15 Jul 2021 10:11:49 +0200 |
parents | 5d6aa584b4fb |
children | 5f47eeea988d |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
2 // without warranty, see README.md and license for details. |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
13 // * Tom Gottfried <tom.gottfried@intevation.de> |
1317
5443f5c9154c
Added missing authors names in Go files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
14 // * Sascha Wilde <sascha.wilde@intevation.de> |
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
827
diff
changeset
|
15 |
226
63dd5216eee4
Refactored gemma server to be more REST-like.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
218
diff
changeset
|
16 package controllers |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 |
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 import ( |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
19 "bytes" |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "database/sql" |
254
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
21 "fmt" |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
22 "log" |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 "net/http" |
5345
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
24 "strconv" |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
25 "strings" |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
26 "text/template" |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
27 "time" |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 |
241
3b688fe04c39
No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
240
diff
changeset
|
29 "github.com/gorilla/mux" |
3b688fe04c39
No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
240
diff
changeset
|
30 |
414
c1047fd04a3a
Moved project specific Go packages to new pkg folder.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
343
diff
changeset
|
31 "gemma.intevation.de/gemma/pkg/auth" |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
32 "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
|
33 "gemma.intevation.de/gemma/pkg/models" |
4062
6c760abcff0e
Move handling of PostgreSQL errors to own package
Tom Gottfried <tom@intevation.de>
parents:
4059
diff
changeset
|
34 "gemma.intevation.de/gemma/pkg/pgxutils" |
1544
5f80ec319a4f
If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1317
diff
changeset
|
35 "gemma.intevation.de/gemma/pkg/scheduler" |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
36 |
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
37 mw "gemma.intevation.de/gemma/pkg/middleware" |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 ) |
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 |
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 const ( |
343
5b03f420957d
Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents:
342
diff
changeset
|
41 createUserSQL = `INSERT INTO users.list_users |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
42 VALUES ($1, $2, $3, $4, NULL, $5, $6)` |
343
5b03f420957d
Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents:
342
diff
changeset
|
43 createUserExtentSQL = `INSERT INTO users.list_users |
5b03f420957d
Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents:
342
diff
changeset
|
44 VALUES ($1, $2, $3, $4, |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
45 ST_MakeBox2D(ST_Point($5, $6), ST_Point($7, $8)), $9, $10)` |
235
7d1f0ffdfa41
Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
226
diff
changeset
|
46 |
327
363983d5c567
Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents:
326
diff
changeset
|
47 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
|
48 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
|
49 = ($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
|
50 WHERE username = $1` |
307
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
51 updateUserSQL = `UPDATE users.list_users |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
52 SET (rolname, username, pw, country, map_extent, email_address, report_reciever) |
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
53 = ($2, $3, $4, $5, NULL, $6, $7) |
307
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
54 WHERE username = $1` |
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
55 updateUserExtentSQL = `UPDATE users.list_users |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
56 SET (rolname, username, pw, country, map_extent, email_address, report_reciever) |
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
57 = ($2, $3, $4, $5, ST_MakeBox2D(ST_Point($6, $7), ST_Point($8, $9)), $10, $11) |
307
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
58 WHERE username = $1` |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
59 |
342
c6bd6ed18942
Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents:
328
diff
changeset
|
60 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
|
61 |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
62 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
|
63 rolname, |
250
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
64 username, |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
65 country, |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
66 email_address, |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
67 ST_XMin(map_extent), ST_YMin(map_extent), |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
68 ST_XMax(map_extent), ST_YMax(map_extent), |
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
69 report_reciever |
279
d89a19c297e0
list_users is now in users schema.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
261
diff
changeset
|
70 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
|
71 |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
72 listUserSQL = `SELECT |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
73 rolname, |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
74 country, |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
75 email_address, |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
76 ST_XMin(map_extent), ST_YMin(map_extent), |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
77 ST_XMax(map_extent), ST_YMax(map_extent), |
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
78 report_reciever |
279
d89a19c297e0
list_users is now in users schema.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
261
diff
changeset
|
79 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
|
80 WHERE username = $1` |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 ) |
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
83 var ( |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
84 testSysadminNotifyMailTmpl = template.Must( |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
85 template.New("sysadmin").Parse(`Dear {{ .User }}, |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
86 |
5372
5d6aa584b4fb
Streamlined the text templates for test emails.
Thomas Junk <thomas.junk@intevation.de>
parents:
5370
diff
changeset
|
87 this is a test email from the Gemma System Errors notification service. You |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
88 recieved this mail, because a System Administrator triggered the test mail |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
89 sending function at {{ .Timestamp }}. |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
90 |
5372
5d6aa584b4fb
Streamlined the text templates for test emails.
Thomas Junk <thomas.junk@intevation.de>
parents:
5370
diff
changeset
|
91 When a critical system error is detected an automated mail will be sent to |
5d6aa584b4fb
Streamlined the text templates for test emails.
Thomas Junk <thomas.junk@intevation.de>
parents:
5370
diff
changeset
|
92 {{ .Email }} with details on the error condition.`)) |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
93 |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
94 testWWAdminNotifyMailTmpl = template.Must( |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
95 template.New("waterwayadmin").Parse(`Dear {{ .User }}, |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
96 |
5372
5d6aa584b4fb
Streamlined the text templates for test emails.
Thomas Junk <thomas.junk@intevation.de>
parents:
5370
diff
changeset
|
97 this is a test email from the Gemma System Mail notification service. You |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
98 recieved this mail, because a System Administrator triggered the test mail |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
99 sending function at {{ .Timestamp }}. |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
100 |
5372
5d6aa584b4fb
Streamlined the text templates for test emails.
Thomas Junk <thomas.junk@intevation.de>
parents:
5370
diff
changeset
|
101 When the status of a data import managed by you changes an automated mail will |
5d6aa584b4fb
Streamlined the text templates for test emails.
Thomas Junk <thomas.junk@intevation.de>
parents:
5370
diff
changeset
|
102 be sent to {{ .Email }} with details on the new import status |
5d6aa584b4fb
Streamlined the text templates for test emails.
Thomas Junk <thomas.junk@intevation.de>
parents:
5370
diff
changeset
|
103 (including import errors) and details on the corresponding import.`)) |
5370
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
104 |
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
105 testWWUserNotifyMailTmpl = template.Must( |
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
106 template.New("waterwayuser").Parse(`Dear {{ .User }}, |
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
107 |
5372
5d6aa584b4fb
Streamlined the text templates for test emails.
Thomas Junk <thomas.junk@intevation.de>
parents:
5370
diff
changeset
|
108 this is a test email from the Gemma System Mail notification service. You |
5370
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
109 recieved this mail, because a System Administrator triggered the test mail |
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
110 sending function at {{ .Timestamp }}.`)) |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
111 ) |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
112 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
113 func deleteUser(req *http.Request) (jr mw.JSONResult, err error) { |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
114 |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
115 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
|
116 if !models.UserName(user).IsValid() { |
4506
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
117 err = mw.JSONError{ |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
118 Code: http.StatusBadRequest, |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
119 Message: "error: user invalid", |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
120 } |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
121 return |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
122 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
123 |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
124 session, _ := auth.GetSession(req) |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
125 if session.User == user { |
4506
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
126 err = mw.JSONError{ |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
127 Code: http.StatusBadRequest, |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
128 Message: "error: cannot delete yourself", |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
129 } |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
130 return |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
131 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
132 |
1660
f32e8a973b48
Scheduler: Moved code to figure out the scheduled jobs per user from the database to a more suited place.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1654
diff
changeset
|
133 ctx := req.Context() |
f32e8a973b48
Scheduler: Moved code to figure out the scheduled jobs per user from the database to a more suited place.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1654
diff
changeset
|
134 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
135 db := mw.JSONConn(req) |
4242
1458c9b0fdaa
Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4148
diff
changeset
|
136 |
1654
85386ad17d34
Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1544
diff
changeset
|
137 // Remove scheduled tasks. |
1660
f32e8a973b48
Scheduler: Moved code to figure out the scheduled jobs per user from the database to a more suited place.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1654
diff
changeset
|
138 ids, err2 := scheduler.ScheduledUserIDs(ctx, db, user) |
1654
85386ad17d34
Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1544
diff
changeset
|
139 if err2 == nil { |
85386ad17d34
Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1544
diff
changeset
|
140 if len(ids) > 0 { |
85386ad17d34
Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1544
diff
changeset
|
141 go func() { scheduler.UnbindByIDs(ids) }() |
85386ad17d34
Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1544
diff
changeset
|
142 } |
85386ad17d34
Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1544
diff
changeset
|
143 } else { |
85386ad17d34
Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1544
diff
changeset
|
144 log.Printf("error: %v\n", err2) |
85386ad17d34
Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1544
diff
changeset
|
145 } |
85386ad17d34
Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1544
diff
changeset
|
146 |
342
c6bd6ed18942
Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents:
328
diff
changeset
|
147 var res sql.Result |
c6bd6ed18942
Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents:
328
diff
changeset
|
148 |
1660
f32e8a973b48
Scheduler: Moved code to figure out the scheduled jobs per user from the database to a more suited place.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1654
diff
changeset
|
149 if res, err = db.ExecContext(ctx, deleteUserSQL, user); err != nil { |
342
c6bd6ed18942
Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents:
328
diff
changeset
|
150 return |
c6bd6ed18942
Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents:
328
diff
changeset
|
151 } |
c6bd6ed18942
Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents:
328
diff
changeset
|
152 |
c6bd6ed18942
Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents:
328
diff
changeset
|
153 if n, err2 := res.RowsAffected(); err2 == nil && n == 0 { |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
154 err = mw.JSONError{ |
342
c6bd6ed18942
Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents:
328
diff
changeset
|
155 Code: http.StatusNotFound, |
c6bd6ed18942
Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents:
328
diff
changeset
|
156 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
|
157 } |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
158 return |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
159 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
160 |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
161 // 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
|
162 go func() { auth.Sessions.Logout(user) }() |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
163 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
164 jr = mw.JSONResult{Code: http.StatusNoContent} |
240
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
165 return |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
166 } |
9012e4045da4
Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
237
diff
changeset
|
167 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
168 func updateUser(req *http.Request) (jr mw.JSONResult, err error) { |
235
7d1f0ffdfa41
Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
226
diff
changeset
|
169 |
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
|
170 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
|
171 if !user.IsValid() { |
4506
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
172 err = mw.JSONError{ |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
173 Code: http.StatusBadRequest, |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
174 Message: "error: user invalid", |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
175 } |
235
7d1f0ffdfa41
Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
226
diff
changeset
|
176 return |
7d1f0ffdfa41
Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
226
diff
changeset
|
177 } |
7d1f0ffdfa41
Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
226
diff
changeset
|
178 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
179 newUser := mw.JSONInput(req).(*models.User) |
307
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
180 var res sql.Result |
235
7d1f0ffdfa41
Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
226
diff
changeset
|
181 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
182 db := mw.JSONConn(req) |
4242
1458c9b0fdaa
Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4148
diff
changeset
|
183 |
326
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
184 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
|
185 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
|
186 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
|
187 req.Context(), |
326
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
188 updateUserSQL, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
189 user, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
190 newUser.Role, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
191 newUser.User, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
192 newUser.Password, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
193 newUser.Country, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
194 newUser.Email, |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
195 newUser.Reports, |
326
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
196 ) |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
197 } 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
|
198 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
|
199 req.Context(), |
326
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
200 updateUserExtentSQL, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
201 user, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
202 newUser.Role, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
203 newUser.User, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
204 newUser.Password, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
205 newUser.Country, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
206 newUser.Extent.X1, newUser.Extent.Y1, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
207 newUser.Extent.X2, newUser.Extent.Y2, |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
208 newUser.Email, |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
209 newUser.Reports, |
326
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
210 ) |
a7b2db8b3d18
Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
314
diff
changeset
|
211 } |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
212 } 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
|
213 if newUser.Extent == nil { |
4506
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
214 err = mw.JSONError{ |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
215 Code: http.StatusBadRequest, |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
216 Message: "extent is mandatory", |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
217 } |
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
|
218 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
|
219 } |
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
|
220 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
|
221 req.Context(), |
327
363983d5c567
Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents:
326
diff
changeset
|
222 updateUserUnprivSQL, |
363983d5c567
Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents:
326
diff
changeset
|
223 user, |
363983d5c567
Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents:
326
diff
changeset
|
224 newUser.Password, |
363983d5c567
Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents:
326
diff
changeset
|
225 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
|
226 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
|
227 newUser.Email, |
363983d5c567
Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents:
326
diff
changeset
|
228 ) |
235
7d1f0ffdfa41
Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
226
diff
changeset
|
229 } |
7d1f0ffdfa41
Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
226
diff
changeset
|
230 |
311
74559e12a59f
sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
231 if err != nil { |
74559e12a59f
sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
232 return |
74559e12a59f
sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
233 } |
74559e12a59f
sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
234 |
74559e12a59f
sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
307
diff
changeset
|
235 if n, err2 := res.RowsAffected(); err2 == nil && n == 0 { |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
236 err = mw.JSONError{ |
307
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
237 Code: http.StatusNotFound, |
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
238 Message: fmt.Sprintf("Cannot find user %s.", user), |
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
239 } |
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
240 return |
750a9c9cd965
Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents:
288
diff
changeset
|
241 } |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
242 |
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
|
243 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
|
244 // 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
|
245 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
|
246 } |
ab9859981ee3
If a user got renamed kick her/him from the connection pool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
257
diff
changeset
|
247 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
248 jr = mw.JSONResult{ |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
249 Code: http.StatusCreated, |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
250 Result: struct { |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
251 Result string `json:"result"` |
314
adceb47920fb
Cosmetics. Little less structure bloat.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
311
diff
changeset
|
252 }{"success"}, |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
253 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
254 return |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
255 } |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
256 |
5345
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
257 func patchUser(req *http.Request) (jr mw.JSONResult, err error) { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
258 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
259 user := models.UserName(mux.Vars(req)["user"]) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
260 if !user.IsValid() { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
261 err = mw.JSONError{ |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
262 Code: http.StatusBadRequest, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
263 Message: "error: user invalid", |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
264 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
265 return |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
266 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
267 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
268 s, ok := auth.GetSession(req) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
269 if !ok { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
270 err = mw.JSONError{ |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
271 Code: http.StatusUnauthorized, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
272 Message: "error: not logged in", |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
273 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
274 return |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
275 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
276 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
277 priv := s.Roles.Has("sys_admin") |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
278 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
279 if !priv && s.User != string(user) { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
280 err = mw.JSONError{ |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
281 Code: http.StatusUnauthorized, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
282 Message: "error: not allowed to modify someone else", |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
283 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
284 return |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
285 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
286 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
287 var ( |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
288 columns []string |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
289 positions []string |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
290 args []interface{} |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
291 ) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
292 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
293 update := func(column string, value interface{}) { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
294 columns = append(columns, column) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
295 positions = append(positions, "$"+strconv.Itoa(len(positions)+1)) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
296 args = append(args, value) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
297 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
298 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
299 updateBox := func(column string, extent *models.BoundingBox) { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
300 columns = append(columns, column) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
301 pos := len(positions) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
302 position := fmt.Sprintf("ST_MakeBox2D(ST_Point($%d, $%d), ST_Point($%d, $%d))", |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
303 pos+1, pos+2, pos+3, pos+4) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
304 positions = append(positions, position) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
305 args = append(args, extent.X1, extent.Y1, extent.X2, extent.Y2) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
306 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
307 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
308 patch := mw.JSONInput(req).(*models.UserPatch) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
309 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
310 if patch.User != nil && priv { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
311 update("user", *patch.User) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
312 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
313 if patch.Role != nil && priv { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
314 update("rolname", *patch.Role) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
315 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
316 if patch.Password != nil { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
317 update("pw", *patch.Password) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
318 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
319 if patch.Email != nil { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
320 update("email_address", *patch.Email) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
321 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
322 if patch.Country != nil && priv { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
323 update("country", *patch.Country) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
324 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
325 if patch.Reports != nil && priv { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
326 update("report_reciever", *patch.Reports) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
327 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
328 if patch.Extent != nil { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
329 updateBox("map_extent", patch.Extent) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
330 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
331 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
332 var colsS, posS string |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
333 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
334 switch len(columns) { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
335 case 0: // Nothing to do |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
336 jr = mw.JSONResult{ |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
337 Code: http.StatusCreated, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
338 Result: struct { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
339 Result string `json:"result"` |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
340 }{"success"}, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
341 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
342 return |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
343 case 1: // No brackets if there is only one argument. |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
344 colsS = columns[0] |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
345 posS = positions[0] |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
346 default: |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
347 colsS = "(" + strings.Join(columns, ",") + ")" |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
348 posS = "(" + strings.Join(positions, ",") + ")" |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
349 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
350 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
351 stmt := fmt.Sprintf( |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
352 `UPDATE users.list_users SET %s = %s WHERE username = $%d`, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
353 colsS, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
354 posS, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
355 len(positions)+1) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
356 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
357 args = append(args, user) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
358 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
359 db := mw.JSONConn(req) |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
360 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
361 var res sql.Result |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
362 if res, err = db.ExecContext(req.Context(), stmt, args...); err != nil { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
363 return |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
364 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
365 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
366 if n, err2 := res.RowsAffected(); err2 == nil && n == 0 { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
367 err = mw.JSONError{ |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
368 Code: http.StatusNotFound, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
369 Message: fmt.Sprintf("Cannot find user %s.", user), |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
370 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
371 return |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
372 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
373 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
374 if patch.User != nil && *patch.User != user { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
375 // Running in a go routine should not be necessary. |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
376 go func() { auth.Sessions.Logout(string(user)) }() |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
377 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
378 |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
379 jr = mw.JSONResult{ |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
380 Code: http.StatusCreated, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
381 Result: struct { |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
382 Result string `json:"result"` |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
383 }{"success"}, |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
384 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
385 return |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
386 } |
95dafb72a288
Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5330
diff
changeset
|
387 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
388 func createUser(req *http.Request) (jr mw.JSONResult, err error) { |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
389 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
390 user := mw.JSONInput(req).(*models.User) |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
391 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
392 db := mw.JSONConn(req) |
4242
1458c9b0fdaa
Made the sql.Conn in function accessible via the context of the request.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4148
diff
changeset
|
393 |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
394 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
|
395 _, 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
|
396 req.Context(), |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
397 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
|
398 user.Role, |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
399 user.User, |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
400 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
|
401 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
|
402 user.Email, |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
403 user.Reports, |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
404 ) |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
405 } 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
|
406 _, 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
|
407 req.Context(), |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
408 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
|
409 user.Role, |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
410 user.User, |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
411 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
|
412 user.Country, |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
413 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
|
414 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
|
415 user.Email, |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
416 user.Reports, |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
417 ) |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
418 } |
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
419 |
187
51addc0533fe
More complete show case for creating users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
186
diff
changeset
|
420 if err != nil { |
4148
1b1218bbd414
Made 'go vet' happy with controllers again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4071
diff
changeset
|
421 m, c := pgxutils.ReadableError{Err: err}.MessageAndCode() |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
422 err = mw.JSONError{Code: c, Message: m} |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
423 return |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
424 } |
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
425 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
426 jr = mw.JSONResult{ |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
427 Code: http.StatusCreated, |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
428 Result: struct { |
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
429 Result string `json:"result"` |
314
adceb47920fb
Cosmetics. Little less structure bloat.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
311
diff
changeset
|
430 }{"success"}, |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
431 } |
237
3771788d3dae
Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
235
diff
changeset
|
432 return |
186
fe3a88f00b0a
Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
433 } |
250
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
434 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
435 func listUsers(req *http.Request) (jr mw.JSONResult, err error) { |
250
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
436 |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
437 var rows *sql.Rows |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
438 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
439 rows, err = mw.JSONConn(req).QueryContext(req.Context(), listUsersSQL) |
250
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
440 if err != nil { |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
441 return |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
442 } |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
443 defer rows.Close() |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
444 |
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
|
445 var users []*models.User |
250
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
446 |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
447 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
|
448 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
|
449 if err = rows.Scan( |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
450 &user.Role, |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
451 &user.User, |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
452 &user.Country, |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
453 &user.Email, |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
454 &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
|
455 &user.Extent.X2, &user.Extent.Y2, |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
456 &user.Reports, |
250
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
457 ); err != nil { |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
458 return |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
459 } |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
460 users = append(users, user) |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
461 } |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
462 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
463 jr = mw.JSONResult{ |
250
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
464 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
|
465 Users []*models.User `json:"users"` |
314
adceb47920fb
Cosmetics. Little less structure bloat.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
311
diff
changeset
|
466 }{users}, |
250
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
467 } |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
468 return |
deabc2712634
Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
243
diff
changeset
|
469 } |
254
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
470 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
471 func listUser(req *http.Request) (jr mw.JSONResult, err error) { |
254
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
472 |
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
|
473 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
|
474 if !user.IsValid() { |
4506
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
475 err = mw.JSONError{ |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
476 Code: http.StatusBadRequest, |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
477 Message: "error: user invalid", |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
478 } |
254
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
479 return |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
480 } |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
481 |
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
|
482 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
|
483 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
|
484 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
|
485 } |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
486 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
487 err = mw.JSONConn(req).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
|
488 &result.Role, |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
489 &result.Country, |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
490 &result.Email, |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
491 &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
|
492 &result.Extent.X2, &result.Extent.Y2, |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
493 &result.Reports, |
254
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
494 ) |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
495 |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
496 switch { |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
497 case err == sql.ErrNoRows: |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
498 err = mw.JSONError{ |
254
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
499 Code: http.StatusNotFound, |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
500 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
|
501 } |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
502 return |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
503 case err != nil: |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
504 return |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
505 } |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
506 |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
507 jr.Result = result |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
508 return |
de6fdb316b8f
Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
253
diff
changeset
|
509 } |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
510 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
511 func sendTestMail(req *http.Request) (jr mw.JSONResult, err error) { |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
512 |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
513 user := models.UserName(mux.Vars(req)["user"]) |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
514 if !user.IsValid() { |
4506
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
515 err = mw.JSONError{ |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
516 Code: http.StatusBadRequest, |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
517 Message: "error: user invalid", |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
518 } |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
519 return |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
520 } |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
521 |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
522 userData := &models.User{ |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
523 User: user, |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
524 Extent: &models.BoundingBox{}, |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
525 } |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
526 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
527 err = mw.JSONConn(req).QueryRowContext(req.Context(), listUserSQL, user).Scan( |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
528 &userData.Role, |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
529 &userData.Country, |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
530 &userData.Email, |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
531 &userData.Extent.X1, &userData.Extent.Y1, |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
532 &userData.Extent.X2, &userData.Extent.Y2, |
5330
b1b9b384540d
Adjust user model and controller to handle reports field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4506
diff
changeset
|
533 &userData.Reports, |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
534 ) |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
535 |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
536 switch { |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
537 case err == sql.ErrNoRows: |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
538 err = mw.JSONError{ |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
539 Code: http.StatusNotFound, |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
540 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
|
541 } |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
542 return |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
543 case err != nil: |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
544 return |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
545 } |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
546 |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
547 var subject string |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
548 |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
549 var tmplVars = struct { |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
550 User string |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
551 Timestamp string |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
552 Email string |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
553 }{ |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
554 User: string(user), |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
555 Timestamp: time.Now().Format("2006-01-02 15:04:05"), |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
556 Email: string(userData.Email), |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
557 } |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
558 |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
559 var bodyTmpl *template.Template |
5370
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
560 switch userData.Role { |
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
561 case "sys_admin": |
2895
92812bf2f008
Improve notification email contents
Bernhard Reiter <bernhard@intevation.de>
parents:
2326
diff
changeset
|
562 subject = "Gemma: Sysadmin Notification TEST" |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
563 bodyTmpl = testSysadminNotifyMailTmpl |
5370
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
564 case "waterway_admin": |
2895
92812bf2f008
Improve notification email contents
Bernhard Reiter <bernhard@intevation.de>
parents:
2326
diff
changeset
|
565 subject = "Gemma: Waterway Admin Notification TEST" |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
566 bodyTmpl = testWWAdminNotifyMailTmpl |
5370
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
567 default: |
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
568 subject = "Gemma: Waterway User Notification TEST" |
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
569 bodyTmpl = testWWUserNotifyMailTmpl |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
570 } |
5370
566c8063223b
Allow test mail sinding for waterway users, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5345
diff
changeset
|
571 |
827
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
572 var buf bytes.Buffer |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
573 if err := bodyTmpl.Execute(&buf, &tmplVars); err != nil { |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
574 log.Printf("error: %v\n", err) |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
575 } |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
576 |
f3adc0f3a20a
Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents:
822
diff
changeset
|
577 err = misc.SendMail(string(userData.Email), subject, buf.String()) |
822
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
578 if err != nil { |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
579 return |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
580 } |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
581 |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
582 jr.Result = &struct { |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
583 Message string `json:"message"` |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
584 }{"Sending test mail."} |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
585 |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
586 return |
8926c413db21
Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents:
493
diff
changeset
|
587 } |