Fixed an issue where the user is not able to create a server if login with an LDAP account. Fixes #5439

Improved LDAP error messages.
This commit is contained in:
Khushboo Vashi 2020-04-27 15:33:19 +05:30 committed by Akshay Joshi
parent ed106e3577
commit 0f6abcc7fa
3 changed files with 27 additions and 23 deletions

View File

@ -36,7 +36,7 @@ Bug fixes
| `Issue #3947 <https://redmine.postgresql.org/issues/3947>`_ - Fixed copy-paste row issues in View/Edit Data.
| `Issue #3972 <https://redmine.postgresql.org/issues/3972>`_ - Modified keyboard shortcuts in Query Tool for OSX native support.
| `Issue #3988 <https://redmine.postgresql.org/issues/3988>`_ - Fixed cursor disappeared issue in the query editor for some of the characters when zoomed out.
| `Issue #4108 <https://redmine.postgresql.org/issues/4108>`_ - Fixed mouse click issue where it does not select an object in Browser unless the pointer is over the object.
| `Issue #4180 <https://redmine.postgresql.org/issues/4180>`_ - Fixed mouse click issue where it does not select an object in Browser unless the pointer is over the object.
| `Issue #4206 <https://redmine.postgresql.org/issues/4206>`_ - Ensure that the grant wizard should be closed on pressing the ESC key.
| `Issue #4292 <https://redmine.postgresql.org/issues/4292>`_ - Added dark mode support for the configuration dialog on Windows/macOS runtime.
| `Issue #4440 <https://redmine.postgresql.org/issues/4440>`_ - Ensure the DROP statements in reverse engineered SQL are properly quoted for all objects.
@ -89,3 +89,4 @@ Bug fixes
| `Issue #5420 <https://redmine.postgresql.org/issues/5420>`_ - Ensure error should be handled properly when LDAP user is created with the same name.
| `Issue #5430 <https://redmine.postgresql.org/issues/5430>`_ - Added title to the login page.
| `Issue #5432 <https://redmine.postgresql.org/issues/5432>`_ - Fixed an issue where an internal user is not created if the authentication source is set to internal and ldap.
| `Issue #5439 <https://redmine.postgresql.org/issues/5439>`_ - Fixed an issue where the user is not able to create a server if login with an LDAP account.

View File

@ -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.")

View File

@ -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