Mercurial > gemma
annotate pkg/models/user.go @ 943:532c8392048f v1-uat1
client: replace arrow image
* Use an arrow image that has the same color and is a triangle.
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Tue, 09 Oct 2018 19:55:33 +0200 |
parents | 756f3fc62da6 |
children | a244b18cb916 |
rev | line source |
---|---|
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
|
1 package models |
287
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import ( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 "database/sql/driver" |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 "encoding/json" |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 "errors" |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 "regexp" |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 "strings" |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 type ( |
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
|
12 Email 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
|
13 Country 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
|
14 Role 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
|
15 UserName string |
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 BoundingBox struct { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 X1 float64 `json:"x1"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 Y1 float64 `json:"y1"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 X2 float64 `json:"x2"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 Y2 float64 `json:"y2"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 } |
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 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
|
25 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
|
26 Role Role `json:"role"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 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
|
28 Email Email `json:"email"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 Country Country `json:"country"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 Extent *BoundingBox `json:"extent"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 } |
302
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
287
diff
changeset
|
32 |
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
287
diff
changeset
|
33 PWResetUser struct { |
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
287
diff
changeset
|
34 User string `json:"user"` |
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
287
diff
changeset
|
35 } |
287
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 var ( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 // 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
|
40 emailRe = regexp.MustCompile( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 `(?:[a-z0-9!#$%&'*+/=?^_` + "`" + |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 `{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_` + "`" + |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 `{|}~-]+)*|"(?:[\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
|
44 `|\\[\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
|
45 `@(?:(?:[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
|
46 `|\[(?:(?:(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
|
47 `(?:(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
|
48 `:(?:[\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
|
49 `|\\[\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
|
50 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 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
|
52 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 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
|
55 var s string |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 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
|
57 return err |
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 if !emailRe.MatchString(s) { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 return errNoEmailAddress |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 *e = Email(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 return nil |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 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
|
67 return string(e), nil |
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 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
|
71 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
|
72 *e = Email(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 } else { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 err = errNoString |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 return |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 |
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
|
79 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
|
80 |
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
|
81 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
|
82 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
|
83 } |
c70ddc6eb168
Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
84 |
c70ddc6eb168
Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 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
|
89 } |
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
|
90 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
|
91 *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
|
92 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
|
93 } |
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
|
94 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
|
95 } |
c70ddc6eb168
Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
96 |
c70ddc6eb168
Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
97 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
|
98 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
|
99 *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
|
100 } 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
|
101 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
|
102 } |
c70ddc6eb168
Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
103 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
|
104 } |
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 |
287
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 var ( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 validCountries = []string{ |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 "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
|
109 "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
|
110 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
111 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
|
112 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
113 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 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
|
115 var s string |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 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
|
117 return err |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 s = strings.ToUpper(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 for _, v := range validCountries { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 if v == s { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 *c = Country(v) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 return nil |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
125 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
126 return errNoValidCountry |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
127 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 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
|
130 return string(c), nil |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
131 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
132 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
133 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
|
134 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
|
135 *c = Country(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
136 } else { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
137 err = errNoString |
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 return |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
142 var ( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 validRoles = []string{ |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 "waterway_user", |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
145 "waterway_admin", |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
146 "sys_admin", |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 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
|
149 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 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
|
152 return string(r), nil |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
155 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
|
156 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
|
157 *r = Role(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
158 } else { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 err = errNoString |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
160 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
161 return |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
164 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
|
165 var s string |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
166 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
|
167 return err |
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 s = strings.ToLower(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
170 for _, v := range validRoles { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
171 if v == s { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
172 *r = Role(v) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
173 return nil |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 } |
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 return errNoValidRole |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
177 } |