/modules/apps/foundation/portal-security/portal-security-ldap/src/main/java/com/liferay/portal/security/ldap/internal/DefaultLDAPSettings.java

http://github.com/liferay/liferay-portal · Java · 337 lines · 240 code · 79 blank · 18 comment · 9 complexity · e135be6c1b84746f52ec602b0b5cc16f MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.security.ldap.internal;
  15. import com.liferay.portal.kernel.log.Log;
  16. import com.liferay.portal.kernel.log.LogFactoryUtil;
  17. import com.liferay.portal.kernel.log.LogUtil;
  18. import com.liferay.portal.kernel.model.User;
  19. import com.liferay.portal.kernel.security.ldap.LDAPSettings;
  20. import com.liferay.portal.kernel.service.UserLocalService;
  21. import com.liferay.portal.kernel.util.ArrayUtil;
  22. import com.liferay.portal.kernel.util.CharPool;
  23. import com.liferay.portal.kernel.util.StringPool;
  24. import com.liferay.portal.kernel.util.StringUtil;
  25. import com.liferay.portal.security.ldap.authenticator.configuration.LDAPAuthConfiguration;
  26. import com.liferay.portal.security.ldap.configuration.ConfigurationProvider;
  27. import com.liferay.portal.security.ldap.configuration.LDAPServerConfiguration;
  28. import com.liferay.portal.security.ldap.configuration.SystemLDAPConfiguration;
  29. import com.liferay.portal.security.ldap.exportimport.configuration.LDAPExportConfiguration;
  30. import com.liferay.portal.security.ldap.exportimport.configuration.LDAPImportConfiguration;
  31. import com.liferay.portal.security.ldap.util.LDAPUtil;
  32. import java.util.Properties;
  33. import org.osgi.service.component.annotations.Component;
  34. import org.osgi.service.component.annotations.Reference;
  35. /**
  36. * @author Edward Han
  37. * @author Michael C. Han
  38. * @author Brian Wing Shun Chan
  39. */
  40. @Component(immediate = true, service = LDAPSettings.class)
  41. public class DefaultLDAPSettings implements LDAPSettings {
  42. @Override
  43. public String getAuthSearchFilter(
  44. long ldapServerId, long companyId, String emailAddress,
  45. String screenName, String userId)
  46. throws Exception {
  47. LDAPServerConfiguration ldapServerConfiguration =
  48. _ldapServerConfigurationProvider.getConfiguration(
  49. companyId, ldapServerId);
  50. String filter = ldapServerConfiguration.authSearchFilter();
  51. if (_log.isDebugEnabled()) {
  52. _log.debug("Search filter before transformation " + filter);
  53. }
  54. filter = StringUtil.replace(
  55. filter,
  56. new String[] {
  57. "@company_id@", "@email_address@", "@screen_name@", "@user_id@"
  58. },
  59. new String[] {
  60. String.valueOf(companyId), emailAddress, screenName, userId
  61. });
  62. LDAPUtil.validateFilter(filter);
  63. if (_log.isDebugEnabled()) {
  64. _log.debug("Search filter after transformation " + filter);
  65. }
  66. return filter;
  67. }
  68. @Override
  69. public Properties getContactExpandoMappings(
  70. long ldapServerId, long companyId)
  71. throws Exception {
  72. LDAPServerConfiguration ldapServerConfiguration =
  73. _ldapServerConfigurationProvider.getConfiguration(
  74. companyId, ldapServerId);
  75. Properties contactExpandoMappings = getProperties(
  76. ldapServerConfiguration.contactCustomMappings());
  77. LogUtil.debug(_log, contactExpandoMappings);
  78. return contactExpandoMappings;
  79. }
  80. @Override
  81. public Properties getContactMappings(long ldapServerId, long companyId)
  82. throws Exception {
  83. LDAPServerConfiguration ldapServerConfiguration =
  84. _ldapServerConfigurationProvider.getConfiguration(
  85. companyId, ldapServerId);
  86. Properties contactMappings = getProperties(
  87. ldapServerConfiguration.contactMappings());
  88. LogUtil.debug(_log, contactMappings);
  89. return contactMappings;
  90. }
  91. @Override
  92. public String[] getErrorPasswordHistoryKeywords(long companyId) {
  93. SystemLDAPConfiguration systemLDAPConfiguration =
  94. _systemLDAPConfigurationProvider.getConfiguration(companyId);
  95. return systemLDAPConfiguration.errorPasswordHistoryKeywords();
  96. }
  97. @Override
  98. public Properties getGroupMappings(long ldapServerId, long companyId)
  99. throws Exception {
  100. LDAPServerConfiguration ldapServerConfiguration =
  101. _ldapServerConfigurationProvider.getConfiguration(
  102. companyId, ldapServerId);
  103. Properties groupMappings = getProperties(
  104. ldapServerConfiguration.groupMappings());
  105. LogUtil.debug(_log, groupMappings);
  106. return groupMappings;
  107. }
  108. @Override
  109. public long getPreferredLDAPServerId(long companyId, String screenName) {
  110. User user = _userLocalService.fetchUserByScreenName(
  111. companyId, screenName);
  112. if (user == null) {
  113. return -1;
  114. }
  115. return user.getLdapServerId();
  116. }
  117. @Override
  118. public String getPropertyPostfix(long ldapServerId) {
  119. return StringPool.PERIOD + ldapServerId;
  120. }
  121. @Override
  122. public Properties getUserExpandoMappings(long ldapServerId, long companyId)
  123. throws Exception {
  124. LDAPServerConfiguration ldapServerConfiguration =
  125. _ldapServerConfigurationProvider.getConfiguration(
  126. companyId, ldapServerId);
  127. Properties contactExpandoMappings = getProperties(
  128. ldapServerConfiguration.userCustomMappings());
  129. LogUtil.debug(_log, contactExpandoMappings);
  130. return contactExpandoMappings;
  131. }
  132. @Override
  133. public Properties getUserMappings(long ldapServerId, long companyId)
  134. throws Exception {
  135. LDAPServerConfiguration ldapServerConfiguration =
  136. _ldapServerConfigurationProvider.getConfiguration(
  137. companyId, ldapServerId);
  138. Properties userMappings = getProperties(
  139. ldapServerConfiguration.userMappings());
  140. LogUtil.debug(_log, userMappings);
  141. return userMappings;
  142. }
  143. @Override
  144. public boolean isExportEnabled(long companyId) {
  145. LDAPImportConfiguration ldapImportConfiguration =
  146. _ldapImportConfigurationProvider.getConfiguration(companyId);
  147. boolean defaultImportUserPasswordAutogenerated =
  148. ldapImportConfiguration.importUserPasswordAutogenerated();
  149. if (ldapImportConfiguration.importEnabled() &&
  150. defaultImportUserPasswordAutogenerated) {
  151. return false;
  152. }
  153. LDAPExportConfiguration ldapExportConfiguration =
  154. _ldapExportConfigurationProvider.getConfiguration(companyId);
  155. return ldapExportConfiguration.exportEnabled();
  156. }
  157. @Override
  158. public boolean isExportGroupEnabled(long companyId) {
  159. LDAPExportConfiguration ldapExportConfiguration =
  160. _ldapExportConfigurationProvider.getConfiguration(companyId);
  161. return ldapExportConfiguration.exportGroupEnabled();
  162. }
  163. @Override
  164. public boolean isImportEnabled(long companyId) {
  165. LDAPImportConfiguration ldapImportConfiguration =
  166. _ldapImportConfigurationProvider.getConfiguration(companyId);
  167. return ldapImportConfiguration.importEnabled();
  168. }
  169. @Override
  170. public boolean isImportOnStartup(long companyId) {
  171. LDAPImportConfiguration ldapImportConfiguration =
  172. _ldapImportConfigurationProvider.getConfiguration(companyId);
  173. return ldapImportConfiguration.importOnStartup();
  174. }
  175. @Override
  176. public boolean isPasswordPolicyEnabled(long companyId) {
  177. LDAPAuthConfiguration ldapAuthConfiguration =
  178. _ldapAuthConfigurationProvider.getConfiguration(companyId);
  179. return ldapAuthConfiguration.passwordPolicyEnabled();
  180. }
  181. protected Properties getProperties(String[] keyValuePairs) {
  182. Properties properties = new Properties();
  183. for (String keyValuePair : keyValuePairs) {
  184. String[] keyValue = StringUtil.split(keyValuePair, CharPool.EQUAL);
  185. if (ArrayUtil.isEmpty(keyValue)) {
  186. continue;
  187. }
  188. String value = StringPool.BLANK;
  189. if (keyValue.length == 2) {
  190. value = keyValue[1];
  191. }
  192. properties.put(keyValue[0], value);
  193. }
  194. return properties;
  195. }
  196. @Reference(
  197. target = "(factoryPid=com.liferay.portal.security.ldap.authenticator.configuration.LDAPAuthConfiguration)",
  198. unbind = "-"
  199. )
  200. protected void setLDAPAuthConfigurationProvider(
  201. ConfigurationProvider<LDAPAuthConfiguration>
  202. ldapAuthConfigurationProvider) {
  203. _ldapAuthConfigurationProvider = ldapAuthConfigurationProvider;
  204. }
  205. @Reference(
  206. target = "(factoryPid=com.liferay.portal.security.ldap.exportimport.configuration.LDAPExportConfiguration)",
  207. unbind = "-"
  208. )
  209. protected void setLDAPExportConfigurationProvider(
  210. ConfigurationProvider<LDAPExportConfiguration>
  211. ldapExportConfigurationProvider) {
  212. _ldapExportConfigurationProvider = ldapExportConfigurationProvider;
  213. }
  214. @Reference(
  215. target = "(factoryPid=com.liferay.portal.security.ldap.exportimport.configuration.LDAPImportConfiguration)",
  216. unbind = "-"
  217. )
  218. protected void setLDAPImportConfigurationProvider(
  219. ConfigurationProvider<LDAPImportConfiguration>
  220. ldapImportConfigurationProvider) {
  221. _ldapImportConfigurationProvider = ldapImportConfigurationProvider;
  222. }
  223. @Reference(
  224. target = "(factoryPid=com.liferay.portal.security.ldap.configuration.LDAPServerConfiguration)",
  225. unbind = "-"
  226. )
  227. protected void setLDAPServerConfigurationProvider(
  228. ConfigurationProvider<LDAPServerConfiguration>
  229. ldapServerConfigurationProvider) {
  230. _ldapServerConfigurationProvider = ldapServerConfigurationProvider;
  231. }
  232. @Reference(
  233. target = "(factoryPid=com.liferay.portal.security.ldap.configuration.SystemLDAPConfiguration)",
  234. unbind = "-"
  235. )
  236. protected void setSystemLDAPConfigurationProvider(
  237. ConfigurationProvider<SystemLDAPConfiguration>
  238. systemLDAPConfigurationProvider) {
  239. _systemLDAPConfigurationProvider = systemLDAPConfigurationProvider;
  240. }
  241. @Reference(unbind = "-")
  242. protected void setUserLocalService(UserLocalService userLocalService) {
  243. _userLocalService = userLocalService;
  244. }
  245. private static final Log _log = LogFactoryUtil.getLog(
  246. DefaultLDAPSettings.class);
  247. private ConfigurationProvider<LDAPAuthConfiguration>
  248. _ldapAuthConfigurationProvider;
  249. private ConfigurationProvider<LDAPExportConfiguration>
  250. _ldapExportConfigurationProvider;
  251. private ConfigurationProvider<LDAPImportConfiguration>
  252. _ldapImportConfigurationProvider;
  253. private ConfigurationProvider<LDAPServerConfiguration>
  254. _ldapServerConfigurationProvider;
  255. private ConfigurationProvider<SystemLDAPConfiguration>
  256. _systemLDAPConfigurationProvider;
  257. private UserLocalService _userLocalService;
  258. }