1
0
mirror of https://github.com/kakwa/ldapcherry synced 2024-11-22 09:24:21 +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,11 +79,34 @@ def exception_decorator(func):
if not username: if not username:
return self.temp_service_unavailable.render() return self.temp_service_unavailable.render()
is_admin = self._check_admin() is_admin = self._check_admin()
return self.temp_error.render( et = type(e)
is_admin=is_admin, if et is UserDoesntExist:
alert='danger', user = e.user
message="An error occured, please check logs for details" 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 return ret
@ -535,6 +558,7 @@ class LdapCherry(object):
try: try:
tmp = self.backends[b].get_user(username) tmp = self.backends[b].get_user(username)
except UserDoesntExist as e: except UserDoesntExist as e:
self._handle_exception(e)
tmp = {} tmp = {}
for attr in tmp: for attr in tmp:
if attr in self.attributes.backend_attributes[b]: if attr in self.attributes.backend_attributes[b]:
@ -1076,7 +1100,7 @@ class LdapCherry(object):
return self.temp_error.render( return self.temp_error.render(
is_admin=is_admin, is_admin=is_admin,
alert='warning', alert='warning',
message="User doesn't exist" message="User '" + user + "' does not exist"
) )
tmp = self._get_roles(user) tmp = self._get_roles(user)
user_roles = tmp['roles'] user_roles = tmp['roles']

View File

@ -11,7 +11,9 @@ import ldap.modlist as modlist
import ldap.filter import ldap.filter
import logging import logging
import ldapcherry.backend import ldapcherry.backend
from ldapcherry.exceptions import UserDoesntExist, GroupDoesntExist from ldapcherry.exceptions import UserDoesntExist, \
GroupDoesntExist, \
UserAlreadyExists
import os import os
import re import re
@ -311,6 +313,8 @@ class Backend(ldapcherry.backend.Backend):
ldif = modlist.addModlist(attrs_str) ldif = modlist.addModlist(attrs_str)
try: try:
ldap_client.add_s(dn, ldif) ldap_client.add_s(dn, ldif)
except ldap.ALREADY_EXISTS as e:
raise UserAlreadyExists(attrs[self.key], 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)
@ -326,7 +330,7 @@ class Backend(ldapcherry.backend.Backend):
ldap_client.delete_s(dn) ldap_client.delete_s(dn)
else: else:
ldap_client.unbind_s() ldap_client.unbind_s()
raise UserDoesntExist(username, self.backend_display_name) raise UserDoesntExist(username, self.backend_name)
ldap_client.unbind_s() ldap_client.unbind_s()
def set_attrs(self, username, attrs): def set_attrs(self, username, attrs):
@ -417,7 +421,7 @@ class Backend(ldapcherry.backend.Backend):
} }
) )
except ldap.NO_SUCH_OBJECT as e: except ldap.NO_SUCH_OBJECT as e:
raise GroupDoesntExist(group, self.backend_display_name) 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)
@ -487,7 +491,7 @@ class Backend(ldapcherry.backend.Backend):
ret = {} ret = {}
tmp = self._get_user(username, ALL_ATTRS) tmp = self._get_user(username, ALL_ATTRS)
if tmp is None: if tmp is None:
raise UserDoesntExist(username, self.backend_display_name) raise UserDoesntExist(username, self.backend_name)
attrs_tmp = tmp[1] attrs_tmp = tmp[1]
for attr in attrs_tmp: for attr in attrs_tmp:
value_tmp = attrs_tmp[attr] value_tmp = attrs_tmp[attr]