• Roel Bruggink

Show a message on errors with LDAP server in Plone

We needed to show a message to the end user when there were problems connecting to the LDAP server when authenticating.

Sometimes IT changes passwords because of policy/BOFH-ing and doesn’t tell the user. Not to mention all the times IT take down a server for maintenance, or when someone needs a socket for the vacuum cleaner. Wow, someone’s up for a surprise in the morning!

Now, what we’ve done is patch the authentication process and check if errors are logged. If so, display a message to the end user to bugger IT. Again.

Here’s the gist.

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"
  />
We love code

Cookies

Wij maken gebruik van cookies. Meer hierover lees je in onze Privacy- cookieverklaring.