annotate pkg/models/user.go @ 1697:f1c3fe8b79f5

Imports: Fixed some golint issues.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 07 Jan 2019 11:02:48 +0100
parents a244b18cb916
children 71b722809b2b
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 // Country is a valid country 2 letter code.
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
29 Country string
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
30 // 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
31 Role string
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
32 // 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
33 UserName string
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
35 // 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
36 // responsibility area.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 BoundingBox struct {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 X1 float64 `json:"x1"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 Y1 float64 `json:"y1"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 X2 float64 `json:"x2"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 Y2 float64 `json:"y2"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
44 // 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
45 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
46 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
47 Role Role `json:"role"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 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
49 Email Email `json:"email"`
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 Country Country `json:"country"`
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
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
54 // 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
55 PWResetUser struct {
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
56 User string `json:"user"`
0777aa6de45b Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 287
diff changeset
57 }
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 var (
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 // 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
62 emailRe = regexp.MustCompile(
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 `(?:[a-z0-9!#$%&'*+/=?^_` + "`" +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 `{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_` + "`" +
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 `{|}~-]+)*|"(?:[\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
66 `|\\[\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
67 `@(?:(?:[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
68 `|\[(?:(?:(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
69 `(?:(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
70 `:(?:[\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
71 `|\\[\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
72
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 errNoEmailAddress = errors.New("Not a valid email address")
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
76 // 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
77 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
78 var s string
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 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
80 return err
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 if !emailRe.MatchString(s) {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 return errNoEmailAddress
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 *e = Email(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 return nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
89 // 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
90 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
91 return string(e), nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
94 // 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
95 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
96 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
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 } else {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 err = errNoString
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 return
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
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
104 var errNoValidUser = errors.New("Not a valid user")
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
105
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
106 // 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
107 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
108 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
109 }
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
110
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
111 // 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
112 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
113 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
114 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
115 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
116 }
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
117 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
118 *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
119 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
120 }
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
121 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
122 }
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
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
124 // 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
125 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
126 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
127 *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
128 } 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
129 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
130 }
c70ddc6eb168 Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 414
diff changeset
131 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
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
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 var (
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135 validCountries = []string{
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 "AT", "BG", "DE", "HU", "HR",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 "MD", "RO", "RS", "SK", "UA",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 errNoValidCountry = errors.New("Not a valid country")
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 )
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
142 // UnmarshalJSON ensures that the given string forms a valid
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
143 // two letter country code.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 func (c *Country) UnmarshalJSON(data []byte) error {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145 var s string
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 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
147 return err
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 s = strings.ToUpper(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150 for _, v := range validCountries {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 if v == s {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152 *c = Country(v)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153 return nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 return errNoValidCountry
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
159 // 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
160 func (c Country) Value() (driver.Value, error) {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161 return string(c), nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
164 // Scan implements the sql.Scanner interfaces.
287
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 func (c *Country) 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
166 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
167 *c = Country(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 } else {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 err = errNoString
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 return
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 }
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 var (
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 validRoles = []string{
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 "waterway_user",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 "waterway_admin",
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 "sys_admin",
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 errNoValidRole = errors.New("Not a valid role")
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
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
183 // 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
184 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
185 return string(r), nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
188 // 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
189 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
190 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
191 *r = Role(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
192 } else {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 err = errNoString
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 return
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
197
1697
f1c3fe8b79f5 Imports: Fixed some golint issues.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
198 // 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
199 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
200 var s string
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
201 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
202 return err
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
203 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
204 s = strings.ToLower(s)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205 for _, v := range validRoles {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 if v == s {
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207 *r = Role(v)
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208 return nil
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
209 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
210 }
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 return errNoValidRole
be6e60fca3dd Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
212 }