Skip to content

Instantly share code, notes, and snippets.

@jaroel
Last active August 29, 2015 14:00
Show Gist options
  • Save jaroel/6ea92dafd1c61dc08de9 to your computer and use it in GitHub Desktop.
Save jaroel/6ea92dafd1c61dc08de9 to your computer and use it in GitHub Desktop.
Show message for end user on errors with LDAP server in Plone
patches.py:
from plone.api.portal import show_message
from cStringIO import StringIO
import logging
fake_log_file = StringIO()
aux_logger = logging.StreamHandler(fake_log_file)
aux_logger.setLevel(logging.ERROR)
ldap_logger = logging.getLogger('event.LDAPDelegate')
ldap_logger.addHandler(aux_logger)
msg = "Something went wrong while connecting to the LDAP server(s)"
def authenticateCredentials(self, credentials):
fake_log_file.truncate(0) # empty log to catch only current messages
try:
self._old_authenticateCredentials(credentials)
except:
raise
finally:
if fake_log_file.getvalue():
# Assume something went wrong if it is logged.
show_message(msg, self.REQUEST, type='error')
return None, None
configure.zcml:
<monkey:patch
description="Show message on LDAP errors."
class="Products.LDAPMultiPlugins.LDAPPluginBase.LDAPPluginBase"
original="authenticateCredentials"
replacement=".patches.authenticateCredentials"
preserveOriginal="true"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment