From d4235bc33ceb0a61c178e6c157c6e5dcacbd33a8 Mon Sep 17 00:00:00 2001 From: kakwa Date: Sun, 31 Jul 2016 11:39:28 +0200 Subject: [PATCH] better behavior if user doesn't exist in one backend --- ldapcherry/__init__.py | 25 +++++++++++++++++++++-- ldapcherry/backend/backendLdap.py | 2 ++ tests/test_LdapCherry.py | 33 ++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/ldapcherry/__init__.py b/ldapcherry/__init__.py index 66764c4..de56708 100644 --- a/ldapcherry/__init__.py +++ b/ldapcherry/__init__.py @@ -661,8 +661,16 @@ class LdapCherry(object): if b not in badd: badd[b] = {} badd[b][backends[b]] = params['attrs'][attr] + added = False for b in badd: - self.backends[b].add_user(badd[b]) + try: + self.backends[b].add_user(badd[b]) + added = True + except UserAlreadyExists as e: + self._add_notification('User already exists in backend "' + b + '"') + if not added: + raise e + key = self.attributes.get_key() username = params['attrs'][key] @@ -719,7 +727,11 @@ class LdapCherry(object): badd[b] = {} badd[b][backends[b]] = params['attrs'][attr] for b in badd: - self.backends[b].set_attrs(username, badd[b]) + try: + self.backends[b].set_attrs(username, badd[b]) + except UserDoesntExist as e: + self._add_notification('User does not exist in backend "' + b + '"') + return badd def _selfmodify(self, params): @@ -1096,6 +1108,15 @@ class LdapCherry(object): display_names = {} for r in self.roles.flatten: display_names[r] = self.roles.flatten[r]['display_name'] + + if user is None: + cherrypy.response.status = 400 + return self.temp['error.tmpl'].render( + is_admin=is_admin, + alert='warning', + message="No user requested" + ) + user_attrs = self._get_user(user) if user_attrs == {}: cherrypy.response.status = 400 diff --git a/ldapcherry/backend/backendLdap.py b/ldapcherry/backend/backendLdap.py index c85b2ef..703dda1 100644 --- a/ldapcherry/backend/backendLdap.py +++ b/ldapcherry/backend/backendLdap.py @@ -342,6 +342,8 @@ class Backend(ldapcherry.backend.Backend): """ Set user attributes""" ldap_client = self._bind() tmp = self._get_user(self._str(username), ALL_ATTRS) + if tmp is None: + raise UserDoesntExist(username, self.backend_name) dn = self._str(tmp[0]) old_attrs = tmp[1] for attr in attrs: diff --git a/tests/test_LdapCherry.py b/tests/test_LdapCherry.py index 1cd8f8a..9c5667b 100644 --- a/tests/test_LdapCherry.py +++ b/tests/test_LdapCherry.py @@ -288,8 +288,13 @@ class TestError(object): def testNoneType(self): app = LdapCherry() loadconf('./tests/cfg/ldapcherry_test.ini', app) - app.modify('ssmith'), + app.modify('ssmith') + def testNoneModify(self): + app = LdapCherry() + loadconf('./tests/cfg/ldapcherry_test.ini', app) + app.modify(user=None) + @slow_disabled def testNaughtyStrings(self): app = LdapCherry() @@ -317,6 +322,32 @@ class TestError(object): inv.add_user(addefault_user.copy()) app._deleteuser(u'☭default_user') + @travis_disabled + def testAddUserOneBackend(self): + app = LdapCherry() + loadconf('./tests/cfg/ldapcherry_adldap.cfg', app) + inv = ldapcherry.backend.backendAD.Backend(adcfg, cherrypy.log, u'test☭', adattr, 'sAMAccountName') + inv.add_user(addefault_user.copy()) + form = {'groups': {}, 'attrs': {'password1': u'password☭P455', 'password2': u'password☭P455', 'cn': u'Test ☭ Test', 'name': u'Test ☭', 'uidNumber': u'1000', 'gidNumber': u'1000', 'home': u'/home/test', 'first-name': u'Test ☭', 'email': u'test@test.fr', 'uid': u'☭default_user'}, 'roles': {'admin-lv3': u'on', 'admin-lv2': u'on', 'users': u'on'}} + app._adduser(form) + app._deleteuser(u'☭default_user') + + @travis_disabled + def testModifyUserOneBackend(self): + app = LdapCherry() + loadconf('./tests/cfg/ldapcherry_adldap.cfg', app) + inv = ldapcherry.backend.backendAD.Backend(adcfg, cherrypy.log, u'test☭', adattr, 'sAMAccountName') + try: + app._deleteuser(u'☭default_user') + except: + pass + inv.add_user(addefault_user.copy()) + modify_form = { 'attrs': {'first-name': u'Test42 ☭', 'uid': u'☭default_user'}, 'roles': { 'admin-lv3': u'on'}} + app._modify(modify_form) + app._deleteuser(u'☭default_user') + + + def testLogger(self): app = LdapCherry() loadconf('./tests/cfg/ldapcherry.ini', app)