将LDAP属性映射到Liferay用户显示语言 [英] Mapping LDAP attribute to Liferay user display language
问题描述
我们正在使用Liferay 6.2 GA4,它允许您仅映射LDAP用户导入的一些基本属性:
screenName, password, emailAddress, firstName, lastName, jobTitle
和group
.如果要映射一些自定义字段,则必须使用自定义映射.两种情况都可以正常工作,但是我们真正想要的是将LDAP属性preferredLanguage
映射到用户languageId
,因此Liferay中的用户语言是基于LDAP值设置的.
我们尝试过的:
We are using Liferay 6.2 GA4, which allows you to map only few basic attributes for LDAP user import:
screenName, password, emailAddress, firstName, lastName, jobTitle
and group
. If you want to map some custom fields you have to use custom mapping. Both cases are working fine but what we really want is to map LDAP attribute preferredLanguage
to user languageId
so the user language in Liferay is set based on LDAP value.
What we have tried:
-
将
preferredLanguage
映射到标准ldap.user.mappings
中的languageId
,因此portalpreferences
表条目中的条目如下所示:
Map
preferredLanguage
tolanguageId
in standardldap.user.mappings
so the entry inportalpreferences
table entry looks like this:
...<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
表条目为:
Map it in ldap.user.custom.mappings
so portalpreferences
table entry is:
...<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
来忽略列表,则也可以将其导入.
Neither works. The only thing what is working is to create custom user field in Liferay e.g. custLanguage
and map preferredLanguage
to this field. But then we don't know how to pass value from custLanguage
to users display settings languageId
so the language for that user is changed automatically.
Lot of users had problems with Liferay language LDAP import, e.g. https://issues.liferay.com/browse/LPS-23143. I assume that if you can add languageId
to ignore list you can also import it.
这个问题与我们的问题相对应,但是解决方案是使用自定义字段,我们不知道如何进一步阐述. 所以我们的问题是:
This question corresponds with our problem but the solution is to use custom fields and we don't know how to elaborate on it further. So our questions are:
- 是否可以将我们的自定义LDAP属性
preferredLanguage
直接映射到Liferay用户的languageId
? - 如果以上操作无法实现,并且您必须使用自定义属性,我们应该如何将自定义用户语言属性的值传递给他的语言显示设置?
- Is it possible to map our custom LDAP attribute
preferredLanguage
directly tolanguageId
of Liferay user? - If the above is not possible and you have to use custom attributes how we should pass the value from custom user language attribute to his language display settings?
推荐答案
如注释中所述,我发现执行此任务的唯一方法是编写自定义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.
这篇关于将LDAP属性映射到Liferay用户显示语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!