From: Jule Anger Date: Mon, 24 Aug 2020 14:34:35 +0000 (+0200) Subject: samdb: add prepare_attr_replace() method X-Git-Tag: talloc-2.3.2~369 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a22a80ed6e8494b3edb717c3b288c7adde5092ee;p=thirdparty%2Fsamba.git samdb: add prepare_attr_replace() method 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 Reviewed-by: Björn Baumbach Reviewed-by: Douglas Bagnall --- diff --git a/python/samba/samdb.py b/python/samba/samdb.py index 6f5404e7f0a..75a0af6f230 100644 --- a/python/samba/samdb.py +++ b/python/samba/samdb.py @@ -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,