我们正在使用Liferay 6.2 GA4,它允许您仅映射LDAP用户导入的几个基本属性:
screenName, password, emailAddress, firstName, lastName, jobTitle
和group
。如果要映射某些自定义字段,则必须使用自定义映射。这两种情况都运行正常但我们真正想要的是将LDAP属性preferredLanguage
映射到用户languageId
,因此Liferay中的用户语言是基于LDAP值设置的。
我们尝试了什么:
将preferredLanguage
映射到标准languageId
中的ldap.user.mappings
,以便portalpreferences
表条目中的条目如下所示:
...<preference>
<name>ldap.user.mappings.21597</name>
<value>emailAddress=uid[$NEW_LINE$]firstName=givenName[$NEW_LINE$]group=memberOf[$NEW_LINE$]lastName=sn[$NEW_LINE$]languageId=preferredLanguage</value>
</preference>...
将其映射到ldap.user.custom.mappings
,以便portalpreferences
表条目为:
...<preference>
<name>ldap.user.custom.mappings.21597</name>
<value>...ourCustomAttributes...[$NEW_LINE$]languageId=preferredLanguage</value>
</preference>...
两者都不起作用。唯一有效的是在Liferay中创建自定义用户字段,例如custLanguage
并将preferredLanguage
映射到此字段。但是,我们不知道如何将值从custLanguage
传递给用户显示设置languageId
,因此该用户的语言会自动更改。
许多用户在Liferay语言LDAP导入时遇到问题,例如https://issues.liferay.com/browse/LPS-23143。我假设如果你可以将languageId
添加到忽略列表,你也可以导入它。
This question对应我们的问题,但解决方案是使用自定义字段,我们不知道如何进一步详细说明。 所以我们的问题是:
preferredLanguage
直接映射到Liferay用户的languageId
?答案 0 :(得分:1)
正如评论中所提到的,我发现执行此类任务的唯一方法是编写自定义LDAPImporterImpl
并将其放入EXT插件中。以下是我的代码片段:
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
文件中定义此导入程序:
<?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>
如果有人提供更好,侵入性更小的解决方案,我会感到高兴。