From: Rob van der Linde Date: Wed, 6 Mar 2024 03:47:29 +0000 (+1300) Subject: netcmd: models: improve Computer constructor adding "$" handling X-Git-Tag: tdb-1.4.11~1435 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=200948c172d20de75a3598d244de3f47d91d7bc0;p=thirdparty%2Fsamba.git netcmd: models: improve Computer constructor adding "$" handling In some cases the previous code would end up creating computers where the account name ended on double "$" Rewrote constructor to handle more cases, for example only an account name is provided, only a name is provided, or both. Signed-off-by: Rob van der Linde Reviewed-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- diff --git a/python/samba/netcmd/domain/models/computer.py b/python/samba/netcmd/domain/models/computer.py index c9e034a530f..84dddb16a9b 100644 --- a/python/samba/netcmd/domain/models/computer.py +++ b/python/samba/netcmd/domain/models/computer.py @@ -33,18 +33,39 @@ class Computer(User): def __init__(self, **kwargs): """Computer constructor automatically adds "$" to account_name. - Also applies to GroupManagedServiceAccount subclass. - """ - name = kwargs.get("name", kwargs.get("cn")) + The various ways a Computer can be constructed: + + >>> Computer(name="pc") + >>> Computer(account_name="pc$") + >>> Computer(cn="pc") + >>> Computer(account_name="pc$", name="pc") + + In each case the constructor does its best to ensure the + account name ends with a "$" and the name doesn't. + + Also applies to GroupManagedServiceAccount subclass.""" + name = kwargs.get("name", kwargs.pop("cn", None)) account_name = kwargs.get("account_name") - # If account_name is missing, use name or cn and add a "$". - # If account_name is present but lacking "$", add it automatically. + # First make sure the account_name always has a "$". + if account_name and not account_name.endswith("$"): + account_name += "$" + + # The name is present but not account name. + # If the name already has a "$" don't add two. if name and not account_name: - kwargs["account_name"] = name + "$" - elif account_name and not account_name.endswith("$"): - kwargs["account_name"] = account_name + "$" + if name.endswith("$"): + account_name = name + else: + account_name = name + "$" + + # The account name is present but not the name. + # Use the account name, stripping the "$" character. + elif account_name and not name: + name = account_name.rstrip("$") + kwargs["name"] = name + kwargs["account_name"] = account_name super().__init__(**kwargs) @staticmethod