public User importLDAPUserByScreenName(long companyId, String screenName) throws Exception { long ldapServerId = PortalLDAPUtil.getLdapServerId(companyId, screenName, StringPool.BLANK); SearchResult result = (SearchResult) PortalLDAPUtil.getUser(ldapServerId, companyId, screenName, StringPool.BLANK); if (result == null) { if (_log.isWarnEnabled()) { _log.warn("No user was found in LDAP with screenName " + screenName); } return null; } LdapContext ldapContext = PortalLDAPUtil.getContext(ldapServerId, companyId); String fullUserDN = PortalLDAPUtil.getNameInNamespace(ldapServerId, companyId, result); Attributes attributes = PortalLDAPUtil.getUserAttributes(ldapServerId, companyId, ldapContext, fullUserDN); User user = importLDAPUser(ldapServerId, companyId, ldapContext, attributes, StringPool.BLANK); ldapContext.close(); return user; }
protected void importGroups( long ldapServerId, long companyId, LdapContext ldapContext, Attributes attributes, User user, Properties userMappings, Properties groupMappings) throws Exception { List<Long> newUserGroupIds = new ArrayList<Long>(); if (PrefsPropsUtil.getBoolean(companyId, PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER_ENABLED)) { String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId); String baseDN = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_BASE_DN + postfix); Binding binding = PortalLDAPUtil.getUser( ldapServerId, companyId, user.getScreenName(), user.getEmailAddress()); String fullUserDN = PortalLDAPUtil.getNameInNamespace(ldapServerId, companyId, binding); StringBundler sb = new StringBundler(9); sb.append(StringPool.OPEN_PARENTHESIS); sb.append(StringPool.AMPERSAND); sb.append( PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER + postfix)); sb.append(StringPool.OPEN_PARENTHESIS); sb.append(groupMappings.getProperty("user")); sb.append(StringPool.EQUAL); sb.append(escapeValue(fullUserDN)); sb.append(StringPool.CLOSE_PARENTHESIS); sb.append(StringPool.CLOSE_PARENTHESIS); byte[] cookie = new byte[0]; while (cookie != null) { List<SearchResult> searchResults = new ArrayList<SearchResult>(); String groupMappingsGroupName = GetterUtil.getString(groupMappings.getProperty("groupName")).toLowerCase(); cookie = PortalLDAPUtil.searchLDAP( companyId, ldapContext, cookie, 0, baseDN, sb.toString(), new String[] {groupMappingsGroupName}, searchResults); for (SearchResult searchResult : searchResults) { String fullGroupDN = PortalLDAPUtil.getNameInNamespace(ldapServerId, companyId, searchResult); newUserGroupIds = importGroup( ldapServerId, companyId, ldapContext, fullGroupDN, user, groupMappings, newUserGroupIds); } } } else { String userMappingsGroup = userMappings.getProperty("group"); if (Validator.isNull(userMappingsGroup)) { return; } Attribute userGroupAttribute = attributes.get(userMappingsGroup); if (userGroupAttribute == null) { return; } for (int i = 0; i < userGroupAttribute.size(); i++) { String fullGroupDN = (String) userGroupAttribute.get(i); newUserGroupIds = importGroup( ldapServerId, companyId, ldapContext, fullGroupDN, user, groupMappings, newUserGroupIds); } } addUserGroupsNotAddedByLDAPImport(user.getUserId(), newUserGroupIds); for (long newUserGroupId : newUserGroupIds) { UserLocalServiceUtil.addUserGroupUsers(newUserGroupId, new long[] {user.getUserId()}); } List<UserGroup> userUserGroups = UserGroupLocalServiceUtil.getUserUserGroups(user.getUserId()); for (UserGroup userGroup : userUserGroups) { if (!newUserGroupIds.contains(userGroup.getUserGroupId())) { UserLocalServiceUtil.deleteUserGroupUser(userGroup.getUserGroupId(), user.getUserId()); } } }