Mercurial > gemma
comparison pkg/models/user.go @ 1879:9a2fbeaabd52 dev-pdf-generation
merging in from branch default
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Tue, 15 Jan 2019 10:07:10 +0100 |
parents | f1c3fe8b79f5 |
children | 71b722809b2b |
comparison
equal
deleted
inserted
replaced
1878:f030182f82f1 | 1879:9a2fbeaabd52 |
---|---|
21 "regexp" | 21 "regexp" |
22 "strings" | 22 "strings" |
23 ) | 23 ) |
24 | 24 |
25 type ( | 25 type ( |
26 Email string | 26 // Email is a string formed by a valid EMail address. |
27 Country string | 27 Email string |
28 Role string | 28 // Country is a valid country 2 letter code. |
29 Country string | |
30 // Role is a string with a valid gemma role. | |
31 Role string | |
32 // UserName is a string forming a valid user name. | |
29 UserName string | 33 UserName string |
30 | 34 |
35 // BoundingBox is a spatial bounding box of user's | |
36 // responsibility area. | |
31 BoundingBox struct { | 37 BoundingBox struct { |
32 X1 float64 `json:"x1"` | 38 X1 float64 `json:"x1"` |
33 Y1 float64 `json:"y1"` | 39 Y1 float64 `json:"y1"` |
34 X2 float64 `json:"x2"` | 40 X2 float64 `json:"x2"` |
35 Y2 float64 `json:"y2"` | 41 Y2 float64 `json:"y2"` |
36 } | 42 } |
37 | 43 |
44 // User is a serialized JSON form of user data. | |
38 User struct { | 45 User struct { |
39 User UserName `json:"user"` | 46 User UserName `json:"user"` |
40 Role Role `json:"role"` | 47 Role Role `json:"role"` |
41 Password string `json:"password,omitempty"` | 48 Password string `json:"password,omitempty"` |
42 Email Email `json:"email"` | 49 Email Email `json:"email"` |
43 Country Country `json:"country"` | 50 Country Country `json:"country"` |
44 Extent *BoundingBox `json:"extent"` | 51 Extent *BoundingBox `json:"extent"` |
45 } | 52 } |
46 | 53 |
54 // PWResetUser is send to request a password reset for a user. | |
47 PWResetUser struct { | 55 PWResetUser struct { |
48 User string `json:"user"` | 56 User string `json:"user"` |
49 } | 57 } |
50 ) | 58 ) |
51 | 59 |
63 `|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])`) | 71 `|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])`) |
64 | 72 |
65 errNoEmailAddress = errors.New("Not a valid email address") | 73 errNoEmailAddress = errors.New("Not a valid email address") |
66 ) | 74 ) |
67 | 75 |
76 // UnmarshalJSON ensures that the given string forms a valid email address. | |
68 func (e *Email) UnmarshalJSON(data []byte) error { | 77 func (e *Email) UnmarshalJSON(data []byte) error { |
69 var s string | 78 var s string |
70 if err := json.Unmarshal(data, &s); err != nil { | 79 if err := json.Unmarshal(data, &s); err != nil { |
71 return err | 80 return err |
72 } | 81 } |
75 } | 84 } |
76 *e = Email(s) | 85 *e = Email(s) |
77 return nil | 86 return nil |
78 } | 87 } |
79 | 88 |
89 // Value implements the driver.Valuer interface. | |
80 func (e Email) Value() (driver.Value, error) { | 90 func (e Email) Value() (driver.Value, error) { |
81 return string(e), nil | 91 return string(e), nil |
82 } | 92 } |
83 | 93 |
94 // Scan implements the sql.Scanner interface. | |
84 func (e *Email) Scan(src interface{}) (err error) { | 95 func (e *Email) Scan(src interface{}) (err error) { |
85 if s, ok := src.(string); ok { | 96 if s, ok := src.(string); ok { |
86 *e = Email(s) | 97 *e = Email(s) |
87 } else { | 98 } else { |
88 err = errNoString | 99 err = errNoString |
90 return | 101 return |
91 } | 102 } |
92 | 103 |
93 var errNoValidUser = errors.New("Not a valid user") | 104 var errNoValidUser = errors.New("Not a valid user") |
94 | 105 |
106 // IsValid checks if a given user name is valid. | |
95 func (u UserName) IsValid() bool { | 107 func (u UserName) IsValid() bool { |
96 return u != "" | 108 return u != "" |
97 } | 109 } |
98 | 110 |
111 // UnmarshalJSON ensures that the given string forms a valid user name. | |
99 func (u *UserName) UnmarshalJSON(data []byte) error { | 112 func (u *UserName) UnmarshalJSON(data []byte) error { |
100 var s string | 113 var s string |
101 if err := json.Unmarshal(data, &s); err != nil { | 114 if err := json.Unmarshal(data, &s); err != nil { |
102 return err | 115 return err |
103 } | 116 } |
106 return nil | 119 return nil |
107 } | 120 } |
108 return errNoValidUser | 121 return errNoValidUser |
109 } | 122 } |
110 | 123 |
124 // Scan implements the sql.Scanner interface. | |
111 func (u *UserName) Scan(src interface{}) (err error) { | 125 func (u *UserName) Scan(src interface{}) (err error) { |
112 if s, ok := src.(string); ok { | 126 if s, ok := src.(string); ok { |
113 *u = UserName(s) | 127 *u = UserName(s) |
114 } else { | 128 } else { |
115 err = errNoString | 129 err = errNoString |
123 "MD", "RO", "RS", "SK", "UA", | 137 "MD", "RO", "RS", "SK", "UA", |
124 } | 138 } |
125 errNoValidCountry = errors.New("Not a valid country") | 139 errNoValidCountry = errors.New("Not a valid country") |
126 ) | 140 ) |
127 | 141 |
142 // UnmarshalJSON ensures that the given string forms a valid | |
143 // two letter country code. | |
128 func (c *Country) UnmarshalJSON(data []byte) error { | 144 func (c *Country) UnmarshalJSON(data []byte) error { |
129 var s string | 145 var s string |
130 if err := json.Unmarshal(data, &s); err != nil { | 146 if err := json.Unmarshal(data, &s); err != nil { |
131 return err | 147 return err |
132 } | 148 } |
138 } | 154 } |
139 } | 155 } |
140 return errNoValidCountry | 156 return errNoValidCountry |
141 } | 157 } |
142 | 158 |
159 // Value implements the driver.Valuer interface. | |
143 func (c Country) Value() (driver.Value, error) { | 160 func (c Country) Value() (driver.Value, error) { |
144 return string(c), nil | 161 return string(c), nil |
145 } | 162 } |
146 | 163 |
164 // Scan implements the sql.Scanner interfaces. | |
147 func (c *Country) Scan(src interface{}) (err error) { | 165 func (c *Country) Scan(src interface{}) (err error) { |
148 if s, ok := src.(string); ok { | 166 if s, ok := src.(string); ok { |
149 *c = Country(s) | 167 *c = Country(s) |
150 } else { | 168 } else { |
151 err = errNoString | 169 err = errNoString |
160 "sys_admin", | 178 "sys_admin", |
161 } | 179 } |
162 errNoValidRole = errors.New("Not a valid role") | 180 errNoValidRole = errors.New("Not a valid role") |
163 ) | 181 ) |
164 | 182 |
183 // Value implements the driver.Valuer interface. | |
165 func (r Role) Value() (driver.Value, error) { | 184 func (r Role) Value() (driver.Value, error) { |
166 return string(r), nil | 185 return string(r), nil |
167 } | 186 } |
168 | 187 |
188 // Scan implements the sql.Scanner interface. | |
169 func (r *Role) Scan(src interface{}) (err error) { | 189 func (r *Role) Scan(src interface{}) (err error) { |
170 if s, ok := src.(string); ok { | 190 if s, ok := src.(string); ok { |
171 *r = Role(s) | 191 *r = Role(s) |
172 } else { | 192 } else { |
173 err = errNoString | 193 err = errNoString |
174 } | 194 } |
175 return | 195 return |
176 } | 196 } |
177 | 197 |
198 // UnmarshalJSON ensure that the given string is a valid user name. | |
178 func (r *Role) UnmarshalJSON(data []byte) error { | 199 func (r *Role) UnmarshalJSON(data []byte) error { |
179 var s string | 200 var s string |
180 if err := json.Unmarshal(data, &s); err != nil { | 201 if err := json.Unmarshal(data, &s); err != nil { |
181 return err | 202 return err |
182 } | 203 } |