changeset 1145:9a9946320435

fixed ldap form, it doesn't validate the baseDN until enable checkbox is set
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 17 Mar 2011 21:05:14 +0100
parents cf5761b37af9
children cb77867d69d3
files rhodecode/controllers/admin/ldap_settings.py rhodecode/model/forms.py
diffstat 2 files changed, 24 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/controllers/admin/ldap_settings.py	Thu Mar 17 20:41:16 2011 +0100
+++ b/rhodecode/controllers/admin/ldap_settings.py	Thu Mar 17 21:05:14 2011 +0100
@@ -68,10 +68,11 @@
         """POST ldap create and store ldap settings"""
 
         settings_model = SettingsModel()
-        _form = LdapSettingsForm()()
+        post_data = dict(request.POST)
+        _form = LdapSettingsForm(post_data.get('ldap_active'))()
 
         try:
-            form_result = _form.to_python(dict(request.POST))
+            form_result = _form.to_python(post_data)
             try:
 
                 for k, v in form_result.items():
--- a/rhodecode/model/forms.py	Thu Mar 17 20:41:16 2011 +0100
+++ b/rhodecode/model/forms.py	Thu Mar 17 21:05:14 2011 +0100
@@ -300,26 +300,29 @@
             raise LdapImportError
         return value
 
-class BaseDnValidator(formencode.validators.FancyValidator):
+def BaseDnValidator(ldap_enable):
+    class _BaseDnValidator(formencode.validators.FancyValidator):
+
+        def to_python(self, value, state):
 
-    def to_python(self, value, state):
-
-        try:
-            value % {'user':'valid'}
+            if not ldap_enable:
+                return ''
+            try:
+                value % {'user':'valid'}
 
-            if value.find('%(user)s') == -1:
-                raise formencode.Invalid(_("You need to specify %(user)s in "
-                                           "template for example uid=%(user)s "
-                                           ",dc=company...") ,
-                                         value, state)
+                if value.find('%(user)s') == -1:
+                    raise formencode.Invalid(_("You need to specify %(user)s in "
+                                               "template for example uid=%(user)s "
+                                               ",dc=company...") ,
+                                             value, state)
 
-        except KeyError:
-            raise formencode.Invalid(_("Wrong template used, only %(user)s "
-                                       "is an valid entry") ,
-                                         value, state)
+            except KeyError:
+                raise formencode.Invalid(_("Wrong template used, only %(user)s "
+                                           "is an valid entry") ,
+                                             value, state)
 
-        return value
-
+            return value
+    return _BaseDnValidator
 #===============================================================================
 # FORMS        
 #===============================================================================
@@ -467,7 +470,7 @@
     return _DefaultPermissionsForm
 
 
-def LdapSettingsForm():
+def LdapSettingsForm(ldap_enable):
     class _LdapSettingsForm(formencode.Schema):
         allow_extra_fields = True
         filter_extra_fields = True
@@ -478,6 +481,6 @@
         ldap_ldaps = StringBoolean(if_missing=False)
         ldap_dn_user = UnicodeString(strip=True,)
         ldap_dn_pass = UnicodeString(strip=True,)
-        ldap_base_dn = All(BaseDnValidator, UnicodeString(strip=True,))
+        ldap_base_dn = All(BaseDnValidator(ldap_enable), UnicodeString(strip=True,))
 
     return _LdapSettingsForm