Mercurial > gemma
annotate pkg/models/types.go @ 442:fc37e7072022
Moved some models used in controllers to to model package because they may be needed elsewhere (e.g. GeoServer config).
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 21 Aug 2018 16:57:55 +0200 |
parents | pkg/controllers/types.go@ffdb507d5b42 |
children | 73c7b2d6246e |
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" |
438
ffdb507d5b42
Removed db service user. Use an impersonated metamorph user instead.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
425
diff
changeset
|
9 |
ffdb507d5b42
Removed db service user. Use an impersonated metamorph user instead.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
425
diff
changeset
|
10 "gemma.intevation.de/gemma/pkg/auth" |
287
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 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
|
14 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
|
15 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
|
16 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
|
17 UserName string |
287
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 BoundingBox struct { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 X1 float64 `json:"x1"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 Y1 float64 `json:"y1"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 X2 float64 `json:"x2"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 Y2 float64 `json:"y2"` |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 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
|
27 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
|
28 Role Role `json:"role"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 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
|
30 Email Email `json:"email"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 Country Country `json:"country"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 Extent *BoundingBox `json:"extent"` |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 } |
302
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
287
diff
changeset
|
34 |
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
287
diff
changeset
|
35 PWResetUser struct { |
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
287
diff
changeset
|
36 User string `json:"user"` |
0777aa6de45b
Password reset. Part I
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
287
diff
changeset
|
37 } |
287
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 var ( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 // 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
|
42 emailRe = regexp.MustCompile( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 `(?:[a-z0-9!#$%&'*+/=?^_` + "`" + |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 `{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_` + "`" + |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 `{|}~-]+)*|"(?:[\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
|
46 `|\\[\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
|
47 `@(?:(?:[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
|
48 `|\[(?:(?:(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
|
49 `(?:(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
|
50 `:(?:[\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
|
51 `|\\[\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
|
52 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 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
|
54 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
|
55 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 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
|
58 var s string |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 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
|
60 return err |
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 if !emailRe.MatchString(s) { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 return errNoEmailAddress |
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 *e = Email(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 return nil |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 } |
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 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
|
70 return string(e), nil |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 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
|
74 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
|
75 *e = Email(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 } else { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 err = errNoString |
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 return |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 |
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
|
82 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
|
83 |
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
|
84 func (u UserName) IsValid() bool { |
438
ffdb507d5b42
Removed db service user. Use an impersonated metamorph user instead.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
425
diff
changeset
|
85 return u != "" && |
ffdb507d5b42
Removed db service user. Use an impersonated metamorph user instead.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
425
diff
changeset
|
86 !strings.ContainsAny(string(u), auth.InvalidRoleCharacters) |
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
|
87 } |
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 |
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 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
|
90 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
|
91 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
|
92 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
|
93 } |
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 if !emailRe.MatchString(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
|
95 return errNoEmailAddress |
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 user := UserName(s) |
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
|
98 if !user.IsValid() { |
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
|
99 return errNoValidUser |
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 } |
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 *u = 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
|
102 return 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
|
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 |
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 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
|
106 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
|
107 *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
|
108 } 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
|
109 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
|
110 } |
c70ddc6eb168
Don't allow user names to contain any of the following characters \"':;
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
414
diff
changeset
|
111 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
|
112 } |
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 |
287
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 var ( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 validCountries = []string{ |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 "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
|
117 "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
|
118 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 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
|
120 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 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
|
123 var s string |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 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
|
125 return err |
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 s = strings.ToUpper(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 for _, v := range validCountries { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 if v == s { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 *c = Country(v) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
131 return 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 return errNoValidCountry |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
135 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
136 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
137 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
|
138 return string(c), nil |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 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
|
142 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
|
143 *c = Country(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 } else { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
145 err = errNoString |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
146 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 return |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 var ( |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 validRoles = []string{ |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
152 "waterway_user", |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
153 "waterway_admin", |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
154 "sys_admin", |
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 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
|
157 ) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
158 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 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
|
160 return string(r), nil |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
163 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
|
164 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
|
165 *r = Role(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
166 } else { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
167 err = errNoString |
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 return |
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 |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
172 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
|
173 var s string |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 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
|
175 return err |
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 s = strings.ToLower(s) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
178 for _, v := range validRoles { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
179 if v == s { |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
180 *r = Role(v) |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
181 return nil |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
182 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
183 } |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
184 return errNoValidRole |
be6e60fca3dd
Separated the type definitions from the user controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
185 } |