src/pyams_ldap/plugin.py
changeset 40 864c3e02e890
parent 38 0cde6357775d
child 44 b38760ada646
equal deleted inserted replaced
39:148e16dfb86c 40:864c3e02e890
    39 
    39 
    40 
    40 
    41 class ConnectionManager(object):
    41 class ConnectionManager(object):
    42     """LDAP connections manager"""
    42     """LDAP connections manager"""
    43 
    43 
       
    44     _connection = None
       
    45 
    44     def __init__(self, plugin):
    46     def __init__(self, plugin):
    45         self.server = ldap3.Server(plugin.host,
    47         self.server = ldap3.Server(plugin.host,
    46                                    port=plugin.port,
    48                                    port=plugin.port,
    47                                    use_ssl=plugin.use_ssl,
    49                                    use_ssl=plugin.use_ssl)
    48                                    tls=plugin.use_tls)
       
    49         self.bind_dn = plugin.bind_dn
    50         self.bind_dn = plugin.bind_dn
    50         self.password = plugin.bind_password
    51         self.password = plugin.bind_password
    51 
    52 
    52     def get_connection(self, user=None, password=None, read_only=True):
    53     def get_connection(self, user=None, password=None, read_only=True):
    53         if user:
    54         if user:
    54             conn = ldap3.Connection(self.server,
    55             conn = ldap3.Connection(self.server,
    55                                     user=user, password=password,
    56                                     user=user, password=password,
    56                                     client_strategy=ldap3.ASYNC,
    57                                     client_strategy=ldap3.ASYNC,
    57                                     auto_bind=ldap3.AUTO_BIND_DEFAULT,
    58                                     auto_bind=True,
    58                                     lazy=True,
    59                                     lazy=False,
    59                                     read_only=read_only)
    60                                     read_only=read_only)
    60         else:
    61         else:
    61             conn = ldap3.Connection(self.server,
    62             conn = self._connection
    62                                     user=self.bind_dn, password=self.password,
    63             if conn is None:
    63                                     client_strategy=ldap3.REUSABLE,
    64                 bind_mode = ldap3.AUTO_BIND_DEFAULT if self.bind_dn else ldap3.AUTO_BIND_NONE
    64                                     auto_bind=ldap3.AUTO_BIND_DEFAULT if self.bind_dn else ldap3.AUTO_BIND_NONE,
    65                 conn = ldap3.Connection(self.server,
    65                                     lazy=True,
    66                                         user=self.bind_dn, password=self.password,
    66                                     read_only=read_only)
    67                                         client_strategy=ldap3.REUSABLE,
    67             if conn.auto_bind == ldap3.AUTO_BIND_NONE:
    68                                         auto_bind=bind_mode,
    68                 conn.open(read_server_info=False)
    69                                         lazy=True,
       
    70                                         read_only=read_only)
       
    71                 if conn.auto_bind == ldap3.AUTO_BIND_NONE:
       
    72                     conn.open(read_server_info=False)
       
    73                 self._connection = conn
    69         return conn
    74         return conn
    70 
    75 
    71 
    76 
    72 @implementer(ILDAPUserInfo)
    77 @implementer(ILDAPUserInfo)
    73 class LDAPUserInfo(object):
    78 class LDAPUserInfo(object):
   164     _host = None
   169     _host = None
   165     _port = None
   170     _port = None
   166     _use_ssl = False
   171     _use_ssl = False
   167 
   172 
   168     _server_uri = FieldProperty(ILDAPPlugin['server_uri'])
   173     _server_uri = FieldProperty(ILDAPPlugin['server_uri'])
   169     use_tls = FieldProperty(ILDAPPlugin['use_tls'])
       
   170     bind_dn = FieldProperty(ILDAPPlugin['bind_dn'])
   174     bind_dn = FieldProperty(ILDAPPlugin['bind_dn'])
   171     bind_password = FieldProperty(ILDAPPlugin['bind_password'])
   175     bind_password = FieldProperty(ILDAPPlugin['bind_password'])
   172 
   176 
   173     base_dn = FieldProperty(ILDAPPlugin['base_dn'])
   177     base_dn = FieldProperty(ILDAPPlugin['base_dn'])
   174     search_scope = FieldProperty(ILDAPPlugin['search_scope'])
   178     search_scope = FieldProperty(ILDAPPlugin['search_scope'])
   249 
   253 
   250     def get_connection(self, user=None, password=None):
   254     def get_connection(self, user=None, password=None):
   251         self_id = self._get_id()
   255         self_id = self._get_id()
   252         if self_id not in managers:
   256         if self_id not in managers:
   253             managers[self_id] = ConnectionManager(self)
   257             managers[self_id] = ConnectionManager(self)
   254         return managers[self_id].get_connection(user, password)
   258         connection = managers[self_id].get_connection(user, password)
       
   259         if connection.closed:
       
   260             connection.open(read_server_info=False)
       
   261         return connection
   255 
   262 
   256     def authenticate(self, credentials, request):
   263     def authenticate(self, credentials, request):
   257         if not self.enabled:
   264         if not self.enabled:
   258             return None
   265             return None
   259         attrs = credentials.attributes
   266         attrs = credentials.attributes