1
0
mirror of https://github.com/kakwa/ldapcherry synced 2024-11-26 11:14:31 +01:00

better exception handling if user or group doesn't exist

This commit is contained in:
kakwa 2015-07-15 21:05:38 +02:00
parent 77fbb346f1
commit 3a6da2e480
2 changed files with 25 additions and 5 deletions

View File

@ -522,7 +522,10 @@ class LdapCherry(object):
return {} return {}
ret = {} ret = {}
for b in self.backends: for b in self.backends:
try:
tmp = self.backends[b].get_user(username) tmp = self.backends[b].get_user(username)
except UserDoesntExist as e:
break
for attr in tmp: for attr in tmp:
if attr in self.attributes.backend_attributes[b]: if attr in self.attributes.backend_attributes[b]:
attrid = self.attributes.backend_attributes[b][attr] attrid = self.attributes.backend_attributes[b][attr]
@ -1053,6 +1056,12 @@ class LdapCherry(object):
for r in self.roles.flatten: for r in self.roles.flatten:
display_names[r] = self.roles.flatten[r]['display_name'] display_names[r] = self.roles.flatten[r]['display_name']
user_attrs = self._get_user(user) 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) tmp = self._get_roles(user)
user_roles = tmp['roles'] user_roles = tmp['roles']
user_lonely_groups = tmp['unusedgroups'] user_lonely_groups = tmp['unusedgroups']
@ -1098,6 +1107,12 @@ class LdapCherry(object):
params = self._parse_params(params) params = self._parse_params(params)
self._selfmodify(params) self._selfmodify(params)
user_attrs = self._get_user(user) 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( form = self.temp_form.render(
attributes=self.attributes.get_selfattributes(), attributes=self.attributes.get_selfattributes(),
values=user_attrs, values=user_attrs,

View File

@ -10,6 +10,7 @@ import ldap
import ldap.modlist as modlist import ldap.modlist as modlist
import logging import logging
import ldapcherry.backend import ldapcherry.backend
from ldapcherry.exceptions import UserDoesntExist, GroupDoesntExist
import os import os
import re import re
@ -97,8 +98,7 @@ class Backend(ldapcherry.backend.Backend):
elif et is ldap.NO_SUCH_OBJECT: elif et is ldap.NO_SUCH_OBJECT:
self._logger( self._logger(
severity=logging.ERROR, severity=logging.ERROR,
msg="Search DN '" + basedn + msg="DN doesn't exist, check '" +
"' doesn't exist, check '" +
self.backend_name + self.backend_name +
".userdn'or '" + ".userdn'or '" +
self.backend_name + self.backend_name +
@ -338,6 +338,8 @@ class Backend(ldapcherry.backend.Backend):
'backend': self.backend_name 'backend': self.backend_name
} }
) )
except ldap.NO_SUCH_OBJECT as e:
raise GroupDoesntExist(group, self.backend_name)
except Exception as e: except Exception as e:
ldap_client.unbind_s() ldap_client.unbind_s()
self._exception_handler(e) self._exception_handler(e)
@ -395,7 +397,10 @@ class Backend(ldapcherry.backend.Backend):
def get_user(self, username): def get_user(self, username):
ret = {} 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: for attr in attrs_tmp:
value_tmp = attrs_tmp[attr] value_tmp = attrs_tmp[attr]
if len(value_tmp) == 1: if len(value_tmp) == 1: