From 3a6da2e48094effdb19a9272f0a0e7284707dc19 Mon Sep 17 00:00:00 2001 From: kakwa Date: Wed, 15 Jul 2015 21:05:38 +0200 Subject: [PATCH] better exception handling if user or group doesn't exist --- ldapcherry/__init__.py | 17 ++++++++++++++++- ldapcherry/backend/backendLdap.py | 13 +++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ldapcherry/__init__.py b/ldapcherry/__init__.py index 4fea3ce..4935d76 100644 --- a/ldapcherry/__init__.py +++ b/ldapcherry/__init__.py @@ -522,7 +522,10 @@ class LdapCherry(object): return {} ret = {} for b in self.backends: - tmp = self.backends[b].get_user(username) + try: + tmp = self.backends[b].get_user(username) + except UserDoesntExist as e: + break for attr in tmp: if attr in self.attributes.backend_attributes[b]: attrid = self.attributes.backend_attributes[b][attr] @@ -1053,6 +1056,12 @@ class LdapCherry(object): for r in self.roles.flatten: display_names[r] = self.roles.flatten[r]['display_name'] user_attrs = self._get_user(user) + if user_attrs == {}: + return self.temp_error.render( + is_admin=is_admin, + alert='warning', + message="User doesn't exist" + ) tmp = self._get_roles(user) user_roles = tmp['roles'] user_lonely_groups = tmp['unusedgroups'] @@ -1098,6 +1107,12 @@ class LdapCherry(object): params = self._parse_params(params) self._selfmodify(params) user_attrs = self._get_user(user) + if user_attrs == {}: + return self.temp_error.render( + is_admin=is_admin, + alert='warning', + message="User doesn't exist" + ) form = self.temp_form.render( attributes=self.attributes.get_selfattributes(), values=user_attrs, diff --git a/ldapcherry/backend/backendLdap.py b/ldapcherry/backend/backendLdap.py index 10bba9d..94db2e5 100644 --- a/ldapcherry/backend/backendLdap.py +++ b/ldapcherry/backend/backendLdap.py @@ -10,6 +10,7 @@ import ldap import ldap.modlist as modlist import logging import ldapcherry.backend +from ldapcherry.exceptions import UserDoesntExist, GroupDoesntExist import os import re @@ -97,10 +98,9 @@ class Backend(ldapcherry.backend.Backend): elif et is ldap.NO_SUCH_OBJECT: self._logger( severity=logging.ERROR, - msg="Search DN '" + basedn + - "' doesn't exist, check '" + + msg="DN doesn't exist, check '" + self.backend_name + - ".userdn' or '" + + ".userdn'or '" + self.backend_name + ".groupdn'", ) @@ -338,6 +338,8 @@ class Backend(ldapcherry.backend.Backend): 'backend': self.backend_name } ) + except ldap.NO_SUCH_OBJECT as e: + raise GroupDoesntExist(group, self.backend_name) except Exception as e: ldap_client.unbind_s() self._exception_handler(e) @@ -395,7 +397,10 @@ class Backend(ldapcherry.backend.Backend): def get_user(self, username): ret = {} - attrs_tmp = self._get_user(username, ALL_ATTRS)[1] + tmp = self._get_user(username, ALL_ATTRS) + if tmp is None: + raise UserDoesntExist(username, self.backend_name) + attrs_tmp = tmp[1] for attr in attrs_tmp: value_tmp = attrs_tmp[attr] if len(value_tmp) == 1: