diff --git a/ldapcherry/__init__.py b/ldapcherry/__init__.py index 6f5eb9b..aba45f6 100644 --- a/ldapcherry/__init__.py +++ b/ldapcherry/__init__.py @@ -79,11 +79,34 @@ def exception_decorator(func): if not username: return self.temp_service_unavailable.render() is_admin = self._check_admin() - return self.temp_error.render( - is_admin=is_admin, - alert='danger', - message="An error occured, please check logs for details" - ) + et = type(e) + if et is UserDoesntExist: + user = e.user + return self.temp_error.render( + is_admin=is_admin, + alert='danger', + message="User '" + user + "' does not exist" + ) + elif et is UserAlreadyExists: + user = e.user + return self.temp_error.render( + is_admin=is_admin, + alert='warning', + message="User '" + user + "' already exist" + ) + elif et is GroupDoesntExist: + group = e.group + return self.temp_error.render( + is_admin=is_admin, + alert='danger', + message="Missing group, please check logs for details" + ) + else: + return self.temp_error.render( + is_admin=is_admin, + alert='danger', + message="An error occured, please check logs for details" + ) return ret @@ -535,6 +558,7 @@ class LdapCherry(object): try: tmp = self.backends[b].get_user(username) except UserDoesntExist as e: + self._handle_exception(e) tmp = {} for attr in tmp: if attr in self.attributes.backend_attributes[b]: @@ -1076,7 +1100,7 @@ class LdapCherry(object): return self.temp_error.render( is_admin=is_admin, alert='warning', - message="User doesn't exist" + message="User '" + user + "' does not exist" ) tmp = self._get_roles(user) user_roles = tmp['roles'] diff --git a/ldapcherry/backend/backendLdap.py b/ldapcherry/backend/backendLdap.py index ccedfae..2d7f8bd 100644 --- a/ldapcherry/backend/backendLdap.py +++ b/ldapcherry/backend/backendLdap.py @@ -11,7 +11,9 @@ import ldap.modlist as modlist import ldap.filter import logging import ldapcherry.backend -from ldapcherry.exceptions import UserDoesntExist, GroupDoesntExist +from ldapcherry.exceptions import UserDoesntExist, \ + GroupDoesntExist, \ + UserAlreadyExists import os import re @@ -311,6 +313,8 @@ class Backend(ldapcherry.backend.Backend): ldif = modlist.addModlist(attrs_str) try: ldap_client.add_s(dn, ldif) + except ldap.ALREADY_EXISTS as e: + raise UserAlreadyExists(attrs[self.key], self.backend_name) except Exception as e: ldap_client.unbind_s() self._exception_handler(e) @@ -326,7 +330,7 @@ class Backend(ldapcherry.backend.Backend): ldap_client.delete_s(dn) else: ldap_client.unbind_s() - raise UserDoesntExist(username, self.backend_display_name) + raise UserDoesntExist(username, self.backend_name) ldap_client.unbind_s() def set_attrs(self, username, attrs): @@ -417,7 +421,7 @@ class Backend(ldapcherry.backend.Backend): } ) except ldap.NO_SUCH_OBJECT as e: - raise GroupDoesntExist(group, self.backend_display_name) + raise GroupDoesntExist(group, self.backend_name) except Exception as e: ldap_client.unbind_s() self._exception_handler(e) @@ -487,7 +491,7 @@ class Backend(ldapcherry.backend.Backend): ret = {} tmp = self._get_user(username, ALL_ATTRS) if tmp is None: - raise UserDoesntExist(username, self.backend_display_name) + raise UserDoesntExist(username, self.backend_name) attrs_tmp = tmp[1] for attr in attrs_tmp: value_tmp = attrs_tmp[attr]