| 1 |
from functools import partial |
| 2 |
|
| 3 |
from tornado import gen |
| 4 |
from tornado.ioloop import IOLoop |
| 5 |
from tornado.concurrent import Future |
| 6 |
|
| 7 |
from ..ldapconnection import BaseLDAPConnection, LDAPSearchScope |
| 8 |
from ..errors import LDAPError, NotAllowedOnNonleaf |
| 9 |
|
| 10 |
|
| 11 |
class TornadoLDAPConnection(BaseLDAPConnection): |
| 12 |
""" |
| 13 |
Asynchronous LDAP connection object that works with Torando. |
| 14 |
It has the same methods and properties as :class:`bonsai.LDAPConnection`. |
| 15 |
|
| 16 |
:param LDAPClient client: a client object. |
| 17 |
:param ioloop: a Tornado IO loop. |
| 18 |
""" |
| 19 |
|
| 20 |
def __init__(self, client, ioloop=None): |
| 21 |
super().__init__(client, is_async=True) |
| 22 |
self._ioloop = ioloop or IOLoop.instance() |
| 23 |
self._fileno = None |
| 24 |
self._timeout = None |
| 25 |
|
| 26 |
def _io_callback(self, fut, msg_id, fd=None, events=None): |
| 27 |
try: |
| 28 |
self._ioloop.remove_handler(self._fileno) |
| 29 |
res = super().get_result(msg_id) |
| 30 |
if res is not None: |
| 31 |
fut.set_result(res) |
| 32 |
if self._timeout is not None: |
| 33 |
self._ioloop.remove_timeout(self._timeout) |
| 34 |
else: |
| 35 |
self._fileno = self.fileno() |
| 36 |
callback = partial(self._io_callback, fut, msg_id) |
| 37 |
try: |
| 38 |
self._ioloop.add_handler( |
| 39 |
self._fileno, callback, IOLoop.WRITE | IOLoop.READ |
| 40 |
) |
| 41 |
except FileExistsError as exc: |
| 42 |
if exc.errno != 17: |
| 43 |
raise exc |
| 44 |
except LDAPError as exc: |
| 45 |
fut.set_exception(exc) |
| 46 |
|
| 47 |
def _timeout_callback(self, fut): |
| 48 |
self._ioloop.remove_handler(self._fileno) |
| 49 |
fut.set_exception(gen.TimeoutError()) |
| 50 |
|
| 51 |
def _evaluate(self, msg_id, timeout=None): |
| 52 |
fut = Future() |
| 53 |
callback = partial(self._io_callback, fut, msg_id) |
| 54 |
self._fileno = self.fileno() |
| 55 |
try: |
| 56 |
self._ioloop.add_handler(self._fileno, callback, IOLoop.WRITE | IOLoop.READ) |
| 57 |
if timeout is not None: |
| 58 |
self._timeout = self._ioloop.call_later( |
| 59 |
timeout, self._timeout_callback, fut |
| 60 |
) |
| 61 |
except FileExistsError as exc: |
| 62 |
|
| 63 |
|
| 64 |
if exc.errno != 17: |
| 65 |
raise exc |
| 66 |
return fut |
| 67 |
|
| 68 |
@gen.coroutine |
| 69 |
def delete(self, dname, timeout=None, recursive=False): |
| 70 |
try: |
| 71 |
res = yield super().delete(dname, timeout, recursive) |
| 72 |
return res |
| 73 |
except NotAllowedOnNonleaf as exc: |
| 74 |
if recursive: |
| 75 |
results = yield self.search( |
| 76 |
dname, LDAPSearchScope.ONELEVEL, attrlist=["1.1"], timeout=timeout |
| 77 |
) |
| 78 |
for res in results: |
| 79 |
yield self.delete(res.dn, timeout, True) |
| 80 |
res = yield self.delete(dname, timeout, False) |
| 81 |
return res |
| 82 |
else: |
| 83 |
raise exc |
| 84 |
|
| 85 |
@gen.coroutine |
| 86 |
def _search_iter_anext(self, search_iter): |
| 87 |
try: |
| 88 |
return next(search_iter) |
| 89 |
except StopIteration: |
| 90 |
msgid = search_iter.acquire_next_page() |
| 91 |
if msgid is None: |
| 92 |
raise StopAsyncIteration from None |
| 93 |
search_iter = yield self._evaluate(msgid) |
| 94 |
return next(search_iter) |
| 95 |
|
| 96 |
@gen.coroutine |
| 97 |
def get_result(self, msg_id, timeout=None): |
| 98 |
res = yield self._evaluate(msg_id, timeout) |
| 99 |
return res |
| 100 |
|