annotate pkg/models/user.go @ 5415:4ad68ab239b7 marking-single-beam

Factored creation of default class breaks in SR import to be reused with markings, too.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 07 Jul 2021 12:01:28 +0200
parents 45b03e8ca47e
children 59cbd1b48c3a
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"`
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 Extent *BoundingBox `json:"extent"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 }
302
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
52
5345
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
53 // 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
54 UserPatch struct {
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
55 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
56 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
57 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
58 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
59 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
60 Reports *bool `json:"reports,omitempty"`
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
61 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
62 }
95dafb72a288 Added a PATCH endpoint for /api/users/{user}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5330
diff changeset
63
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
64 // 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
65 PWResetUser struct {
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
66 User string `json:"user"`
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
67 }
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 var (
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 // 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
72 emailRe = regexp.MustCompile(
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 `(?:[a-z0-9!#$%&'*+/=?^_` + "`" +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 `{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_` + "`" +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 `{|}~-]+)*|"(?:[\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
76 `|\\[\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
77 `@(?:(?:[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
78 `|\[(?:(?:(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
79 `(?:(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
80 `:(?:[\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
81 `|\\[\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
82
4847
4847ac70103a Made staticcheck happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1901
diff changeset
83 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
84 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
86 // 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
87 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
88 var s string
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 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
90 return err
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 if !emailRe.MatchString(s) {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 return errNoEmailAddress
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 *e = Email(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 return nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
99 // 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
100 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
101 return string(e), nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
104 // Scan implements the sql.Scanner interface.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 func (e *Email) Scan(src interface{}) (err error) {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 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
107 *e = Email(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 } else {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 err = errNoString
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 return
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
4847
4847ac70103a Made staticcheck happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1901
diff changeset
114 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
115
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
116 // 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
117 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
118 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
119 }
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
120
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
121 // 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
122 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
123 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
124 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
125 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
126 }
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
127 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
128 *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
129 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
130 }
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
131 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
132 }
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
133
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
134 // Scan implements the sql.Scanner interface.
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
135 func (u *UserName) Scan(src interface{}) (err 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
136 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
137 *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
138 } 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
139 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
140 }
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 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
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
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 var (
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145 validRoles = []string{
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 "waterway_user",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147 "waterway_admin",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 "sys_admin",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 }
4847
4847ac70103a Made staticcheck happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1901
diff changeset
150 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
151 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
153 // 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
154 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
155 return string(r), nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
158 // Scan implements the sql.Scanner interface.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159 func (r *Role) Scan(src interface{}) (err error) {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 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
161 *r = Role(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 } else {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 err = errNoString
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 return
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
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
168 // 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
169 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
170 var s string
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 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
172 return err
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 s = strings.ToLower(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 for _, v := range validRoles {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 if v == s {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 *r = Role(v)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 return nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181 return errNoValidRole
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 }