|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
# |
3 |
# Univention Management Console |
4 |
# module: management of virtualization servers |
5 |
# |
6 |
# Copyright 2010-2015 Univention GmbH |
7 |
# |
8 |
# http://www.univention.de/ |
9 |
# |
10 |
# All rights reserved. |
11 |
# |
12 |
# The source code of this program is made available |
13 |
# under the terms of the GNU Affero General Public License version 3 |
14 |
# (GNU AGPL V3) as published by the Free Software Foundation. |
15 |
# |
16 |
# Binary versions of this program provided by Univention to you as |
17 |
# well as other copyrighted, protected or trademarked materials like |
18 |
# Logos, graphics, fonts, specific documentations and configurations, |
19 |
# cryptographic keys etc. are subject to a license agreement between |
20 |
# you and Univention and not subject to the GNU AGPL V3. |
21 |
# |
22 |
# In the case you use this program under the terms of the GNU AGPL V3, |
23 |
# the program is provided in the hope that it will be useful, |
24 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 |
# GNU Affero General Public License for more details. |
27 |
# |
28 |
# You should have received a copy of the GNU Affero General Public |
29 |
# License with the Debian GNU/Linux or Univention distribution in file |
30 |
# /usr/share/common-licenses/AGPL-3; if not, see |
31 |
# <http://www.gnu.org/licenses/>. |
32 |
|
33 |
import univention.admin.uldap as udm_uldap |
34 |
import univention.admin.uexceptions as udm_errors |
35 |
|
36 |
from ldap import LDAPError |
37 |
|
38 |
# decorator for LDAP connections |
39 |
_ldap_connection = None |
40 |
_ldap_position = None |
41 |
|
42 |
|
43 |
class LDAP_ConnectionError(Exception): |
44 |
""" |
45 |
Error connecting LDAP server. |
46 |
""" |
47 |
pass |
48 |
|
49 |
|
50 |
def LDAP_Connection(func): |
51 |
""" |
52 |
This decorator function provides an open LDAP connection that can |
53 |
be accessed via the variable ldap_connection and a vaild position |
54 |
within the LDAP directory in the variable ldap_position. It reuses |
55 |
an already open connection or creates a new one. If the function |
56 |
fails with an LDAP error the decorators tries to reopen the LDAP |
57 |
connection and invokes the function again. if it still fails an |
58 |
LDAP_ConnectionError is raised. |
59 |
|
60 |
When using the decorator the method gets two additional keyword arguments. |
61 |
|
62 |
example: |
63 |
@LDAP_Connection |
64 |
def do_ldap_stuff(arg1, arg2, ldap_connection=None, ldap_positio=None): |
65 |
... |
66 |
ldap_connection.searchDn(..., position=ldap_position) |
67 |
... |
68 |
""" |
69 |
def wrapper_func(*args, **kwargs): |
70 |
global _ldap_connection, _ldap_position |
71 |
|
72 |
if _ldap_connection is not None: |
73 |
lo = _ldap_connection |
74 |
po = _ldap_position |
75 |
else: |
76 |
try: |
77 |
lo, po = udm_uldap.getMachineConnection(ldap_master=False) |
78 |
except LDAPError, ex: |
79 |
raise LDAP_ConnectionError('Opening LDAP connection failed: %s' % (ex,)) |
80 |
|
81 |
kwargs['ldap_connection'] = lo |
82 |
kwargs['ldap_position'] = po |
83 |
try: |
84 |
ret = func(*args, **kwargs) |
85 |
_ldap_connection = lo |
86 |
_ldap_position = po |
87 |
return ret |
88 |
except udm_errors.base as ex: |
89 |
raise LDAP_ConnectionError(str(ex)) |
90 |
|
91 |
return [] |
92 |
|
93 |
return wrapper_func |