bug 12292: stop user.py throwing errors if user is unknown
authorRowland Penny <rpenny@samba.org>
Wed, 28 Sep 2016 14:39:52 +0000 (15:39 +0100)
committerAlexander Bokovoy <ab@samba.org>
Fri, 30 Sep 2016 00:11:21 +0000 (02:11 +0200)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12292

Signed-off-by: Rowland Penny <rpenny@samba.org>
Reviewed-by: Alexander Bokovoy <ab@samba.org>
python/samba/netcmd/user.py

index 5adc2879bccf077800c223db216e5defffde1579..43581703bfd27cc002120319f2ba9b6b4a726517 100644 (file)
@@ -406,10 +406,23 @@ Example2 shows how to delete a user in the domain against the local server.   su
         lp = sambaopts.get_loadparm()
         creds = credopts.get_credentials(lp, fallback_machine=True)
 
+        samdb = SamDB(url=H, session_info=system_session(),
+                      credentials=creds, lp=lp)
+
+        filter = ("(&(sAMAccountName=%s)(sAMAccountType=805306368))" %
+                   username)
+
         try:
-            samdb = SamDB(url=H, session_info=system_session(),
-                          credentials=creds, lp=lp)
-            samdb.deleteuser(username)
+            res = samdb.search(base=samdb.domain_dn(),
+                               scope=ldb.SCOPE_SUBTREE,
+                               expression=filter,
+                               attrs=["dn"])
+            user_dn = res[0].dn
+        except IndexError:
+            raise CommandError('Unable to find user "%s"' % (username))
+
+        try:
+            samdb.delete(user_dn)
         except Exception, e:
             raise CommandError('Failed to remove user "%s"' % username, e)
         self.outf.write("Deleted user %s\n" % username)