Show More
@@ -1,694 +1,716 | |||
|
1 | 1 | """ this is forms validation classes |
|
2 | 2 | http://formencode.org/module-formencode.validators.html |
|
3 | 3 | for list off all availible validators |
|
4 | 4 | |
|
5 | 5 | we can create our own validators |
|
6 | 6 | |
|
7 | 7 | The table below outlines the options which can be used in a schema in addition to the validators themselves |
|
8 | 8 | pre_validators [] These validators will be applied before the schema |
|
9 | 9 | chained_validators [] These validators will be applied after the schema |
|
10 | 10 | allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present |
|
11 | 11 | filter_extra_fields False If True, then keys that aren't associated with a validator are removed |
|
12 | 12 | if_key_missing NoDefault If this is given, then any keys that aren't available but are expected will be replaced with this value (and then validated). This does not override a present .if_missing attribute on validators. NoDefault is a special FormEncode class to mean that no default values has been specified and therefore missing keys shouldn't take a default value. |
|
13 | 13 | ignore_key_missing False If True, then missing keys will be missing in the result, if the validator doesn't have .if_missing on it already |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | <name> = formencode.validators.<name of validator> |
|
17 | 17 | <name> must equal form name |
|
18 | 18 | list=[1,2,3,4,5] |
|
19 | 19 | for SELECT use formencode.All(OneOf(list), Int()) |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | import os |
|
23 | 23 | import re |
|
24 | 24 | import logging |
|
25 | 25 | import traceback |
|
26 | 26 | |
|
27 | 27 | import formencode |
|
28 | 28 | from formencode import All |
|
29 | 29 | from formencode.validators import UnicodeString, OneOf, Int, Number, Regex, \ |
|
30 | 30 | Email, Bool, StringBoolean, Set |
|
31 | 31 | |
|
32 | 32 | from pylons.i18n.translation import _ |
|
33 | 33 | from webhelpers.pylonslib.secure_form import authentication_token |
|
34 | 34 | |
|
35 | 35 | from rhodecode.config.routing import ADMIN_PREFIX |
|
36 | 36 | from rhodecode.lib.utils import repo_name_slug |
|
37 | 37 | from rhodecode.lib.auth import authenticate, get_crypt_password |
|
38 | 38 | from rhodecode.lib.exceptions import LdapImportError |
|
39 | 39 | from rhodecode.model.user import UserModel |
|
40 | 40 | from rhodecode.model.repo import RepoModel |
|
41 | from rhodecode.model.db import User, UsersGroup, Group | |
|
41 | from rhodecode.model.db import User, UsersGroup, Group, Repository | |
|
42 | 42 | from rhodecode import BACKENDS |
|
43 | 43 | |
|
44 | 44 | log = logging.getLogger(__name__) |
|
45 | 45 | |
|
46 | 46 | #this is needed to translate the messages using _() in validators |
|
47 | 47 | class State_obj(object): |
|
48 | 48 | _ = staticmethod(_) |
|
49 | 49 | |
|
50 | 50 | #============================================================================== |
|
51 | 51 | # VALIDATORS |
|
52 | 52 | #============================================================================== |
|
53 | 53 | class ValidAuthToken(formencode.validators.FancyValidator): |
|
54 | 54 | messages = {'invalid_token':_('Token mismatch')} |
|
55 | 55 | |
|
56 | 56 | def validate_python(self, value, state): |
|
57 | 57 | |
|
58 | 58 | if value != authentication_token(): |
|
59 | 59 | raise formencode.Invalid(self.message('invalid_token', state, |
|
60 | 60 | search_number=value), value, state) |
|
61 | 61 | |
|
62 | 62 | def ValidUsername(edit, old_data): |
|
63 | 63 | class _ValidUsername(formencode.validators.FancyValidator): |
|
64 | 64 | |
|
65 | 65 | def validate_python(self, value, state): |
|
66 | 66 | if value in ['default', 'new_user']: |
|
67 | 67 | raise formencode.Invalid(_('Invalid username'), value, state) |
|
68 | 68 | #check if user is unique |
|
69 | 69 | old_un = None |
|
70 | 70 | if edit: |
|
71 | 71 | old_un = UserModel().get(old_data.get('user_id')).username |
|
72 | 72 | |
|
73 | 73 | if old_un != value or not edit: |
|
74 | 74 | if User.get_by_username(value, case_insensitive=True): |
|
75 | 75 | raise formencode.Invalid(_('This username already ' |
|
76 | 76 | 'exists') , value, state) |
|
77 | 77 | |
|
78 | 78 | if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+$', value) is None: |
|
79 | 79 | raise formencode.Invalid(_('Username may only contain ' |
|
80 | 80 | 'alphanumeric characters ' |
|
81 | 81 | 'underscores, periods or dashes ' |
|
82 | 82 | 'and must begin with alphanumeric ' |
|
83 | 83 | 'character'), value, state) |
|
84 | 84 | |
|
85 | 85 | return _ValidUsername |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | def ValidUsersGroup(edit, old_data): |
|
89 | 89 | |
|
90 | 90 | class _ValidUsersGroup(formencode.validators.FancyValidator): |
|
91 | 91 | |
|
92 | 92 | def validate_python(self, value, state): |
|
93 | 93 | if value in ['default']: |
|
94 | 94 | raise formencode.Invalid(_('Invalid group name'), value, state) |
|
95 | 95 | #check if group is unique |
|
96 | 96 | old_ugname = None |
|
97 | 97 | if edit: |
|
98 | 98 | old_ugname = UsersGroup.get( |
|
99 | 99 | old_data.get('users_group_id')).users_group_name |
|
100 | 100 | |
|
101 | 101 | if old_ugname != value or not edit: |
|
102 | 102 | if UsersGroup.get_by_group_name(value, cache=False, |
|
103 | 103 | case_insensitive=True): |
|
104 | 104 | raise formencode.Invalid(_('This users group ' |
|
105 | 105 | 'already exists') , value, |
|
106 | 106 | state) |
|
107 | 107 | |
|
108 | 108 | |
|
109 | 109 | if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+$', value) is None: |
|
110 | 110 | raise formencode.Invalid(_('Group name may only contain ' |
|
111 | 111 | 'alphanumeric characters ' |
|
112 | 112 | 'underscores, periods or dashes ' |
|
113 | 113 | 'and must begin with alphanumeric ' |
|
114 | 114 | 'character'), value, state) |
|
115 | 115 | |
|
116 | 116 | return _ValidUsersGroup |
|
117 | 117 | |
|
118 | 118 | |
|
119 | 119 | def ValidReposGroup(edit, old_data): |
|
120 | 120 | class _ValidReposGroup(formencode.validators.FancyValidator): |
|
121 | 121 | |
|
122 | 122 | def validate_python(self, value, state): |
|
123 | #TODO WRITE VALIDATIONS | |
|
123 | # TODO WRITE VALIDATIONS | |
|
124 | 124 | group_name = value.get('group_name') |
|
125 |
group_parent_id = |
|
|
125 | group_parent_id = value.get('group_parent_id') | |
|
126 | 126 | |
|
127 | 127 | # slugify repo group just in case :) |
|
128 | 128 | slug = repo_name_slug(group_name) |
|
129 | 129 | |
|
130 | 130 | # check for parent of self |
|
131 | if edit and old_data['group_id'] == group_parent_id: | |
|
132 | e_dict = {'group_parent_id':_('Cannot assign this group ' | |
|
133 | 'as parent')} | |
|
131 | parent_of_self = lambda: ( | |
|
132 | old_data['group_id'] == int(group_parent_id) | |
|
133 | if group_parent_id else False | |
|
134 | ) | |
|
135 | if edit and parent_of_self(): | |
|
136 | e_dict = { | |
|
137 | 'group_parent_id': _('Cannot assign this group as parent') | |
|
138 | } | |
|
134 | 139 | raise formencode.Invalid('', value, state, |
|
135 | 140 | error_dict=e_dict) |
|
136 | 141 | |
|
137 | 142 | old_gname = None |
|
138 | 143 | if edit: |
|
139 | 144 | old_gname = Group.get( |
|
140 | 145 | old_data.get('group_id')).group_name |
|
141 | 146 | |
|
142 | 147 | if old_gname != group_name or not edit: |
|
143 | # check filesystem | |
|
144 | gr = Group.query().filter(Group.group_name == slug)\ | |
|
145 | .filter(Group.group_parent_id == group_parent_id).scalar() | |
|
148 | ||
|
149 | # check group | |
|
150 | gr = Group.query()\ | |
|
151 | .filter(Group.group_name == slug)\ | |
|
152 | .filter(Group.group_parent_id == group_parent_id)\ | |
|
153 | .scalar() | |
|
146 | 154 | |
|
147 | 155 | if gr: |
|
148 | e_dict = {'group_name':_('This group already exists')} | |
|
156 | e_dict = { | |
|
157 | 'group_name': _('This group already exists') | |
|
158 | } | |
|
159 | raise formencode.Invalid('', value, state, | |
|
160 | error_dict=e_dict) | |
|
161 | ||
|
162 | # check for same repo | |
|
163 | repo = Repository.query()\ | |
|
164 | .filter(Repository.repo_name == slug)\ | |
|
165 | .scalar() | |
|
166 | ||
|
167 | if repo: | |
|
168 | e_dict = { | |
|
169 | 'group_name': _('Repository with this name already exists') | |
|
170 | } | |
|
149 | 171 | raise formencode.Invalid('', value, state, |
|
150 | 172 | error_dict=e_dict) |
|
151 | 173 | |
|
152 | 174 | return _ValidReposGroup |
|
153 | 175 | |
|
154 | 176 | class ValidPassword(formencode.validators.FancyValidator): |
|
155 | 177 | |
|
156 | 178 | def to_python(self, value, state): |
|
157 | 179 | |
|
158 | 180 | if value: |
|
159 | 181 | |
|
160 | 182 | if value.get('password'): |
|
161 | 183 | try: |
|
162 | 184 | value['password'] = get_crypt_password(value['password']) |
|
163 | 185 | except UnicodeEncodeError: |
|
164 | 186 | e_dict = {'password':_('Invalid characters in password')} |
|
165 | 187 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
166 | 188 | |
|
167 | 189 | if value.get('password_confirmation'): |
|
168 | 190 | try: |
|
169 | 191 | value['password_confirmation'] = \ |
|
170 | 192 | get_crypt_password(value['password_confirmation']) |
|
171 | 193 | except UnicodeEncodeError: |
|
172 | 194 | e_dict = {'password_confirmation':_('Invalid characters in password')} |
|
173 | 195 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
174 | 196 | |
|
175 | 197 | if value.get('new_password'): |
|
176 | 198 | try: |
|
177 | 199 | value['new_password'] = \ |
|
178 | 200 | get_crypt_password(value['new_password']) |
|
179 | 201 | except UnicodeEncodeError: |
|
180 | 202 | e_dict = {'new_password':_('Invalid characters in password')} |
|
181 | 203 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
182 | 204 | |
|
183 | 205 | return value |
|
184 | 206 | |
|
185 | 207 | class ValidPasswordsMatch(formencode.validators.FancyValidator): |
|
186 | 208 | |
|
187 | 209 | def validate_python(self, value, state): |
|
188 | 210 | |
|
189 | 211 | pass_val = value.get('password') or value.get('new_password') |
|
190 | 212 | if pass_val != value['password_confirmation']: |
|
191 | 213 | e_dict = {'password_confirmation': |
|
192 | 214 | _('Passwords do not match')} |
|
193 | 215 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
194 | 216 | |
|
195 | 217 | class ValidAuth(formencode.validators.FancyValidator): |
|
196 | 218 | messages = { |
|
197 | 219 | 'invalid_password':_('invalid password'), |
|
198 | 220 | 'invalid_login':_('invalid user name'), |
|
199 | 221 | 'disabled_account':_('Your account is disabled') |
|
200 | 222 | } |
|
201 | 223 | |
|
202 | 224 | # error mapping |
|
203 | 225 | e_dict = {'username':messages['invalid_login'], |
|
204 | 226 | 'password':messages['invalid_password']} |
|
205 | 227 | e_dict_disable = {'username':messages['disabled_account']} |
|
206 | 228 | |
|
207 | 229 | def validate_python(self, value, state): |
|
208 | 230 | password = value['password'] |
|
209 | 231 | username = value['username'] |
|
210 | 232 | user = User.get_by_username(username) |
|
211 | 233 | |
|
212 | 234 | if authenticate(username, password): |
|
213 | 235 | return value |
|
214 | 236 | else: |
|
215 | 237 | if user and user.active is False: |
|
216 | 238 | log.warning('user %s is disabled', username) |
|
217 | 239 | raise formencode.Invalid(self.message('disabled_account', |
|
218 | 240 | state=State_obj), |
|
219 | 241 | value, state, |
|
220 | 242 | error_dict=self.e_dict_disable) |
|
221 | 243 | else: |
|
222 | 244 | log.warning('user %s not authenticated', username) |
|
223 | 245 | raise formencode.Invalid(self.message('invalid_password', |
|
224 | 246 | state=State_obj), value, state, |
|
225 | 247 | error_dict=self.e_dict) |
|
226 | 248 | |
|
227 | 249 | class ValidRepoUser(formencode.validators.FancyValidator): |
|
228 | 250 | |
|
229 | 251 | def to_python(self, value, state): |
|
230 | 252 | try: |
|
231 | 253 | User.query().filter(User.active == True)\ |
|
232 | 254 | .filter(User.username == value).one() |
|
233 | 255 | except Exception: |
|
234 | 256 | raise formencode.Invalid(_('This username is not valid'), |
|
235 | 257 | value, state) |
|
236 | 258 | return value |
|
237 | 259 | |
|
238 | 260 | def ValidRepoName(edit, old_data): |
|
239 | 261 | class _ValidRepoName(formencode.validators.FancyValidator): |
|
240 | 262 | def to_python(self, value, state): |
|
241 | 263 | |
|
242 | 264 | repo_name = value.get('repo_name') |
|
243 | 265 | |
|
244 | 266 | slug = repo_name_slug(repo_name) |
|
245 | 267 | if slug in [ADMIN_PREFIX, '']: |
|
246 | 268 | e_dict = {'repo_name': _('This repository name is disallowed')} |
|
247 | 269 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
248 | 270 | |
|
249 | 271 | |
|
250 | 272 | if value.get('repo_group'): |
|
251 | 273 | gr = Group.get(value.get('repo_group')) |
|
252 | 274 | group_path = gr.full_path |
|
253 | 275 | # value needs to be aware of group name in order to check |
|
254 | 276 | # db key This is an actual just the name to store in the |
|
255 | 277 | # database |
|
256 | 278 | repo_name_full = group_path + Group.url_sep() + repo_name |
|
257 | 279 | |
|
258 | 280 | else: |
|
259 | 281 | group_path = '' |
|
260 | 282 | repo_name_full = repo_name |
|
261 | 283 | |
|
262 | 284 | |
|
263 | 285 | value['repo_name_full'] = repo_name_full |
|
264 | 286 | rename = old_data.get('repo_name') != repo_name_full |
|
265 | 287 | create = not edit |
|
266 | 288 | if rename or create: |
|
267 | 289 | |
|
268 | 290 | if group_path != '': |
|
269 | 291 | if RepoModel().get_by_repo_name(repo_name_full,): |
|
270 | 292 | e_dict = {'repo_name':_('This repository already ' |
|
271 | 293 | 'exists in a group "%s"') % |
|
272 | 294 | gr.group_name} |
|
273 | 295 | raise formencode.Invalid('', value, state, |
|
274 | 296 | error_dict=e_dict) |
|
275 | 297 | elif Group.get_by_group_name(repo_name_full): |
|
276 | 298 | e_dict = {'repo_name':_('There is a group with this' |
|
277 | 299 | ' name already "%s"') % |
|
278 | 300 | repo_name_full} |
|
279 | 301 | raise formencode.Invalid('', value, state, |
|
280 | 302 | error_dict=e_dict) |
|
281 | 303 | |
|
282 | 304 | elif RepoModel().get_by_repo_name(repo_name_full): |
|
283 | 305 | e_dict = {'repo_name':_('This repository ' |
|
284 | 306 | 'already exists')} |
|
285 | 307 | raise formencode.Invalid('', value, state, |
|
286 | 308 | error_dict=e_dict) |
|
287 | 309 | |
|
288 | 310 | return value |
|
289 | 311 | |
|
290 | 312 | return _ValidRepoName |
|
291 | 313 | |
|
292 | 314 | def ValidForkName(): |
|
293 | 315 | class _ValidForkName(formencode.validators.FancyValidator): |
|
294 | 316 | def to_python(self, value, state): |
|
295 | 317 | |
|
296 | 318 | repo_name = value.get('fork_name') |
|
297 | 319 | |
|
298 | 320 | slug = repo_name_slug(repo_name) |
|
299 | 321 | if slug in [ADMIN_PREFIX, '']: |
|
300 | 322 | e_dict = {'repo_name': _('This repository name is disallowed')} |
|
301 | 323 | raise formencode.Invalid('', value, state, error_dict=e_dict) |
|
302 | 324 | |
|
303 | 325 | if RepoModel().get_by_repo_name(repo_name): |
|
304 | 326 | e_dict = {'fork_name':_('This repository ' |
|
305 | 327 | 'already exists')} |
|
306 | 328 | raise formencode.Invalid('', value, state, |
|
307 | 329 | error_dict=e_dict) |
|
308 | 330 | return value |
|
309 | 331 | return _ValidForkName |
|
310 | 332 | |
|
311 | 333 | |
|
312 | 334 | def SlugifyName(): |
|
313 | 335 | class _SlugifyName(formencode.validators.FancyValidator): |
|
314 | 336 | |
|
315 | 337 | def to_python(self, value, state): |
|
316 | 338 | return repo_name_slug(value) |
|
317 | 339 | |
|
318 | 340 | return _SlugifyName |
|
319 | 341 | |
|
320 | 342 | def ValidCloneUri(): |
|
321 | 343 | from mercurial.httprepo import httprepository, httpsrepository |
|
322 | 344 | from rhodecode.lib.utils import make_ui |
|
323 | 345 | |
|
324 | 346 | class _ValidCloneUri(formencode.validators.FancyValidator): |
|
325 | 347 | |
|
326 | 348 | def to_python(self, value, state): |
|
327 | 349 | if not value: |
|
328 | 350 | pass |
|
329 | 351 | elif value.startswith('https'): |
|
330 | 352 | try: |
|
331 | 353 | httpsrepository(make_ui('db'), value).capabilities |
|
332 | 354 | except Exception, e: |
|
333 | 355 | log.error(traceback.format_exc()) |
|
334 | 356 | raise formencode.Invalid(_('invalid clone url'), value, |
|
335 | 357 | state) |
|
336 | 358 | elif value.startswith('http'): |
|
337 | 359 | try: |
|
338 | 360 | httprepository(make_ui('db'), value).capabilities |
|
339 | 361 | except Exception, e: |
|
340 | 362 | log.error(traceback.format_exc()) |
|
341 | 363 | raise formencode.Invalid(_('invalid clone url'), value, |
|
342 | 364 | state) |
|
343 | 365 | else: |
|
344 | 366 | raise formencode.Invalid(_('Invalid clone url, provide a ' |
|
345 | 367 | 'valid clone http\s url'), value, |
|
346 | 368 | state) |
|
347 | 369 | return value |
|
348 | 370 | |
|
349 | 371 | return _ValidCloneUri |
|
350 | 372 | |
|
351 | 373 | def ValidForkType(old_data): |
|
352 | 374 | class _ValidForkType(formencode.validators.FancyValidator): |
|
353 | 375 | |
|
354 | 376 | def to_python(self, value, state): |
|
355 | 377 | if old_data['repo_type'] != value: |
|
356 | 378 | raise formencode.Invalid(_('Fork have to be the same ' |
|
357 | 379 | 'type as original'), value, state) |
|
358 | 380 | |
|
359 | 381 | return value |
|
360 | 382 | return _ValidForkType |
|
361 | 383 | |
|
362 | 384 | class ValidPerms(formencode.validators.FancyValidator): |
|
363 | 385 | messages = {'perm_new_member_name':_('This username or users group name' |
|
364 | 386 | ' is not valid')} |
|
365 | 387 | |
|
366 | 388 | def to_python(self, value, state): |
|
367 | 389 | perms_update = [] |
|
368 | 390 | perms_new = [] |
|
369 | 391 | #build a list of permission to update and new permission to create |
|
370 | 392 | for k, v in value.items(): |
|
371 | 393 | #means new added member to permissions |
|
372 | 394 | if k.startswith('perm_new_member'): |
|
373 | 395 | new_perm = value.get('perm_new_member', False) |
|
374 | 396 | new_member = value.get('perm_new_member_name', False) |
|
375 | 397 | new_type = value.get('perm_new_member_type') |
|
376 | 398 | |
|
377 | 399 | if new_member and new_perm: |
|
378 | 400 | if (new_member, new_perm, new_type) not in perms_new: |
|
379 | 401 | perms_new.append((new_member, new_perm, new_type)) |
|
380 | 402 | elif k.startswith('u_perm_') or k.startswith('g_perm_'): |
|
381 | 403 | member = k[7:] |
|
382 | 404 | t = {'u':'user', |
|
383 | 405 | 'g':'users_group'}[k[0]] |
|
384 | 406 | if member == 'default': |
|
385 | 407 | if value['private']: |
|
386 | 408 | #set none for default when updating to private repo |
|
387 | 409 | v = 'repository.none' |
|
388 | 410 | perms_update.append((member, v, t)) |
|
389 | 411 | |
|
390 | 412 | value['perms_updates'] = perms_update |
|
391 | 413 | value['perms_new'] = perms_new |
|
392 | 414 | |
|
393 | 415 | #update permissions |
|
394 | 416 | for k, v, t in perms_new: |
|
395 | 417 | try: |
|
396 | 418 | if t is 'user': |
|
397 | 419 | self.user_db = User.query()\ |
|
398 | 420 | .filter(User.active == True)\ |
|
399 | 421 | .filter(User.username == k).one() |
|
400 | 422 | if t is 'users_group': |
|
401 | 423 | self.user_db = UsersGroup.query()\ |
|
402 | 424 | .filter(UsersGroup.users_group_active == True)\ |
|
403 | 425 | .filter(UsersGroup.users_group_name == k).one() |
|
404 | 426 | |
|
405 | 427 | except Exception: |
|
406 | 428 | msg = self.message('perm_new_member_name', |
|
407 | 429 | state=State_obj) |
|
408 | 430 | raise formencode.Invalid(msg, value, state, |
|
409 | 431 | error_dict={'perm_new_member_name':msg}) |
|
410 | 432 | return value |
|
411 | 433 | |
|
412 | 434 | class ValidSettings(formencode.validators.FancyValidator): |
|
413 | 435 | |
|
414 | 436 | def to_python(self, value, state): |
|
415 | 437 | #settings form can't edit user |
|
416 | 438 | if value.has_key('user'): |
|
417 | 439 | del['value']['user'] |
|
418 | 440 | |
|
419 | 441 | return value |
|
420 | 442 | |
|
421 | 443 | class ValidPath(formencode.validators.FancyValidator): |
|
422 | 444 | def to_python(self, value, state): |
|
423 | 445 | |
|
424 | 446 | if not os.path.isdir(value): |
|
425 | 447 | msg = _('This is not a valid path') |
|
426 | 448 | raise formencode.Invalid(msg, value, state, |
|
427 | 449 | error_dict={'paths_root_path':msg}) |
|
428 | 450 | return value |
|
429 | 451 | |
|
430 | 452 | def UniqSystemEmail(old_data): |
|
431 | 453 | class _UniqSystemEmail(formencode.validators.FancyValidator): |
|
432 | 454 | def to_python(self, value, state): |
|
433 | 455 | value = value.lower() |
|
434 | 456 | if old_data.get('email') != value: |
|
435 | 457 | user = User.query().filter(User.email == value).scalar() |
|
436 | 458 | if user: |
|
437 | 459 | raise formencode.Invalid( |
|
438 | 460 | _("This e-mail address is already taken"), |
|
439 | 461 | value, state) |
|
440 | 462 | return value |
|
441 | 463 | |
|
442 | 464 | return _UniqSystemEmail |
|
443 | 465 | |
|
444 | 466 | class ValidSystemEmail(formencode.validators.FancyValidator): |
|
445 | 467 | def to_python(self, value, state): |
|
446 | 468 | value = value.lower() |
|
447 | 469 | user = User.query().filter(User.email == value).scalar() |
|
448 | 470 | if user is None: |
|
449 | 471 | raise formencode.Invalid(_("This e-mail address doesn't exist.") , |
|
450 | 472 | value, state) |
|
451 | 473 | |
|
452 | 474 | return value |
|
453 | 475 | |
|
454 | 476 | class LdapLibValidator(formencode.validators.FancyValidator): |
|
455 | 477 | |
|
456 | 478 | def to_python(self, value, state): |
|
457 | 479 | |
|
458 | 480 | try: |
|
459 | 481 | import ldap |
|
460 | 482 | except ImportError: |
|
461 | 483 | raise LdapImportError |
|
462 | 484 | return value |
|
463 | 485 | |
|
464 | 486 | class AttrLoginValidator(formencode.validators.FancyValidator): |
|
465 | 487 | |
|
466 | 488 | def to_python(self, value, state): |
|
467 | 489 | |
|
468 | 490 | if not value or not isinstance(value, (str, unicode)): |
|
469 | 491 | raise formencode.Invalid(_("The LDAP Login attribute of the CN " |
|
470 | 492 | "must be specified - this is the name " |
|
471 | 493 | "of the attribute that is equivalent " |
|
472 | 494 | "to 'username'"), |
|
473 | 495 | value, state) |
|
474 | 496 | |
|
475 | 497 | return value |
|
476 | 498 | |
|
477 | 499 | #=============================================================================== |
|
478 | 500 | # FORMS |
|
479 | 501 | #=============================================================================== |
|
480 | 502 | class LoginForm(formencode.Schema): |
|
481 | 503 | allow_extra_fields = True |
|
482 | 504 | filter_extra_fields = True |
|
483 | 505 | username = UnicodeString( |
|
484 | 506 | strip=True, |
|
485 | 507 | min=1, |
|
486 | 508 | not_empty=True, |
|
487 | 509 | messages={ |
|
488 | 510 | 'empty':_('Please enter a login'), |
|
489 | 511 | 'tooShort':_('Enter a value %(min)i characters long or more')} |
|
490 | 512 | ) |
|
491 | 513 | |
|
492 | 514 | password = UnicodeString( |
|
493 | 515 | strip=True, |
|
494 | 516 | min=3, |
|
495 | 517 | not_empty=True, |
|
496 | 518 | messages={ |
|
497 | 519 | 'empty':_('Please enter a password'), |
|
498 | 520 | 'tooShort':_('Enter %(min)i characters or more')} |
|
499 | 521 | ) |
|
500 | 522 | |
|
501 | 523 | chained_validators = [ValidAuth] |
|
502 | 524 | |
|
503 | 525 | def UserForm(edit=False, old_data={}): |
|
504 | 526 | class _UserForm(formencode.Schema): |
|
505 | 527 | allow_extra_fields = True |
|
506 | 528 | filter_extra_fields = True |
|
507 | 529 | username = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
508 | 530 | ValidUsername(edit, old_data)) |
|
509 | 531 | if edit: |
|
510 | 532 | new_password = All(UnicodeString(strip=True, min=6, not_empty=False)) |
|
511 | 533 | password_confirmation = All(UnicodeString(strip=True, min=6, not_empty=False)) |
|
512 | 534 | admin = StringBoolean(if_missing=False) |
|
513 | 535 | else: |
|
514 | 536 | password = All(UnicodeString(strip=True, min=6, not_empty=True)) |
|
515 | 537 | password_confirmation = All(UnicodeString(strip=True, min=6, not_empty=False)) |
|
516 | 538 | |
|
517 | 539 | active = StringBoolean(if_missing=False) |
|
518 | 540 | name = UnicodeString(strip=True, min=1, not_empty=True) |
|
519 | 541 | lastname = UnicodeString(strip=True, min=1, not_empty=True) |
|
520 | 542 | email = All(Email(not_empty=True), UniqSystemEmail(old_data)) |
|
521 | 543 | |
|
522 | 544 | chained_validators = [ValidPasswordsMatch, ValidPassword] |
|
523 | 545 | |
|
524 | 546 | return _UserForm |
|
525 | 547 | |
|
526 | 548 | |
|
527 | 549 | def UsersGroupForm(edit=False, old_data={}, available_members=[]): |
|
528 | 550 | class _UsersGroupForm(formencode.Schema): |
|
529 | 551 | allow_extra_fields = True |
|
530 | 552 | filter_extra_fields = True |
|
531 | 553 | |
|
532 | 554 | users_group_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
533 | 555 | ValidUsersGroup(edit, old_data)) |
|
534 | 556 | |
|
535 | 557 | users_group_active = StringBoolean(if_missing=False) |
|
536 | 558 | |
|
537 | 559 | if edit: |
|
538 | 560 | users_group_members = OneOf(available_members, hideList=False, |
|
539 | 561 | testValueList=True, |
|
540 | 562 | if_missing=None, not_empty=False) |
|
541 | 563 | |
|
542 | 564 | return _UsersGroupForm |
|
543 | 565 | |
|
544 | 566 | def ReposGroupForm(edit=False, old_data={}, available_groups=[]): |
|
545 | 567 | class _ReposGroupForm(formencode.Schema): |
|
546 | 568 | allow_extra_fields = True |
|
547 | 569 | filter_extra_fields = True |
|
548 | 570 | |
|
549 | 571 | group_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
550 | 572 | SlugifyName()) |
|
551 | 573 | group_description = UnicodeString(strip=True, min=1, |
|
552 | 574 | not_empty=True) |
|
553 | 575 | group_parent_id = OneOf(available_groups, hideList=False, |
|
554 | 576 | testValueList=True, |
|
555 | 577 | if_missing=None, not_empty=False) |
|
556 | 578 | |
|
557 | 579 | chained_validators = [ValidReposGroup(edit, old_data)] |
|
558 | 580 | |
|
559 | 581 | return _ReposGroupForm |
|
560 | 582 | |
|
561 | 583 | def RegisterForm(edit=False, old_data={}): |
|
562 | 584 | class _RegisterForm(formencode.Schema): |
|
563 | 585 | allow_extra_fields = True |
|
564 | 586 | filter_extra_fields = True |
|
565 | 587 | username = All(ValidUsername(edit, old_data), |
|
566 | 588 | UnicodeString(strip=True, min=1, not_empty=True)) |
|
567 | 589 | password = All(UnicodeString(strip=True, min=6, not_empty=True)) |
|
568 | 590 | password_confirmation = All(UnicodeString(strip=True, min=6, not_empty=True)) |
|
569 | 591 | active = StringBoolean(if_missing=False) |
|
570 | 592 | name = UnicodeString(strip=True, min=1, not_empty=True) |
|
571 | 593 | lastname = UnicodeString(strip=True, min=1, not_empty=True) |
|
572 | 594 | email = All(Email(not_empty=True), UniqSystemEmail(old_data)) |
|
573 | 595 | |
|
574 | 596 | chained_validators = [ValidPasswordsMatch, ValidPassword] |
|
575 | 597 | |
|
576 | 598 | return _RegisterForm |
|
577 | 599 | |
|
578 | 600 | def PasswordResetForm(): |
|
579 | 601 | class _PasswordResetForm(formencode.Schema): |
|
580 | 602 | allow_extra_fields = True |
|
581 | 603 | filter_extra_fields = True |
|
582 | 604 | email = All(ValidSystemEmail(), Email(not_empty=True)) |
|
583 | 605 | return _PasswordResetForm |
|
584 | 606 | |
|
585 | 607 | def RepoForm(edit=False, old_data={}, supported_backends=BACKENDS.keys(), |
|
586 | 608 | repo_groups=[]): |
|
587 | 609 | class _RepoForm(formencode.Schema): |
|
588 | 610 | allow_extra_fields = True |
|
589 | 611 | filter_extra_fields = False |
|
590 | 612 | repo_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
591 | 613 | SlugifyName()) |
|
592 | 614 | clone_uri = All(UnicodeString(strip=True, min=1, not_empty=False), |
|
593 | 615 | ValidCloneUri()()) |
|
594 | 616 | repo_group = OneOf(repo_groups, hideList=True) |
|
595 | 617 | repo_type = OneOf(supported_backends) |
|
596 | 618 | description = UnicodeString(strip=True, min=1, not_empty=True) |
|
597 | 619 | private = StringBoolean(if_missing=False) |
|
598 | 620 | enable_statistics = StringBoolean(if_missing=False) |
|
599 | 621 | enable_downloads = StringBoolean(if_missing=False) |
|
600 | 622 | |
|
601 | 623 | if edit: |
|
602 | 624 | #this is repo owner |
|
603 | 625 | user = All(UnicodeString(not_empty=True), ValidRepoUser) |
|
604 | 626 | |
|
605 | 627 | chained_validators = [ValidRepoName(edit, old_data), ValidPerms] |
|
606 | 628 | return _RepoForm |
|
607 | 629 | |
|
608 | 630 | def RepoForkForm(edit=False, old_data={}, supported_backends=BACKENDS.keys()): |
|
609 | 631 | class _RepoForkForm(formencode.Schema): |
|
610 | 632 | allow_extra_fields = True |
|
611 | 633 | filter_extra_fields = False |
|
612 | 634 | fork_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
613 | 635 | SlugifyName()) |
|
614 | 636 | description = UnicodeString(strip=True, min=1, not_empty=True) |
|
615 | 637 | private = StringBoolean(if_missing=False) |
|
616 | 638 | repo_type = All(ValidForkType(old_data), OneOf(supported_backends)) |
|
617 | 639 | |
|
618 | 640 | chained_validators = [ValidForkName()] |
|
619 | 641 | |
|
620 | 642 | return _RepoForkForm |
|
621 | 643 | |
|
622 | 644 | def RepoSettingsForm(edit=False, old_data={}, supported_backends=BACKENDS.keys(), |
|
623 | 645 | repo_groups=[]): |
|
624 | 646 | class _RepoForm(formencode.Schema): |
|
625 | 647 | allow_extra_fields = True |
|
626 | 648 | filter_extra_fields = False |
|
627 | 649 | repo_name = All(UnicodeString(strip=True, min=1, not_empty=True), |
|
628 | 650 | SlugifyName()) |
|
629 | 651 | description = UnicodeString(strip=True, min=1, not_empty=True) |
|
630 | 652 | repo_group = OneOf(repo_groups, hideList=True) |
|
631 | 653 | private = StringBoolean(if_missing=False) |
|
632 | 654 | |
|
633 | 655 | chained_validators = [ValidRepoName(edit, old_data), ValidPerms, |
|
634 | 656 | ValidSettings] |
|
635 | 657 | return _RepoForm |
|
636 | 658 | |
|
637 | 659 | |
|
638 | 660 | def ApplicationSettingsForm(): |
|
639 | 661 | class _ApplicationSettingsForm(formencode.Schema): |
|
640 | 662 | allow_extra_fields = True |
|
641 | 663 | filter_extra_fields = False |
|
642 | 664 | rhodecode_title = UnicodeString(strip=True, min=1, not_empty=True) |
|
643 | 665 | rhodecode_realm = UnicodeString(strip=True, min=1, not_empty=True) |
|
644 | 666 | rhodecode_ga_code = UnicodeString(strip=True, min=1, not_empty=False) |
|
645 | 667 | |
|
646 | 668 | return _ApplicationSettingsForm |
|
647 | 669 | |
|
648 | 670 | def ApplicationUiSettingsForm(): |
|
649 | 671 | class _ApplicationUiSettingsForm(formencode.Schema): |
|
650 | 672 | allow_extra_fields = True |
|
651 | 673 | filter_extra_fields = False |
|
652 | 674 | web_push_ssl = OneOf(['true', 'false'], if_missing='false') |
|
653 | 675 | paths_root_path = All(ValidPath(), UnicodeString(strip=True, min=1, not_empty=True)) |
|
654 | 676 | hooks_changegroup_update = OneOf(['True', 'False'], if_missing=False) |
|
655 | 677 | hooks_changegroup_repo_size = OneOf(['True', 'False'], if_missing=False) |
|
656 | 678 | hooks_pretxnchangegroup_push_logger = OneOf(['True', 'False'], if_missing=False) |
|
657 | 679 | hooks_preoutgoing_pull_logger = OneOf(['True', 'False'], if_missing=False) |
|
658 | 680 | |
|
659 | 681 | return _ApplicationUiSettingsForm |
|
660 | 682 | |
|
661 | 683 | def DefaultPermissionsForm(perms_choices, register_choices, create_choices): |
|
662 | 684 | class _DefaultPermissionsForm(formencode.Schema): |
|
663 | 685 | allow_extra_fields = True |
|
664 | 686 | filter_extra_fields = True |
|
665 | 687 | overwrite_default = StringBoolean(if_missing=False) |
|
666 | 688 | anonymous = OneOf(['True', 'False'], if_missing=False) |
|
667 | 689 | default_perm = OneOf(perms_choices) |
|
668 | 690 | default_register = OneOf(register_choices) |
|
669 | 691 | default_create = OneOf(create_choices) |
|
670 | 692 | |
|
671 | 693 | return _DefaultPermissionsForm |
|
672 | 694 | |
|
673 | 695 | |
|
674 | 696 | def LdapSettingsForm(tls_reqcert_choices, search_scope_choices, tls_kind_choices): |
|
675 | 697 | class _LdapSettingsForm(formencode.Schema): |
|
676 | 698 | allow_extra_fields = True |
|
677 | 699 | filter_extra_fields = True |
|
678 | 700 | pre_validators = [LdapLibValidator] |
|
679 | 701 | ldap_active = StringBoolean(if_missing=False) |
|
680 | 702 | ldap_host = UnicodeString(strip=True,) |
|
681 | 703 | ldap_port = Number(strip=True,) |
|
682 | 704 | ldap_tls_kind = OneOf(tls_kind_choices) |
|
683 | 705 | ldap_tls_reqcert = OneOf(tls_reqcert_choices) |
|
684 | 706 | ldap_dn_user = UnicodeString(strip=True,) |
|
685 | 707 | ldap_dn_pass = UnicodeString(strip=True,) |
|
686 | 708 | ldap_base_dn = UnicodeString(strip=True,) |
|
687 | 709 | ldap_filter = UnicodeString(strip=True,) |
|
688 | 710 | ldap_search_scope = OneOf(search_scope_choices) |
|
689 | 711 | ldap_attr_login = All(AttrLoginValidator, UnicodeString(strip=True,)) |
|
690 | 712 | ldap_attr_firstname = UnicodeString(strip=True,) |
|
691 | 713 | ldap_attr_lastname = UnicodeString(strip=True,) |
|
692 | 714 | ldap_attr_email = UnicodeString(strip=True,) |
|
693 | 715 | |
|
694 | 716 | return _LdapSettingsForm |
General Comments 0
You need to be logged in to leave comments.
Login now