annotate pkg/controllers/user.go @ 1654:85386ad17d34

Scheduled imports: Don't track the user in the running scheduler. The user is stored in the database already.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 20 Dec 2018 18:39:19 +0100
parents 5f80ec319a4f
children f32e8a973b48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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"
1654
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
20 "context"
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 "database/sql"
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
22 "fmt"
827
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
23 "log"
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 "net/http"
827
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
25 "text/template"
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
26 "time"
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27
241
3b688fe04c39 No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 240
diff changeset
28 "github.com/gorilla/mux"
3b688fe04c39 No omitempty if JSON serialising PostgreSQL errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 240
diff changeset
29
414
c1047fd04a3a Moved project specific Go packages to new pkg folder.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 343
diff changeset
30 "gemma.intevation.de/gemma/pkg/auth"
822
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
31 "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
32 "gemma.intevation.de/gemma/pkg/models"
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
33 "gemma.intevation.de/gemma/pkg/scheduler"
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 )
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 const (
343
5b03f420957d Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents: 342
diff changeset
37 createUserSQL = `INSERT INTO users.list_users
5b03f420957d Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents: 342
diff changeset
38 VALUES ($1, $2, $3, $4, NULL, $5)`
5b03f420957d Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents: 342
diff changeset
39 createUserExtentSQL = `INSERT INTO users.list_users
5b03f420957d Use INSTEAD OF trigger for user creation
Tom Gottfried <tom@intevation.de>
parents: 342
diff changeset
40 VALUES ($1, $2, $3, $4,
188
ee3093966a6d ST_SetSRID should be not need as the map extent column does not have any SRID code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 187
diff changeset
41 ST_MakeBox2D(ST_Point($5, $6), ST_Point($7, $8)), $9)`
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
42
327
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
43 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
44 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
45 = ($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
46 WHERE username = $1`
307
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
47 updateUserSQL = `UPDATE users.list_users
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
48 SET (rolname, username, pw, country, map_extent, email_address)
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
49 = ($2, $3, $4, $5, NULL, $6)
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
50 WHERE username = $1`
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
51 updateUserExtentSQL = `UPDATE users.list_users
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
52 SET (rolname, username, pw, country, map_extent, email_address)
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
53 = ($2, $3, $4, $5, ST_MakeBox2D(ST_Point($6, $7), ST_Point($8, $9)), $10)
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
54 WHERE username = $1`
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
55
342
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
56 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
57
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
58 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
59 rolname,
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
60 username,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
61 country,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
62 email_address,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
63 ST_XMin(map_extent), ST_YMin(map_extent),
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
64 ST_XMax(map_extent), ST_YMax(map_extent)
279
d89a19c297e0 list_users is now in users schema.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 261
diff changeset
65 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
66
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
67 listUserSQL = `SELECT
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
68 rolname,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
69 country,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
70 email_address,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
71 ST_XMin(map_extent), ST_YMin(map_extent),
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
72 ST_XMax(map_extent), ST_YMax(map_extent)
279
d89a19c297e0 list_users is now in users schema.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 261
diff changeset
73 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
74 WHERE username = $1`
1654
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
75
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
76 scheduledIDsSQL = `
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
77 SELECT id from waterway.import_configuration
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
78 WHERE username = $1`
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 )
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80
827
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
81 var (
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
82 testSysadminNotifyMailTmpl = template.Must(
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
83 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
84
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
85 this is a test email for the Gemma System Errors notification service. You
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
86 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
87 sending function at {{ .Timestamp }}.
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
88
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
89 When a critical system error is detecte an automated mail will be send to
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
90 the address: {{ .Email }} with details on the error condition.`))
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
91
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
92 testWWAdminNotifyMailTmpl = template.Must(
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
93 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
94
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
95 this is a test email for the Gemma System Imports notification service. You
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
96 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
97 sending function at {{ .Timestamp }}.
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
98
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
99 When the status of an data import managed by you changes an automated mail will
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
100 be send to the address: {{ .Email }} with details on the new import status
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
101 (inkluding import errors) and details on the concerned import.`))
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
102 )
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
103
1654
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
104 func scheduledIDs(
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
105 ctx context.Context,
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
106 conn *sql.Conn,
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
107 user string,
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
108 ) (map[int64]struct{}, error) {
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
109 ids := map[int64]struct{}{}
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
110 rows, err := conn.QueryContext(ctx, scheduledIDsSQL, user)
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
111 if err != nil {
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
112 return nil, nil
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
113 }
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
114 defer rows.Close()
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
115 for rows.Next() {
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
116 var id int64
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
117 if err := rows.Scan(&id); err != nil {
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
118 return nil, err
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
119 }
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
120 ids[id] = struct{}{}
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
121 }
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
122 if err := rows.Err(); err != nil {
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
123 return nil, err
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
124 }
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
125 return ids, nil
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
126 }
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
127
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
128 func deleteUser(
288
4befc5868ea6 Mark input in user controllers as unused if they don't need a JSON input.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
129 _ interface{}, req *http.Request,
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
130 db *sql.Conn,
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
131 ) (jr JSONResult, err error) {
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
132
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
133 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
134 if !models.UserName(user).IsValid() {
425
c49f4c1808b1 Simplified user validation with new UserName type.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 418
diff changeset
135 err = JSONError{http.StatusBadRequest, "error: user invalid"}
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
136 return
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
137 }
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
138
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
139 session, _ := auth.GetSession(req)
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
140 if session.User == user {
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
141 err = JSONError{http.StatusBadRequest, "error: cannot delete yourself"}
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
142 return
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
143 }
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
144
1654
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
145 // Remove scheduled tasks.
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
146 ids, err2 := scheduledIDs(req.Context(), db, user)
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
147 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
148 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
149 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
150 }
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
151 } else {
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
152 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
153 }
85386ad17d34 Scheduled imports: Don't track the user in the running scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
154
342
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
155 var res sql.Result
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
156
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
157 if res, err = db.ExecContext(req.Context(), deleteUserSQL, user); err != nil {
342
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
158 return
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
159 }
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
160
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
161 if n, err2 := res.RowsAffected(); err2 == nil && n == 0 {
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
162 err = JSONError{
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
163 Code: http.StatusNotFound,
c6bd6ed18942 Use INSTEAD OF trigger for user deletion
Tom Gottfried <tom@intevation.de>
parents: 328
diff changeset
164 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
165 }
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
166 return
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
167 }
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
168
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
169 // 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
170 go func() { auth.Sessions.Logout(user) }()
240
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
171
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
172 jr = JSONResult{Code: http.StatusNoContent}
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
173 return
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
174 }
9012e4045da4 Implemented /user delete controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 237
diff changeset
175
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
176 func updateUser(
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
177 input interface{},
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
178 req *http.Request,
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
179 db *sql.Conn,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
180 ) (jr JSONResult, err error) {
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
181
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
182 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
183 if !user.IsValid() {
418
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
184 err = JSONError{http.StatusBadRequest, "error: user invalid"}
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
185 return
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
186 }
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
187
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
188 newUser := input.(*models.User)
307
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
189 var res sql.Result
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
190
326
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
191 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
192 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
193 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
194 req.Context(),
326
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
195 updateUserSQL,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
196 user,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
197 newUser.Role,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
198 newUser.User,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
199 newUser.Password,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
200 newUser.Country,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
201 newUser.Email,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
202 )
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
203 } 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
204 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
205 req.Context(),
326
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
206 updateUserExtentSQL,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
207 user,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
208 newUser.Role,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
209 newUser.User,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
210 newUser.Password,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
211 newUser.Country,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
212 newUser.Extent.X1, newUser.Extent.Y1,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
213 newUser.Extent.X2, newUser.Extent.Y2,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
214 newUser.Email,
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
215 )
a7b2db8b3d18 Added type for roles.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
216 }
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
217 } 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
218 if newUser.Extent == nil {
003243ec5ce5 Don't crash if we doing update as unprivileged user without a bounding box.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 327
diff changeset
219 err = JSONError{http.StatusBadRequest, "extent is mandatory"}
003243ec5ce5 Don't crash if we doing update as unprivileged user without a bounding box.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 327
diff changeset
220 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
221 }
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
222 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
223 req.Context(),
327
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
224 updateUserUnprivSQL,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
225 user,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
226 newUser.Password,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
227 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
228 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
229 newUser.Email,
363983d5c567 Allow Waterway User to update a limited set of profile attributes
Tom Gottfried <tom@intevation.de>
parents: 326
diff changeset
230 )
235
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
231 }
7d1f0ffdfa41 Implemented /users update controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 226
diff changeset
232
311
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
233 if err != nil {
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
234 return
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
235 }
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
236
74559e12a59f sql.Result.RowsAffected is a driver specific feature. Check
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 307
diff changeset
237 if n, err2 := res.RowsAffected(); err2 == nil && n == 0 {
307
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
238 err = JSONError{
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
239 Code: http.StatusNotFound,
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
240 Message: fmt.Sprintf("Cannot find user %s.", user),
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
241 }
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
242 return
750a9c9cd965 Use SQL UPDATE to update users
Tom Gottfried <tom@intevation.de>
parents: 288
diff changeset
243 }
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
244
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
245 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
246 // 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
247 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
248 }
ab9859981ee3 If a user got renamed kick her/him from the connection pool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 257
diff changeset
249
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
250 jr = JSONResult{
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
251 Code: http.StatusCreated,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
252 Result: struct {
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
253 Result string `json:"result"`
314
adceb47920fb Cosmetics. Little less structure bloat.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 311
diff changeset
254 }{"success"},
237
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 return
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
257 }
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
258
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
259 func createUser(
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
260 input interface{},
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
261 req *http.Request,
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
262 db *sql.Conn,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
263 ) (jr JSONResult, err error) {
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
264
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
265 user := input.(*models.User)
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
266
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
267 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
268 _, 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
269 req.Context(),
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
270 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
271 user.Role,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
272 user.User,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
273 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
274 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
275 user.Email,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
276 )
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
277 } 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
278 _, 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
279 req.Context(),
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
280 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
281 user.Role,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
282 user.User,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
283 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
284 user.Country,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
285 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
286 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
287 user.Email,
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
288 )
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
289 }
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
290
187
51addc0533fe More complete show case for creating users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 186
diff changeset
291 if err != nil {
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
292 return
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
293 }
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
294
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
295 jr = JSONResult{
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
296 Code: http.StatusCreated,
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
297 Result: struct {
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
298 Result string `json:"result"`
314
adceb47920fb Cosmetics. Little less structure bloat.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 311
diff changeset
299 }{"success"},
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
300 }
237
3771788d3dae Reduce boilerplate code when writing JSON parsing/generating endpoints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 235
diff changeset
301 return
186
fe3a88f00b0a Experimental user creation support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
302 }
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
303
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
304 func listUsers(
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
305 _ interface{},
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
306 req *http.Request,
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
307 db *sql.Conn,
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
308 ) (jr JSONResult, err error) {
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
309
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
310 var rows *sql.Rows
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
311
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
312 rows, err = db.QueryContext(req.Context(), listUsersSQL)
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
313 if err != nil {
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
314 return
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
315 }
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
316 defer rows.Close()
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
317
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
318 var users []*models.User
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
319
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
320 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
321 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
322 if err = rows.Scan(
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
323 &user.Role,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
324 &user.User,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
325 &user.Country,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
326 &user.Email,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
327 &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
328 &user.Extent.X2, &user.Extent.Y2,
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
329 ); err != nil {
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
330 return
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
331 }
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
332 users = append(users, user)
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
333 }
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
334
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
335 jr = JSONResult{
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
336 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
337 Users []*models.User `json:"users"`
314
adceb47920fb Cosmetics. Little less structure bloat.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 311
diff changeset
338 }{users},
250
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
339 }
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
340 return
deabc2712634 Implemented /users GET as list of users.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 243
diff changeset
341 }
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
342
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
343 func listUser(
486
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
344 _ interface{},
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
345 req *http.Request,
b2dc9c2f69e0 First stab to use the metamorphic db to do all database stuff.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
346 db *sql.Conn,
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
347 ) (jr JSONResult, err error) {
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
348
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
349 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
350 if !user.IsValid() {
418
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
351 err = JSONError{http.StatusBadRequest, "error: user invalid"}
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
352 return
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
353 }
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
354
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
355 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
356 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
357 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
358 }
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
359
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
360 err = db.QueryRowContext(req.Context(), listUserSQL, user).Scan(
254
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
361 &result.Role,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
362 &result.Country,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
363 &result.Email,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
364 &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
365 &result.Extent.X2, &result.Extent.Y2,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
366 )
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
367
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
368 switch {
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
369 case err == sql.ErrNoRows:
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
370 err = JSONError{
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
371 Code: http.StatusNotFound,
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
372 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
373 }
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
374 return
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
375 case err != nil:
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
376 return
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
377 }
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
378
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
379 jr.Result = result
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
380 return
de6fdb316b8f Implemented /users/{user} GET a listing of given user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 253
diff changeset
381 }
822
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
382
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
383 func sendTestMail(
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
384 _ interface{},
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
385 req *http.Request,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
386 db *sql.Conn,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
387 ) (jr JSONResult, err error) {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
388
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
389 user := models.UserName(mux.Vars(req)["user"])
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
390 if !user.IsValid() {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
391 err = JSONError{http.StatusBadRequest, "error: user invalid"}
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
392 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
393 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
394
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
395 userData := &models.User{
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
396 User: user,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
397 Extent: &models.BoundingBox{},
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
398 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
399
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
400 err = db.QueryRowContext(req.Context(), listUserSQL, user).Scan(
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
401 &userData.Role,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
402 &userData.Country,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
403 &userData.Email,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
404 &userData.Extent.X1, &userData.Extent.Y1,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
405 &userData.Extent.X2, &userData.Extent.Y2,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
406 )
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
407
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
408 switch {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
409 case err == sql.ErrNoRows:
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
410 err = JSONError{
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
411 Code: http.StatusNotFound,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
412 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
413 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
414 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
415 case err != nil:
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
416 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
417 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
418
827
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
419 var subject string
822
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
420
827
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
421 var tmplVars = struct {
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
422 User string
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
423 Timestamp string
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
424 Email string
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
425 }{
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
426 User: string(user),
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
427 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
428 Email: string(userData.Email),
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
429 }
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
430
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
431 var bodyTmpl *template.Template
822
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
432 if userData.Role == "sys_admin" {
827
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
433 subject = "Sysadmin Notification TEST"
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
434 bodyTmpl = testSysadminNotifyMailTmpl
822
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
435 } else if userData.Role == "waterway_admin" {
827
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
436 subject = "Waterway Admin Notification TEST"
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
437 bodyTmpl = testWWAdminNotifyMailTmpl
822
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
438 } else {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
439 err = JSONError{
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
440 Code: http.StatusBadRequest,
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
441 Message: "Test mails can only be generated for admin roles.",
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
442 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
443 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
444 }
827
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
445 var buf bytes.Buffer
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
446 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
447 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
448 }
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
449
f3adc0f3a20a Use templating to make somewhat more interesting test notification mails.
Sascha Wilde <wilde@intevation.de>
parents: 822
diff changeset
450 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
451 if err != nil {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
452 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
453 }
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
454
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
455 jr.Result = &struct {
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
456 Message string `json:"message"`
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
457 }{"Sending test mail."}
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
458
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
459 return
8926c413db21 Started implementation of test mail end point.
Sascha Wilde <wilde@intevation.de>
parents: 493
diff changeset
460 }