Mercurial > gemma
annotate pkg/models/types.go @ 615:4ffdb922db44
Document adding of external OGC services.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Mon, 10 Sep 2018 19:12:24 +0200 |
parents | 2ac37419f593 |
children |
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 errNoString = errors.New("Not a string") |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 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
|
56 var s string |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 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
|
58 return err |
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 if !emailRe.MatchString(s) { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 return errNoEmailAddress |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 *e = Email(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 return nil |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 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
|
68 return string(e), nil |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 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
|
72 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
|
73 *e = Email(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 } else { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 err = errNoString |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 return |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 |
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
|
80 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
|
81 |
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
|
82 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
|
83 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
|
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 |
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 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
|
87 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
|
88 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
|
89 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
|
90 } |
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
|
91 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
|
92 *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
|
93 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
|
94 } |
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
|
95 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
|
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 |
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 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
|
99 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
|
100 *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
|
101 } 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
|
102 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
|
103 } |
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 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
|
105 } |
c70ddc6eb168
Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
106 |
287
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 var ( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 validCountries = []string{ |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
109 "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
|
110 "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
|
111 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 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
|
113 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 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
|
116 var s string |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 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
|
118 return err |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 s = strings.ToUpper(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 for _, v := range validCountries { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 if v == s { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 *c = Country(v) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 return nil |
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 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
127 return errNoValidCountry |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 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
|
131 return string(c), nil |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
134 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
|
135 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
|
136 *c = Country(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
137 } else { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
138 err = errNoString |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
139 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
140 return |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 var ( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 validRoles = []string{ |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
145 "waterway_user", |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
146 "waterway_admin", |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 "sys_admin", |
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 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
|
150 ) |
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 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
|
153 return string(r), 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 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
|
157 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
|
158 *r = Role(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 } else { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
160 err = errNoString |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
161 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
162 return |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
165 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
|
166 var s string |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
167 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
|
168 return err |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
169 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
170 s = strings.ToLower(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
171 for _, v := range validRoles { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
172 if v == s { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
173 *r = Role(v) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 return nil |
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 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
177 return errNoValidRole |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
178 } |