]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
samdb: add prepare_attr_replace() method
authorJule Anger <ja@sernet.de>
Mon, 24 Aug 2020 14:34:35 +0000 (16:34 +0200)
committerDouglas Bagnall <dbagnall@samba.org>
Thu, 1 Oct 2020 01:18:39 +0000 (01:18 +0000)
Add a method to prepare a given Message to replace the given attribute.
If the given new value is None or the old value and the new value are
the same, do nothing.
If the new value is empty, prepare to replace the given attribute with
[].
Else prepare to replace the given attribute with the new value.

Use this for samdb.modify(msg).

Signed-off-by: Jule Anger <ja@sernet.de>
Reviewed-by: Björn Baumbach <bb@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/samba/samdb.py

index 6f5404e7f0ac5242599f0dfa7579bb70ff4a7354..75a0af6f23062d81c446b6c2a57f7bb0c0311c53 100644 (file)
@@ -392,6 +392,29 @@ member: %s
         else:
             self.transaction_commit()
 
+    def prepare_attr_replace(self, msg, old, attr_name, value):
+        """Changes the MessageElement with the given attr_name of the
+        given Message. If the value is "" set an empty value and the flag
+        FLAG_MOD_DELETE, otherwise set the new value and FLAG_MOD_REPLACE.
+        If the value is None or the Message contains the attr_name with this
+        value, nothing will changed."""
+        # skip unchanged attribute
+        if value is None:
+            return
+        if attr_name in old and str(value) == str(old[attr_name]):
+            return
+
+        # remove attribute
+        if len(value) == 0:
+            if attr_name in old:
+                el = ldb.MessageElement([], ldb.FLAG_MOD_DELETE, attr_name)
+                msg.add(el)
+            return
+
+        # change attribute
+        el = ldb.MessageElement(value, ldb.FLAG_MOD_REPLACE, attr_name)
+        msg.add(el)
+
     def newuser(self, username, password,
                 force_password_change_at_next_login_req=False,
                 useusernameascn=False, userou=None, surname=None, givenname=None,