LDAP Sync with Liferay DB


In liferay you can define listeners for an entity. They work like triggers and necessary code will be called based on what action is performed on the entity (update , create etc).In these three steps you can understand how LDAP is synched with Liferay DB.

1) Necessary properties.

For LDAP Sync following listeners are used.They are already defined in portal.properties . If you have to change them you need to import classes  in your ext env.

##
## Value Object
##
 value.object.listener.com.liferay.portal.model.Contact=com.liferay.portal.model.ContactListener
 value.object.listener.com.liferay.portal.model.User=com.liferay.portal.model.UserListener

2) Listener Classes

  1. User listener is called when we update the user model i.e. data saved in User_ Table. Here is the code from UserListener.java
public class UserListener extends BaseModelListener<User> { 

 public void onAfterUpdate(User user) throws ModelListenerException {
 try {
 //PortalLDAPUtil.exportToLDAP(user);
 MyPortalLDAPUtil.exportToLDAP(user);
 }
 catch (Exception e) {
 throw new ModelListenerException(e);
 }

 }
}

Here  PortalLDAPUtil.exportToLDAP(user) is called which updates the user data in LDAP. You can define you own class also which can extend this class and  update your additional attributes also.

2. Contact listener is called when a contact is created (which means when a User or contact is created) and when a contact is updated. Here is the code from ContactListener.java

public class ContactListener extends BaseModelListener<Contact> {

  public void onAfterCreate(Contact contact) throws ModelListenerException {
    try {
        //PortalLDAPUtil.exportToLDAP(contact);
        MyPortalLDAPUtil.exportToLDAP(contact);
    }
    catch (Exception e) {
       throw new ModelListenerException(e);
   }
  }

  public void onAfterUpdate(Contact contact) throws ModelListenerException {
     try {
           //PortalLDAPUtil.exportToLDAP(contact);
           MyPortalLDAPUtil.exportToLDAP(contact);
      }
     catch (Exception e) {
         throw new ModelListenerException(e);
     }
  }

3. LDAP Util Class

PortalLDAPUtil.java implements the functionality to complete the sync.This class reads LDAP Properties to connect/search/fetch attributes etc with LDAP.The LDAP properties are read either from Control Panel -> Authentication -> LDAP tab or you can specify them in portal-ext.properties files also. Here are the propeties which are used in LDAP , they are defined in portal.properties  LDAP section.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s