annotate pkg/models/user.go @ 5736:55892008ec96 default tip

Fixed a bunch of corner cases in WG import.
author Sascha Wilde <wilde@sha-bang.de>
date Wed, 29 May 2024 19:02:42 +0200
parents 6270951dda28
children
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: 643
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: 643
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: 643
diff changeset
3 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
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: 643
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: 643
diff changeset
6 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
7 // Copyright (C) 2018 by via donau
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
9 // Software engineering by Intevation GmbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
10 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
11 // Author(s):
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
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: 643
diff changeset
13 // * Tom Gottfried <tom.gottfried@intevation.de>
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
14
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: 438
diff changeset
15 package models
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import (
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "database/sql/driver"
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "encoding/json"
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "errors"
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 "regexp"
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "strings"
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 type (
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
26 // Email is a string formed by a valid EMail address.
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
27 Email string
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
28 // Role is a string with a valid gemma role.
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
29 Role string
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
30 // UserName is a string forming a valid user name.
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
31 UserName string
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
33 // BoundingBox is a spatial bounding box of user's
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
34 // responsibility area.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 BoundingBox struct {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 X1 float64 `json:"x1"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 Y1 float64 `json:"y1"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 X2 float64 `json:"x2"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 Y2 float64 `json:"y2"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
42 // User is a serialized JSON form of user data.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 User struct {
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
44 User UserName `json:"user"`
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 Role Role `json:"role"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 Password string `json:"password,omitempty"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 Email Email `json:"email"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 Country Country `json:"country"`
5348
45b03e8ca47e Toggles in user overview and in details section as well established
Thomas Junk <thomas.junk@intevation.de>
parents: 5345
diff changeset
49 Reports bool `json:"reports"`
5498
59cbd1b48c3a Implemented delete a user if its a none admin user or a admin w/o imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5348
diff changeset
50 Active bool `json:"active"`
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 Extent *BoundingBox `json:"extent"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 }
302
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
53
5345
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
54 // UserPatch is used to send only partial updates.
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
55 UserPatch struct {
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
56 User *UserName `json:"user,omitempty"`
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
57 Role *Role `json:"role,omitempty"`
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
58 Password *string `json:"password,omitempty"`
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
59 Email *Email `json:"email,omitempty"`
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
60 Country *Country `json:"country,omitempty"`
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
61 Reports *bool `json:"reports,omitempty"`
5500
f0c668bc4082 Moved active login/nologin to triggers. Allow /user PATCH endpoint to modify the field.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5498
diff changeset
62 Active *bool `json:"active,omitempty"`
5345
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
63 Extent *BoundingBox `json:"extent,omitempty"`
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
64 }
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
65
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
66 // PWResetUser is send to request a password reset for a user.
302
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
67 PWResetUser struct {
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
68 User string `json:"user"`
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
69 }
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 var (
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 // https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 emailRe = regexp.MustCompile(
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 `(?:[a-z0-9!#$%&'*+/=?^_` + "`" +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 `{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_` + "`" +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 `{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]` +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 `|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")` +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 `@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?` +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 `|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}` +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 `(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]` +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 `:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]` +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 `|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])`)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84
4847
4847ac70103a Made staticcheck happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1901
diff changeset
85 errNoEmailAddress = errors.New("not a valid email address")
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
88 // UnmarshalJSON ensures that the given string forms a valid email address.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 func (e *Email) UnmarshalJSON(data []byte) error {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 var s string
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 if err := json.Unmarshal(data, &s); err != nil {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 return err
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 if !emailRe.MatchString(s) {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 return errNoEmailAddress
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 *e = Email(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 return nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
101 // Value implements the driver.Valuer interface.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 func (e Email) Value() (driver.Value, error) {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 return string(e), nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
106 // Scan implements the sql.Scanner interface.
5712
6270951dda28 /interface{}/any/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5500
diff changeset
107 func (e *Email) Scan(src any) (err error) {
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 if s, ok := src.(string); ok {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 *e = Email(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 } else {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 err = errNoString
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 return
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115
4847
4847ac70103a Made staticcheck happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1901
diff changeset
116 var errNoValidUser = errors.New("not a valid user")
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
117
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
118 // IsValid checks if a given user name is valid.
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: 438
diff changeset
119 func (u UserName) IsValid() bool {
467
73c7b2d6246e Used hex-encoded usernames and a stored procedure to decode them to impersonate with the metamorph user.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 442
diff changeset
120 return u != ""
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
121 }
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
122
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
123 // UnmarshalJSON ensures that the given string forms a valid user name.
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
124 func (u *UserName) UnmarshalJSON(data []byte) error {
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
125 var s string
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
126 if err := json.Unmarshal(data, &s); err != nil {
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
127 return err
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
128 }
484
2ac37419f593 Implemented wamos/issue114 (Improve code consistency: For login use json body, disallow GET).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 467
diff changeset
129 if user := UserName(s); user.IsValid() {
2ac37419f593 Implemented wamos/issue114 (Improve code consistency: For login use json body, disallow GET).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 467
diff changeset
130 *u = user
2ac37419f593 Implemented wamos/issue114 (Improve code consistency: For login use json body, disallow GET).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 467
diff changeset
131 return nil
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
132 }
484
2ac37419f593 Implemented wamos/issue114 (Improve code consistency: For login use json body, disallow GET).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 467
diff changeset
133 return errNoValidUser
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
134 }
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
135
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
136 // Scan implements the sql.Scanner interface.
5712
6270951dda28 /interface{}/any/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5500
diff changeset
137 func (u *UserName) Scan(src any) (err error) {
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
138 if s, ok := src.(string); ok {
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
139 *u = UserName(s)
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
140 } else {
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
141 err = errNoString
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
142 }
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
143 return
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
144 }
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
145
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 var (
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147 validRoles = []string{
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 "waterway_user",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 "waterway_admin",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150 "sys_admin",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 }
4847
4847ac70103a Made staticcheck happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1901
diff changeset
152 errNoValidRole = errors.New("not a valid role")
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
155 // Value implements the driver.Valuer interface.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 func (r Role) Value() (driver.Value, error) {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 return string(r), nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
160 // Scan implements the sql.Scanner interface.
5712
6270951dda28 /interface{}/any/
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5500
diff changeset
161 func (r *Role) Scan(src any) (err error) {
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 if s, ok := src.(string); ok {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 *r = Role(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 } else {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 err = errNoString
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 return
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
170 // UnmarshalJSON ensure that the given string is a valid user name.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 func (r *Role) UnmarshalJSON(data []byte) error {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 var s string
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 if err := json.Unmarshal(data, &s); err != nil {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 return err
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 s = strings.ToLower(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 for _, v := range validRoles {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 if v == s {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 *r = Role(v)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 return nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 return errNoValidRole
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 }