From 709ece0430cd7441ac1fa20ed8d51882bae6423c Mon Sep 17 00:00:00 2001 From: Robert Niederreiter Date: Wed, 13 Dec 2017 12:20:59 +0100 Subject: [PATCH] Add ``exists`` property to ``LDAPStorage``. --- CHANGES.rst | 3 +++ LICENSE.rst | 2 +- README.rst | 7 +++++++ src/node/ext/ldap/_node.py | 16 ++++++++++++++++ src/node/ext/ldap/_node.rst | 9 +++++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index efd8a36..2ab84bb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,9 @@ History 1.0b7 (unreleased) ------------------ +- Add ``exists`` property to ``LDAPStorage``. + [rnix] + - Add ``objectSid`` and ``objectGUID`` from Active Directory schema to ``properties.BINARY_DEFAULTS``. [rnix] diff --git a/LICENSE.rst b/LICENSE.rst index dd8c5d9..12a1bc3 100644 --- a/LICENSE.rst +++ b/LICENSE.rst @@ -1,7 +1,7 @@ License ======= -Copyright (c) 2006-2016, BlueDynamics Alliance, Austria, Germany, Switzerland +Copyright (c) 2006-2017, BlueDynamics Alliance, Austria, Germany, Switzerland All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/README.rst b/README.rst index 33d0f3f..ab37243 100644 --- a/README.rst +++ b/README.rst @@ -306,6 +306,13 @@ Every LDAP node has a DN and a RDN: >>> root.rdn_attr u'ou' +Check whether created node exists in the database:: + +.. code-block:: pycon + + >>> root.exists + True + Directory entry has no children yet: .. code-block:: pycon diff --git a/src/node/ext/ldap/_node.py b/src/node/ext/ldap/_node.py index 0b1c194..64c77ee 100644 --- a/src/node/ext/ldap/_node.py +++ b/src/node/ext/ldap/_node.py @@ -433,6 +433,22 @@ def child_dn(self, key): return u','.join([decode(key), decode(self._dn)]) return u','.join([decode(key), decode(self.name)]) + @default + @property + def exists(self): + try: + res = self.ldap_session.search( + scope=BASE, + baseDN=self.DN.encode('utf-8'), + attrlist=[''] + ) + # this probably never happens + if len(res) != 1: + raise RuntimeError() + return True + except NO_SUCH_OBJECT: + return False + @default def node_by_dn(self, dn, strict=False): """Return node from tree by DN. diff --git a/src/node/ext/ldap/_node.rst b/src/node/ext/ldap/_node.rst index 96cdcf6..0bb4744 100644 --- a/src/node/ext/ldap/_node.rst +++ b/src/node/ext/ldap/_node.rst @@ -59,6 +59,15 @@ The non-unicode name got decoded:: >>> root.rdn_attr u'dc' +Check exists:: + + >>> root.exists + True + + >>> inexistent = LDAPNode('dc=other-domain,dc=com', props) + >>> inexistent.exists + False + LDAP attributes for DN are stored on ``attrs``:: >>> root.attrs