Mercurial > kallithea
comparison rhodecode/model/validators.py @ 3212:6c28533d122c beta
IP restrictions now also enabled for IPv6
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 23 Jan 2013 23:51:57 +0100 |
parents | fc26083c7436 |
children | f5c5095c7028 |
comparison
equal
deleted
inserted
replaced
3211:c77a846a24d5 | 3212:6c28533d122c |
---|---|
12 from formencode.validators import ( | 12 from formencode.validators import ( |
13 UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set, | 13 UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set, |
14 NotEmpty, IPAddress, CIDR | 14 NotEmpty, IPAddress, CIDR |
15 ) | 15 ) |
16 from rhodecode.lib.compat import OrderedSet | 16 from rhodecode.lib.compat import OrderedSet |
17 from rhodecode.lib import ipaddr | |
17 from rhodecode.lib.utils import repo_name_slug | 18 from rhodecode.lib.utils import repo_name_slug |
18 from rhodecode.model.db import RepoGroup, Repository, UsersGroup, User,\ | 19 from rhodecode.model.db import RepoGroup, Repository, UsersGroup, User,\ |
19 ChangesetStatus | 20 ChangesetStatus |
20 from rhodecode.lib.exceptions import LdapImportError | 21 from rhodecode.lib.exceptions import LdapImportError |
21 from rhodecode.config.routing import ADMIN_PREFIX | 22 from rhodecode.config.routing import ADMIN_PREFIX |
709 | 710 |
710 | 711 |
711 def ValidIp(): | 712 def ValidIp(): |
712 class _validator(CIDR): | 713 class _validator(CIDR): |
713 messages = dict( | 714 messages = dict( |
714 badFormat=_('Please enter a valid IP address (a.b.c.d)'), | 715 badFormat=_('Please enter a valid IPv4 or IpV6 address'), |
715 illegalOctets=_('The octets must be within the range of 0-255' | |
716 ' (not %(octet)r)'), | |
717 illegalBits=_('The network size (bits) must be within the range' | 716 illegalBits=_('The network size (bits) must be within the range' |
718 ' of 0-32 (not %(bits)r)')) | 717 ' of 0-32 (not %(bits)r)')) |
719 | 718 |
719 def to_python(self, value, state): | |
720 v = super(_validator, self).to_python(value, state) | |
721 v = v.strip() | |
722 net = ipaddr.IPNetwork(address=v) | |
723 if isinstance(net, ipaddr.IPv4Network): | |
724 #if IPv4 doesn't end with a mask, add /32 | |
725 if '/' not in value: | |
726 v += '/32' | |
727 if isinstance(net, ipaddr.IPv6Network): | |
728 #if IPv6 doesn't end with a mask, add /128 | |
729 if '/' not in value: | |
730 v += '/128' | |
731 return v | |
732 | |
720 def validate_python(self, value, state): | 733 def validate_python(self, value, state): |
721 try: | 734 try: |
722 # Split into octets and bits | 735 addr = value.strip() |
723 if '/' in value: # a.b.c.d/e | 736 #this raises an ValueError if address is not IpV4 or IpV6 |
724 addr, bits = value.split('/') | 737 ipaddr.IPNetwork(address=addr) |
725 else: # a.b.c.d | |
726 addr, bits = value, 32 | |
727 # Use IPAddress validator to validate the IP part | |
728 IPAddress.validate_python(self, addr, state) | |
729 # Bits (netmask) correct? | |
730 if not 0 <= int(bits) <= 32: | |
731 raise formencode.Invalid( | |
732 self.message('illegalBits', state, bits=bits), | |
733 value, state) | |
734 # Splitting faild: wrong syntax | |
735 except ValueError: | 738 except ValueError: |
736 raise formencode.Invalid(self.message('badFormat', state), | 739 raise formencode.Invalid(self.message('badFormat', state), |
737 value, state) | 740 value, state) |
738 | 741 |
739 def to_python(self, value, state): | 742 return _validator |
740 v = super(_validator, self).to_python(value, state) | |
741 #if IP doesn't end with a mask, add /32 | |
742 if '/' not in value: | |
743 v += '/32' | |
744 return v | |
745 return _validator |