Last active
August 29, 2015 14:00
-
-
Save jaroel/6ea92dafd1c61dc08de9 to your computer and use it in GitHub Desktop.
Show message for end user on errors with LDAP server in Plone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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