##// END OF EJS Templates
validation-schema: added StringBoolean type and IPAddr validator.
marcink -
r1149:94d0095b default
parent child Browse files
Show More
@@ -1,105 +1,137 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2016 RhodeCode GmbH
3 # Copyright (C) 2016-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import colander
21 import colander
22
22
23 from rhodecode.model.db import User, UserGroup
23 from rhodecode.model.db import User, UserGroup
24
24
25
25
26 class GroupNameType(colander.String):
26 class GroupNameType(colander.String):
27 SEPARATOR = '/'
27 SEPARATOR = '/'
28
28
29 def deserialize(self, node, cstruct):
29 def deserialize(self, node, cstruct):
30 result = super(GroupNameType, self).deserialize(node, cstruct)
30 result = super(GroupNameType, self).deserialize(node, cstruct)
31 return self._replace_extra_slashes(result)
31 return self._replace_extra_slashes(result)
32
32
33 def _replace_extra_slashes(self, path):
33 def _replace_extra_slashes(self, path):
34 path = path.split(self.SEPARATOR)
34 path = path.split(self.SEPARATOR)
35 path = [item for item in path if item]
35 path = [item for item in path if item]
36 return self.SEPARATOR.join(path)
36 return self.SEPARATOR.join(path)
37
37
38
38
39 class StringBooleanType(colander.String):
40 true_values = ['true', 't', 'yes', 'y', 'on', '1']
41 false_values = ['false', 'f', 'no', 'n', 'off', '0']
42
43 def serialize(self, node, appstruct):
44 if appstruct is colander.null:
45 return colander.null
46 if not isinstance(appstruct, bool):
47 raise colander.Invalid(node, '%r is not a boolean' % appstruct)
48
49 return appstruct and 'true' or 'false'
50
51 def deserialize(self, node, cstruct):
52 if cstruct is colander.null:
53 return colander.null
54
55 if isinstance(cstruct, bool):
56 return cstruct
57
58 if not isinstance(cstruct, basestring):
59 raise colander.Invalid(node, '%r is not a string' % cstruct)
60
61 value = cstruct.lower()
62 if value in self.true_values:
63 return True
64 elif value in self.false_values:
65 return False
66 else:
67 raise colander.Invalid(
68 node, '{} value cannot be translated to bool'.format(value))
69
70
39 class UserOrUserGroupType(colander.SchemaType):
71 class UserOrUserGroupType(colander.SchemaType):
40 """ colander Schema type for valid rhodecode user and/or usergroup """
72 """ colander Schema type for valid rhodecode user and/or usergroup """
41 scopes = ('user', 'usergroup')
73 scopes = ('user', 'usergroup')
42
74
43 def __init__(self):
75 def __init__(self):
44 self.users = 'user' in self.scopes
76 self.users = 'user' in self.scopes
45 self.usergroups = 'usergroup' in self.scopes
77 self.usergroups = 'usergroup' in self.scopes
46
78
47 def serialize(self, node, appstruct):
79 def serialize(self, node, appstruct):
48 if appstruct is colander.null:
80 if appstruct is colander.null:
49 return colander.null
81 return colander.null
50
82
51 if self.users:
83 if self.users:
52 if isinstance(appstruct, User):
84 if isinstance(appstruct, User):
53 if self.usergroups:
85 if self.usergroups:
54 return 'user:%s' % appstruct.username
86 return 'user:%s' % appstruct.username
55 return appstruct.username
87 return appstruct.username
56
88
57 if self.usergroups:
89 if self.usergroups:
58 if isinstance(appstruct, UserGroup):
90 if isinstance(appstruct, UserGroup):
59 if self.users:
91 if self.users:
60 return 'usergroup:%s' % appstruct.users_group_name
92 return 'usergroup:%s' % appstruct.users_group_name
61 return appstruct.users_group_name
93 return appstruct.users_group_name
62
94
63 raise colander.Invalid(
95 raise colander.Invalid(
64 node, '%s is not a valid %s' % (appstruct, ' or '.join(self.scopes)))
96 node, '%s is not a valid %s' % (appstruct, ' or '.join(self.scopes)))
65
97
66 def deserialize(self, node, cstruct):
98 def deserialize(self, node, cstruct):
67 if cstruct is colander.null:
99 if cstruct is colander.null:
68 return colander.null
100 return colander.null
69
101
70 user, usergroup = None, None
102 user, usergroup = None, None
71 if self.users:
103 if self.users:
72 if cstruct.startswith('user:'):
104 if cstruct.startswith('user:'):
73 user = User.get_by_username(cstruct.split(':')[1])
105 user = User.get_by_username(cstruct.split(':')[1])
74 else:
106 else:
75 user = User.get_by_username(cstruct)
107 user = User.get_by_username(cstruct)
76
108
77 if self.usergroups:
109 if self.usergroups:
78 if cstruct.startswith('usergroup:'):
110 if cstruct.startswith('usergroup:'):
79 usergroup = UserGroup.get_by_group_name(cstruct.split(':')[1])
111 usergroup = UserGroup.get_by_group_name(cstruct.split(':')[1])
80 else:
112 else:
81 usergroup = UserGroup.get_by_group_name(cstruct)
113 usergroup = UserGroup.get_by_group_name(cstruct)
82
114
83 if self.users and self.usergroups:
115 if self.users and self.usergroups:
84 if user and usergroup:
116 if user and usergroup:
85 raise colander.Invalid(node, (
117 raise colander.Invalid(node, (
86 '%s is both a user and usergroup, specify which '
118 '%s is both a user and usergroup, specify which '
87 'one was wanted by prepending user: or usergroup: to the '
119 'one was wanted by prepending user: or usergroup: to the '
88 'name') % cstruct)
120 'name') % cstruct)
89
121
90 if self.users and user:
122 if self.users and user:
91 return user
123 return user
92
124
93 if self.usergroups and usergroup:
125 if self.usergroups and usergroup:
94 return usergroup
126 return usergroup
95
127
96 raise colander.Invalid(
128 raise colander.Invalid(
97 node, '%s is not a valid %s' % (cstruct, ' or '.join(self.scopes)))
129 node, '%s is not a valid %s' % (cstruct, ' or '.join(self.scopes)))
98
130
99
131
100 class UserType(UserOrUserGroupType):
132 class UserType(UserOrUserGroupType):
101 scopes = ('user',)
133 scopes = ('user',)
102
134
103
135
104 class UserGroupType(UserOrUserGroupType):
136 class UserGroupType(UserOrUserGroupType):
105 scopes = ('usergroup',)
137 scopes = ('usergroup',)
@@ -1,25 +1,38 b''
1 import os
1 import os
2 import re
2 import re
3
3
4 import ipaddress
4 import ipaddress
5 import colander
5 import colander
6
6
7 from rhodecode.translation import _
7 from rhodecode.translation import _
8 from rhodecode.lib.utils2 import glob2re
8 from rhodecode.lib.utils2 import glob2re
9
9
10
10
11 def ip_addr_validator(node, value):
11 def ip_addr_validator(node, value):
12 try:
12 try:
13 # this raises an ValueError if address is not IpV4 or IpV6
13 # this raises an ValueError if address is not IpV4 or IpV6
14 ipaddress.ip_network(value, strict=False)
14 ipaddress.ip_network(value, strict=False)
15 except ValueError:
15 except ValueError:
16 msg = _(u'Please enter a valid IPv4 or IpV6 address')
16 msg = _(u'Please enter a valid IPv4 or IpV6 address')
17 raise colander.Invalid(node, msg)
17 raise colander.Invalid(node, msg)
18
18
19
19
20 class IpAddrValidator(object):
21 def __init__(self, strict=True):
22 self.strict = strict
23
24 def __call__(self, node, value):
25 try:
26 # this raises an ValueError if address is not IpV4 or IpV6
27 ipaddress.ip_network(value, strict=self.strict)
28 except ValueError:
29 msg = _(u'Please enter a valid IPv4 or IpV6 address')
30 raise colander.Invalid(node, msg)
31
32
20 def glob_validator(node, value):
33 def glob_validator(node, value):
21 try:
34 try:
22 re.compile('^' + glob2re(value) + '$')
35 re.compile('^' + glob2re(value) + '$')
23 except Exception:
36 except Exception:
24 msg = _(u'Invalid glob pattern')
37 msg = _(u'Invalid glob pattern')
25 raise colander.Invalid(node, msg)
38 raise colander.Invalid(node, msg)
General Comments 0
You need to be logged in to leave comments. Login now