1
0
mirror of https://github.com/kakwa/ldapcherry synced 2024-11-25 18:54:29 +01:00

custom error handling for UserDoesntExist, UserAlreadyExists and GroupDoesntExist

This commit is contained in:
kakwa 2015-07-31 22:43:51 +02:00
parent 5561a9f42b
commit a8af29692e
2 changed files with 38 additions and 10 deletions

View File

@ -79,6 +79,29 @@ def exception_decorator(func):
if not username:
return self.temp_service_unavailable.render()
is_admin = self._check_admin()
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',
@ -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']

View File

@ -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]