]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
netcmd: model: add a find method to User model to avoid repeating code
authorRob van der Linde <rob@catalyst.net.nz>
Wed, 1 Nov 2023 03:44:18 +0000 (16:44 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 15 Nov 2023 04:05:34 +0000 (04:05 +0000)
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/netcmd/domain/models/user.py

index b00caceb5f063924e23fbd4ea01be8a3a93bc0fb..c18ffc6bf6d8d9a99ae01709912a0a7da671f329 100644 (file)
@@ -20,6 +20,8 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+from ldb import Dn
+
 from samba.dsdb import DS_GUID_USERS_CONTAINER
 
 from .fields import DnField, StringField
@@ -53,3 +55,16 @@ class User(Model):
     @staticmethod
     def get_object_class():
         return "user"
+
+    @classmethod
+    def find(cls, ldb, name):
+        """Helper function to find a user first by Dn then username.
+
+        If the Dn can't be parsed, use sAMAccountName instead.
+        """
+        try:
+            query = {"dn": Dn(ldb, name)}
+        except ValueError:
+            query = {"username": name}
+
+        return cls.get(ldb, **query)