10 # FOR A PARTICULAR PURPOSE. |
10 # FOR A PARTICULAR PURPOSE. |
11 # |
11 # |
12 |
12 |
13 __docformat__ = 'restructuredtext' |
13 __docformat__ = 'restructuredtext' |
14 |
14 |
15 |
|
16 # import standard library |
|
17 import ldap3 |
|
18 import logging |
15 import logging |
19 logger = logging.getLogger('PyAMS (ldap)') |
16 logger = logging.getLogger('PyAMS (ldap)') |
20 |
17 |
|
18 import ldap3 |
21 import re |
19 import re |
22 |
20 |
23 # import interfaces |
|
24 from pyams_ldap.interfaces import ILDAPPlugin, ILDAPUserInfo, ILDAPGroupInfo |
21 from pyams_ldap.interfaces import ILDAPPlugin, ILDAPUserInfo, ILDAPGroupInfo |
25 from pyams_mail.interfaces import IPrincipalMailInfo |
22 from pyams_mail.interfaces import IPrincipalMailInfo |
26 from zope.intid.interfaces import IIntIds |
23 from zope.intid.interfaces import IIntIds |
27 |
24 |
28 # import packages |
|
29 from beaker.cache import cache_region |
25 from beaker.cache import cache_region |
30 from persistent import Persistent |
26 from persistent import Persistent |
31 from pyams_ldap.query import LDAPQuery |
27 from pyams_ldap.query import LDAPQuery |
32 from pyams_security.principal import PrincipalInfo |
28 from pyams_security.principal import PrincipalInfo |
33 from pyams_utils.adapter import adapter_config, ContextAdapter |
29 from pyams_utils.adapter import adapter_config, ContextAdapter |
51 port=plugin.port, |
46 port=plugin.port, |
52 use_ssl=plugin.use_ssl, |
47 use_ssl=plugin.use_ssl, |
53 tls=plugin.use_tls) |
48 tls=plugin.use_tls) |
54 self.bind_dn = plugin.bind_dn |
49 self.bind_dn = plugin.bind_dn |
55 self.password = plugin.bind_password |
50 self.password = plugin.bind_password |
56 if plugin.use_pool: |
51 |
57 self.strategy = ldap3.REUSABLE |
52 def get_connection(self, user=None, password=None, read_only=True): |
58 self.pool_name = 'pyams_ldap:{prefix}'.format(prefix=plugin.prefix) |
|
59 self.pool_size = plugin.pool_size |
|
60 self.pool_lifetime = plugin.pool_lifetime |
|
61 else: |
|
62 self.strategy = ldap3.ASYNC |
|
63 self.pool_name = None |
|
64 self.pool_size = None |
|
65 self.pool_lifetime = None |
|
66 |
|
67 def get_connection(self, user=None, password=None): |
|
68 if user: |
53 if user: |
69 conn = ldap3.Connection(self.server, |
54 conn = ldap3.Connection(self.server, |
70 user=user, password=password, |
55 user=user, password=password, |
71 client_strategy=ldap3.SYNC, |
56 client_strategy=ldap3.ASYNC, |
72 auto_bind=True, lazy=False, read_only=True) |
57 auto_bind=ldap3.AUTO_BIND_DEFAULT, |
|
58 lazy=True, |
|
59 read_only=read_only) |
73 else: |
60 else: |
74 conn = ldap3.Connection(self.server, |
61 conn = ldap3.Connection(self.server, |
75 user=self.bind_dn, password=self.password, |
62 user=self.bind_dn, password=self.password, |
76 client_strategy=self.strategy, |
63 client_strategy=ldap3.REUSABLE, |
77 pool_name=self.pool_name, |
64 auto_bind=ldap3.AUTO_BIND_DEFAULT if self.bind_dn else ldap3.AUTO_BIND_NONE, |
78 pool_size=self.pool_size, |
65 lazy=True, |
79 pool_lifetime=self.pool_lifetime, |
66 read_only=read_only) |
80 auto_bind=True, lazy=False, read_only=True) |
67 if conn.auto_bind == ldap3.AUTO_BIND_NONE: |
|
68 conn.open(read_server_info=False) |
81 return conn |
69 return conn |
82 |
70 |
83 |
71 |
84 @implementer(ILDAPUserInfo) |
72 @implementer(ILDAPUserInfo) |
85 class LDAPUserInfo(object): |
73 class LDAPUserInfo(object): |
176 _host = None |
164 _host = None |
177 _port = None |
165 _port = None |
178 _use_ssl = False |
166 _use_ssl = False |
179 |
167 |
180 _server_uri = FieldProperty(ILDAPPlugin['server_uri']) |
168 _server_uri = FieldProperty(ILDAPPlugin['server_uri']) |
|
169 use_tls = FieldProperty(ILDAPPlugin['use_tls']) |
181 bind_dn = FieldProperty(ILDAPPlugin['bind_dn']) |
170 bind_dn = FieldProperty(ILDAPPlugin['bind_dn']) |
182 bind_password = FieldProperty(ILDAPPlugin['bind_password']) |
171 bind_password = FieldProperty(ILDAPPlugin['bind_password']) |
183 use_tls = FieldProperty(ILDAPPlugin['use_tls']) |
172 |
184 use_pool = FieldProperty(ILDAPPlugin['use_pool']) |
|
185 pool_size = FieldProperty(ILDAPPlugin['pool_size']) |
|
186 pool_lifetime = FieldProperty(ILDAPPlugin['pool_lifetime']) |
|
187 base_dn = FieldProperty(ILDAPPlugin['base_dn']) |
173 base_dn = FieldProperty(ILDAPPlugin['base_dn']) |
188 search_scope = FieldProperty(ILDAPPlugin['search_scope']) |
174 search_scope = FieldProperty(ILDAPPlugin['search_scope']) |
189 |
175 |
190 login_attribute = FieldProperty(ILDAPPlugin['login_attribute']) |
176 login_attribute = FieldProperty(ILDAPPlugin['login_attribute']) |
191 login_query = FieldProperty(ILDAPPlugin['login_query']) |
177 login_query = FieldProperty(ILDAPPlugin['login_query']) |