From a22a80ed6e8494b3edb717c3b288c7adde5092ee Mon Sep 17 00:00:00 2001 From: Jule Anger Date: Mon, 24 Aug 2020 16:34:35 +0200 Subject: [PATCH] samdb: add prepare_attr_replace() method MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- python/samba/samdb.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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, -- 2.47.3