Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: LDAP Import User Customization
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");
}
You can use LDAPUtil.getContext() to retrieve the LDAP
context. Pass the required parameters such as companyId
and configuration values for the LDAP server.
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
Thank you so much for the help.