From 0f6abcc7fad88f6c40e42d6e3511aaf380336ea1 Mon Sep 17 00:00:00 2001 From: Khushboo Vashi Date: Mon, 27 Apr 2020 15:33:19 +0530 Subject: [PATCH] =?UTF-8?q?Fixed=20an=20issue=20where=20the=20user=20is=20?= =?UTF-8?q?not=20able=20to=C2=A0create=20a=20server=20if=20login=20with=20?= =?UTF-8?q?an=20LDAP=20account.=20Fixes=20#5439?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improved LDAP error messages. --- docs/en_US/release_notes_4_21.rst | 3 ++- web/pgadmin/authenticate/ldap.py | 43 +++++++++++++++++-------------- web/pgadmin/utils/paths.py | 4 +-- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/docs/en_US/release_notes_4_21.rst b/docs/en_US/release_notes_4_21.rst index 7adb352f7..382b11332 100644 --- a/docs/en_US/release_notes_4_21.rst +++ b/docs/en_US/release_notes_4_21.rst @@ -36,7 +36,7 @@ Bug fixes | `Issue #3947 `_ - Fixed copy-paste row issues in View/Edit Data. | `Issue #3972 `_ - Modified keyboard shortcuts in Query Tool for OSX native support. | `Issue #3988 `_ - Fixed cursor disappeared issue in the query editor for some of the characters when zoomed out. -| `Issue #4108 `_ - Fixed mouse click issue where it does not select an object in Browser unless the pointer is over the object. +| `Issue #4180 `_ - Fixed mouse click issue where it does not select an object in Browser unless the pointer is over the object. | `Issue #4206 `_ - Ensure that the grant wizard should be closed on pressing the ESC key. | `Issue #4292 `_ - Added dark mode support for the configuration dialog on Windows/macOS runtime. | `Issue #4440 `_ - Ensure the DROP statements in reverse engineered SQL are properly quoted for all objects. @@ -89,3 +89,4 @@ Bug fixes | `Issue #5420 `_ - Ensure error should be handled properly when LDAP user is created with the same name. | `Issue #5430 `_ - Added title to the login page. | `Issue #5432 `_ - Fixed an issue where an internal user is not created if the authentication source is set to internal and ldap. +| `Issue #5439 `_ - Fixed an issue where the user is not able to create a server if login with an LDAP account. diff --git a/web/pgadmin/authenticate/ldap.py b/web/pgadmin/authenticate/ldap.py index 5aadda642..edd3af612 100644 --- a/web/pgadmin/authenticate/ldap.py +++ b/web/pgadmin/authenticate/ldap.py @@ -28,9 +28,7 @@ except ImportError: from urlparse import urlparse -ERROR_SEARCHING_LDAP_DIRECTORY = gettext( - "Error searching the LDAP directory: %s" -) +ERROR_SEARCHING_LDAP_DIRECTORY = "Error searching the LDAP directory: {}" class LDAPAuthentication(BaseAuthentication): @@ -90,8 +88,9 @@ class LDAPAuthentication(BaseAuthentication): ca_certs_file=ca_cert_file) except LDAPSSLConfigurationError as e: current_app.logger.exception( - "LDAP configuration error: %s\n" % e) - return False, "LDAP configuration error: %s\n" % e.args[0] + "LDAP configuration error: {}\n".format(e)) + return False, "LDAP configuration error: {}\n".format( + e.args[0]) try: # Create the server object @@ -102,7 +101,7 @@ class LDAPAuthentication(BaseAuthentication): tls=tls, connect_timeout=config.LDAP_CONNECTION_TIMEOUT) except ValueError as e: - return False, "LDAP configuration error: %s." % e + return False, "LDAP configuration error: {}.".format(e) # Create the connection try: @@ -118,18 +117,18 @@ class LDAPAuthentication(BaseAuthentication): except LDAPSocketOpenError as e: current_app.logger.exception( - "Error connecting to the LDAP server: %s\n" % e) + "Error connecting to the LDAP server: {}\n".format(e)) return False, "Error connecting to the LDAP server:" \ - " %s\n" % e.args[0] + " {}\n".format(e.args[0]) except LDAPBindError as e: current_app.logger.exception( "Error binding to the LDAP server.") return False, "Error binding to the LDAP server." except Exception as e: current_app.logger.exception( - "Error connecting to the LDAP server: %s\n" % e) + "Error connecting to the LDAP server: {}\n".format(e)) return False, "Error connecting to the LDAP server:" \ - " %s\n" % e.args[0] + " {}\n".format(e.args[0]) # Enable TLS if STARTTLS is configured if not uri.scheme == 'ldaps' and config.LDAP_USE_STARTTLS: @@ -137,8 +136,8 @@ class LDAPAuthentication(BaseAuthentication): self.conn.start_tls() except LDAPStartTLSError as e: current_app.logger.exception( - "Error starting TLS: %s\n" % e) - return False, "Error starting TLS: %s\n" % e.args[0] + "Error starting TLS: {}\n".format(e)) + return False, "Error starting TLS: {}\n".format(e.args[0]) return True, None @@ -162,7 +161,10 @@ class LDAPAuthentication(BaseAuthentication): """Get a list of users from the LDAP server based on config search criteria.""" try: - self.conn.search(search_base=config.LDAP_SEARCH_BASE_DN, + search_base_dn = config.LDAP_SEARCH_BASE_DN + if search_base_dn is None or search_base_dn == '': + search_base_dn = config.LDAP_BASE_DN + self.conn.search(search_base=search_base_dn, search_filter=config.LDAP_SEARCH_FILTER, search_scope=config.LDAP_SEARCH_SCOPE, attributes=ALL_ATTRIBUTES @@ -170,19 +172,19 @@ class LDAPAuthentication(BaseAuthentication): except LDAPInvalidScopeError as e: current_app.logger.exception( - gettext(ERROR_SEARCHING_LDAP_DIRECTORY) % e + ERROR_SEARCHING_LDAP_DIRECTORY.format(e.args[0]) ) - return False, gettext(ERROR_SEARCHING_LDAP_DIRECTORY) % e.args[0] + return False, ERROR_SEARCHING_LDAP_DIRECTORY.format(e.args[0]) except LDAPAttributeError as e: current_app.logger.exception( - gettext(ERROR_SEARCHING_LDAP_DIRECTORY) % e + ERROR_SEARCHING_LDAP_DIRECTORY.format(e) ) - return False, gettext(ERROR_SEARCHING_LDAP_DIRECTORY) % e.args[0] + return False, ERROR_SEARCHING_LDAP_DIRECTORY.format(e.args[0]) except LDAPInvalidFilterError as e: current_app.logger.exception( - gettext(ERROR_SEARCHING_LDAP_DIRECTORY) % e + ERROR_SEARCHING_LDAP_DIRECTORY.format(e) ) - return False, gettext(ERROR_SEARCHING_LDAP_DIRECTORY) % e.args[0] + return False, ERROR_SEARCHING_LDAP_DIRECTORY.format(e.args[0]) for entry in self.conn.entries: user_email = None @@ -191,4 +193,5 @@ class LDAPAuthentication(BaseAuthentication): if 'mail' in entry: user_email = entry['mail'].value return True, user_email - return False, None + return False, ERROR_SEARCHING_LDAP_DIRECTORY.format( + "Could not find the specified user.") diff --git a/web/pgadmin/utils/paths.py b/web/pgadmin/utils/paths.py index dad5f0e41..462147cee 100644 --- a/web/pgadmin/utils/paths.py +++ b/web/pgadmin/utils/paths.py @@ -33,7 +33,7 @@ def get_storage_directory(): if storage_dir is None: return None - username = current_user.email.split('@')[0] + username = current_user.username.split('@')[0] if len(username) == 0 or username[0].isdigit(): username = 'pga_user_' + username @@ -48,7 +48,7 @@ def get_storage_directory(): storage_dir = os.path.join( storage_dir.decode('utf-8') if hasattr(storage_dir, 'decode') else storage_dir, - current_user.email.replace('@', '_') + current_user.username.replace('@', '_') ) # Rename an old-style storage directory, if the new style doesn't exist