RE: LDAP Import User Customization

Jamie Sammons, modified 1 Year ago. New Member Posts: 4 Join Date: 12/19/24 Recent Posts

Requirement:

While running the import interval, I want to prevent the import of new users from LDAP to Liferay. Instead, I only want to check the existing users in both LDAP and Liferay.

Implementation:

To achieve this, I have implemented the LDAPUserImporter interface and overridden the importUsers(long companyId) method in my custom import class. In this method, I need to obtain the LDAP context to perform the required operations. Could someone please assist with how to retrieve the LDAP context in this scenario?

 

@Override
public void importUsers(long companyId) throws Exception {
    _log.info("Entering importUsers for a specific company");
    _log.debug("Company ID: " + companyId);

    // Fetch users from LDAP
    List<User> usersFromLDAP = getUsersFromLDAP(companyId);

    // Only import existing users
    for (User user : usersFromLDAP) {
        try {
            // Skip new users or deactivate users based on your custom logic
            _log.info("Importing user: " + user.getFullName());
            _defaultLDAPUserImporter.importUser(companyId, user.getEmailAddress(), user.getScreenName());
        } catch (Exception e) {
            _log.error("Error importing user: " + user.getScreenName(), e);
        }
    }

    _log.info("Exiting importUsers for a specific company");
}
Jamie Sammons, modified 1 Year ago. New Member Post: 1 Join Date: 1/4/25 Recent Posts

You can use LDAPUtil.getContext() to retrieve the LDAP context. Pass the required parameters such as companyId and configuration values for the LDAP server.

thumbnail
Zsigmond Rab, modified 1 Year ago. Liferay Master Posts: 764 Join Date: 1/5/10 Recent Posts

Hi Vishal,

My answer may be late, however, for getting the LDAP server context you need the specific server and its id. For example: https://github.com/liferay/liferay-portal/blob/master/modules/apps/portal-security/portal-security-ldap-impl/src/main/java/com/liferay/portal/security/ldap/internal/exportimport/LDAPUserImporterImpl.java#L541-L542

So first you need to get the LDAP server like it's done in the original implementation of that method: https://github.com/liferay/liferay-portal/blob/master/modules/apps/portal-security/portal-security-ldap-impl/src/main/java/com/liferay/portal/security/ldap/internal/exportimport/LDAPUserImporterImpl.java#L519-L526

In the latter code, the importUsers method called contains the first linked LDAP server context initialization.

Regards,
Zsigmond

Jamie Sammons, modified 1 Year ago. New Member Post: 1 Join Date: 1/23/25 Recent Posts

Thank you so much for the help.