Mercurial > gemma
comparison pkg/models/user.go @ 635:729c2692fea0
Renamed file containing models for users to something more suited.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Wed, 12 Sep 2018 19:55:00 +0200 |
parents | pkg/models/types.go@2ac37419f593 |
children | 756f3fc62da6 |
comparison
equal
deleted
inserted
replaced
634:0c79068c46b7 | 635:729c2692fea0 |
---|---|
1 package models | |
2 | |
3 import ( | |
4 "database/sql/driver" | |
5 "encoding/json" | |
6 "errors" | |
7 "regexp" | |
8 "strings" | |
9 ) | |
10 | |
11 type ( | |
12 Email string | |
13 Country string | |
14 Role string | |
15 UserName string | |
16 | |
17 BoundingBox struct { | |
18 X1 float64 `json:"x1"` | |
19 Y1 float64 `json:"y1"` | |
20 X2 float64 `json:"x2"` | |
21 Y2 float64 `json:"y2"` | |
22 } | |
23 | |
24 User struct { | |
25 User UserName `json:"user"` | |
26 Role Role `json:"role"` | |
27 Password string `json:"password,omitempty"` | |
28 Email Email `json:"email"` | |
29 Country Country `json:"country"` | |
30 Extent *BoundingBox `json:"extent"` | |
31 } | |
32 | |
33 PWResetUser struct { | |
34 User string `json:"user"` | |
35 } | |
36 ) | |
37 | |
38 var ( | |
39 // https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression | |
40 emailRe = regexp.MustCompile( | |
41 `(?:[a-z0-9!#$%&'*+/=?^_` + "`" + | |
42 `{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_` + "`" + | |
43 `{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]` + | |
44 `|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")` + | |
45 `@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?` + | |
46 `|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}` + | |
47 `(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]` + | |
48 `:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]` + | |
49 `|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])`) | |
50 | |
51 errNoEmailAddress = errors.New("Not a valid email address") | |
52 errNoString = errors.New("Not a string") | |
53 ) | |
54 | |
55 func (e *Email) UnmarshalJSON(data []byte) error { | |
56 var s string | |
57 if err := json.Unmarshal(data, &s); err != nil { | |
58 return err | |
59 } | |
60 if !emailRe.MatchString(s) { | |
61 return errNoEmailAddress | |
62 } | |
63 *e = Email(s) | |
64 return nil | |
65 } | |
66 | |
67 func (e Email) Value() (driver.Value, error) { | |
68 return string(e), nil | |
69 } | |
70 | |
71 func (e *Email) Scan(src interface{}) (err error) { | |
72 if s, ok := src.(string); ok { | |
73 *e = Email(s) | |
74 } else { | |
75 err = errNoString | |
76 } | |
77 return | |
78 } | |
79 | |
80 var errNoValidUser = errors.New("Not a valid user") | |
81 | |
82 func (u UserName) IsValid() bool { | |
83 return u != "" | |
84 } | |
85 | |
86 func (u *UserName) UnmarshalJSON(data []byte) error { | |
87 var s string | |
88 if err := json.Unmarshal(data, &s); err != nil { | |
89 return err | |
90 } | |
91 if user := UserName(s); user.IsValid() { | |
92 *u = user | |
93 return nil | |
94 } | |
95 return errNoValidUser | |
96 } | |
97 | |
98 func (u *UserName) Scan(src interface{}) (err error) { | |
99 if s, ok := src.(string); ok { | |
100 *u = UserName(s) | |
101 } else { | |
102 err = errNoString | |
103 } | |
104 return | |
105 } | |
106 | |
107 var ( | |
108 validCountries = []string{ | |
109 "AT", "BG", "DE", "HU", "HR", | |
110 "MD", "RO", "RS", "SK", "UA", | |
111 } | |
112 errNoValidCountry = errors.New("Not a valid country") | |
113 ) | |
114 | |
115 func (c *Country) UnmarshalJSON(data []byte) error { | |
116 var s string | |
117 if err := json.Unmarshal(data, &s); err != nil { | |
118 return err | |
119 } | |
120 s = strings.ToUpper(s) | |
121 for _, v := range validCountries { | |
122 if v == s { | |
123 *c = Country(v) | |
124 return nil | |
125 } | |
126 } | |
127 return errNoValidCountry | |
128 } | |
129 | |
130 func (c Country) Value() (driver.Value, error) { | |
131 return string(c), nil | |
132 } | |
133 | |
134 func (c *Country) Scan(src interface{}) (err error) { | |
135 if s, ok := src.(string); ok { | |
136 *c = Country(s) | |
137 } else { | |
138 err = errNoString | |
139 } | |
140 return | |
141 } | |
142 | |
143 var ( | |
144 validRoles = []string{ | |
145 "waterway_user", | |
146 "waterway_admin", | |
147 "sys_admin", | |
148 } | |
149 errNoValidRole = errors.New("Not a valid role") | |
150 ) | |
151 | |
152 func (r Role) Value() (driver.Value, error) { | |
153 return string(r), nil | |
154 } | |
155 | |
156 func (r *Role) Scan(src interface{}) (err error) { | |
157 if s, ok := src.(string); ok { | |
158 *r = Role(s) | |
159 } else { | |
160 err = errNoString | |
161 } | |
162 return | |
163 } | |
164 | |
165 func (r *Role) UnmarshalJSON(data []byte) error { | |
166 var s string | |
167 if err := json.Unmarshal(data, &s); err != nil { | |
168 return err | |
169 } | |
170 s = strings.ToLower(s) | |
171 for _, v := range validRoles { | |
172 if v == s { | |
173 *r = Role(v) | |
174 return nil | |
175 } | |
176 } | |
177 return errNoValidRole | |
178 } |