Browse Source

HUE-8585 [useradmin] Bubbling up errors for Add Sync Ldap Users

Ying Chen 6 years ago
parent
commit
d5d479c6da

+ 3 - 0
apps/useradmin/src/useradmin/templates/add_ldap_users.mako

@@ -80,6 +80,9 @@ ${ layout.menubar(section='users') }
           huePubSub.publish('open.link', '${ url('useradmin.views.list_users') }');
           huePubSub.publish('open.link', '${ url('useradmin.views.list_users') }');
           $.jHueNotify.info("${ _('User added/synced correctly') }");
           $.jHueNotify.info("${ _('User added/synced correctly') }");
         }
         }
+      },
+      error: function (data) {
+        $.jHueNotify.error(data.responseJSON['message']);
       }
       }
     });
     });
     %endif
     %endif

+ 3 - 0
apps/useradmin/src/useradmin/templates/list_users.mako

@@ -250,6 +250,9 @@ ${layout.menubar(section='users')}
             },
             },
             error: function(data) {
             error: function(data) {
               $usersComponents.find('input[type="submit"]').removeAttr("disabled");
               $usersComponents.find('input[type="submit"]').removeAttr("disabled");
+              huePubSub.publish('open.link', data.url);
+              $.jHueNotify.error(data.responseJSON['message']);
+              $usersComponents.find(".sync-ldap").modal("hide");
             }
             }
           });
           });
           % endif
           % endif

+ 12 - 7
apps/useradmin/src/useradmin/views.py

@@ -558,10 +558,11 @@ def add_ldap_users(request):
         connection = ldap_access.get_connection_from_server(server)
         connection = ldap_access.get_connection_from_server(server)
         users = import_ldap_users(connection, username_pattern, False, import_by_dn, failed_users=failed_ldap_users)
         users = import_ldap_users(connection, username_pattern, False, import_by_dn, failed_users=failed_ldap_users)
       except (ldap.LDAPError, LdapBindException), e:
       except (ldap.LDAPError, LdapBindException), e:
-        LOG.error("LDAP Exception: %s" % e)
-        raise PopupException(_('There was an error when communicating with LDAP'), detail=str(e))
+        LOG.error("LDAP Exception: %s" % smart_str(e))
+        raise PopupException(smart_str(_('There was an error when communicating with LDAP: %s')) % str(e))
       except ValidationError, e:
       except ValidationError, e:
-        raise PopupException(_('There was a problem with some of the LDAP information'), detail=str(e))
+        LOG.error("LDAP Exception: %s" % smart_str(e))
+        raise PopupException(smart_str(_('There was a problem with some of the LDAP information: %s')) % str(e))
 
 
       if users and form.cleaned_data['ensure_home_directory']:
       if users and form.cleaned_data['ensure_home_directory']:
         for user in users:
         for user in users:
@@ -702,7 +703,11 @@ def sync_ldap_users_groups(request):
     if form.is_valid():
     if form.is_valid():
       is_ensuring_home_directory = form.cleaned_data['ensure_home_directory']
       is_ensuring_home_directory = form.cleaned_data['ensure_home_directory']
       server = form.cleaned_data.get('server')
       server = form.cleaned_data.get('server')
-      connection = ldap_access.get_connection_from_server(server)
+      try:
+        connection = ldap_access.get_connection_from_server(server)
+      except (ldap.LDAPError, LdapBindException), e:
+        LOG.error("LDAP Exception: %s" % smart_str(e))
+        raise PopupException(smart_str(_('There was an error when communicating with LDAP: %s')) % str(e))
 
 
       failed_ldap_users = []
       failed_ldap_users = []
 
 
@@ -737,8 +742,8 @@ def sync_ldap_users_and_groups(connection, is_ensuring_home_directory=False, fs=
     users = sync_ldap_users(connection, failed_users=failed_users)
     users = sync_ldap_users(connection, failed_users=failed_users)
     groups = sync_ldap_groups(connection, failed_users=failed_users)
     groups = sync_ldap_groups(connection, failed_users=failed_users)
   except (ldap.LDAPError, LdapBindException), e:
   except (ldap.LDAPError, LdapBindException), e:
-    LOG.error("LDAP Exception: %s" % e)
-    raise PopupException(_('There was an error when communicating with LDAP'), detail=str(e))
+    LOG.error("LDAP Exception: %s" % smart_str(e))
+    raise PopupException(smart_str(_('There was an error when communicating with LDAP: %s')) % str(e))
 
 
   # Create home dirs for every user sync'd
   # Create home dirs for every user sync'd
   if is_ensuring_home_directory:
   if is_ensuring_home_directory:
@@ -773,7 +778,7 @@ def sync_ldap_users(connection, failed_users=None):
   """
   """
   users = User.objects.filter(userprofile__creation_method=UserProfile.CreationMethod.EXTERNAL.name).all()
   users = User.objects.filter(userprofile__creation_method=UserProfile.CreationMethod.EXTERNAL.name).all()
   for user in users:
   for user in users:
-    _import_ldap_users(connection, user.username, failed_users=failed_users)
+    _import_ldap_users(connection, smart_str(user.username), failed_users=failed_users)
   return users
   return users
 
 
 
 

+ 3 - 1
desktop/core/src/desktop/auth/forms.py

@@ -24,6 +24,7 @@ from django.contrib.auth.models import User
 from django.contrib.auth.forms import AuthenticationForm as AuthAuthenticationForm, UserCreationForm as AuthUserCreationForm
 from django.contrib.auth.forms import AuthenticationForm as AuthAuthenticationForm, UserCreationForm as AuthUserCreationForm
 from django.forms import CharField, TextInput, PasswordInput, ChoiceField, ValidationError
 from django.forms import CharField, TextInput, PasswordInput, ChoiceField, ValidationError
 from django.utils.safestring import mark_safe
 from django.utils.safestring import mark_safe
+from django.utils.encoding import smart_str
 from django.utils.translation import ugettext_lazy as _t, ugettext as _
 from django.utils.translation import ugettext_lazy as _t, ugettext as _
 
 
 from desktop import conf
 from desktop import conf
@@ -157,7 +158,8 @@ class LdapAuthenticationForm(AuthenticationForm):
           else:
           else:
             get_ldap_connection(conf.LDAP)
             get_ldap_connection(conf.LDAP)
         except Exception as e:
         except Exception as e:
-          raise ValidationError(_("LDAP server %s Error: %s" % (server_key, str(e))))
+          LOG.error("LDAP Exception: %s" % smart_str(e))
+          raise ValidationError(_("LDAP server %s Error: %s").encode('utf-8') % (server_key, str(e)))
 
 
         raise ValidationError(
         raise ValidationError(
           self.error_messages['invalid_login'])
           self.error_messages['invalid_login'])