且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将LDAP属性映射到Liferay用户显示语言

更新时间:2022-06-04 07:38:21

如注释中所述,我发现执行此任务的唯一方法是编写自定义LDAPImporterImpl并将其放入EXT插件中.这是我的代码段:

As mentioned in comments, the only way I've found to perform such task is to write a custom LDAPImporterImpl and put it into an EXT plug-in. Here is a snippet of my code:

import com.liferay.portal.security.ldap.PortalLDAPImporterImpl
// other imports 

public class CustomPortalLDAPImporterImpl extends PortalLDAPImporterImpl {

  @Override
  public User importLDAPUser(long ldapServerId, long companyId, LdapContext ldapContext, Attributes attributes, String password) throws Exception {
    User user = super.importLDAPUser(ldapServerId, companyId, ldapContext, attributes, password);
    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
    String baseDN = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_BASE_DN + postfix);
    Attributes completeUserAttributes = getUserLdapAttributes(ldapContext, user, baseDN);
    setUserAddress(user, completeUserAttributes);
    setUserPhones(user, completeUserAttributes);
    return user;
  }

  // ...

  private Attributes getUserLdapAttributes(LdapContext ctx, User user, String baseDN) {
    String searchFilter = "(&(objectClass=person)(sAMAccountName=" + user.getScreenName() + "))";
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration<SearchResult> results;
    try {
      log.debug("Searching LDAP with the following filter: " + searchFilter);
      results = ctx.search(baseDN, searchFilter, searchControls);
      SearchResult searchResult = null;
      if(results.hasMoreElements()) {
        searchResult = (SearchResult) results.nextElement();
        if(results.hasMoreElements()) {
          log.error("Matched multiple users for the user: " + user.getScreenName());
          return null;
        }
        Attributes attributes = searchResult.getAttributes();
        return attributes;
      } else {
        log.error("No LDAP record for username [" + user.getScreenName() + "] found.");
      }
    } catch (NamingException e) {
      log.error("Error getting attributes for user [" + user.getScreenName() + "]: " + e.getMessage());
    }
    return null;
  }

  // ...

}

您还必须在EXT插件的META-INF/ext-spring.xml文件中定义此导入程序:

You also have to define this importer in the META-INF/ext-spring.xml file of the EXT plug-in:

<?xml version="1.0"?>

<beans
    default-destroy-method="destroy"
    default-init-method="afterPropertiesSet"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >

    <bean id="ldapToPortalConverter" class="com.liferay.portal.security.ldap.DefaultLDAPToPortalConverter" />
    <bean id="portalToLDAPConverter" class="com.liferay.portal.security.ldap.DefaultPortalToLDAPConverter" />
    <bean id="com.liferay.portal.security.ldap.PortalLDAPExporterUtil" class="com.liferay.portal.security.ldap.PortalLDAPExporterUtil">
        <property name="portalLDAPExporter">
            <bean class="com.liferay.portal.security.ldap.PortalLDAPExporterImpl">
                <property name="portalToLDAPConverter" ref="portalToLDAPConverter" />
            </bean>
        </property>
    </bean>
    <bean id="com.liferay.portal.security.ldap.PortalLDAPImporterUtil" class="com.liferay.portal.security.ldap.PortalLDAPImporterUtil">
        <property name="portalLDAPImporter">
            <bean class="ch.openinteractive.familea.security.ldap.CustomPortalLDAPImporterImpl">
                <property name="LDAPToPortalConverter" ref="ldapToPortalConverter" />
            </bean>
        </property>
    </bean>
</beans>

如果有人提供更好,侵入性较小的解决方案,我会很高兴.

I'd be happy if someone come with a better, less invasive solution.