001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.ldap;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.model.Contact;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.service.UserLocalServiceUtil;
022    
023    import java.io.Serializable;
024    
025    import java.util.Map;
026    import java.util.Properties;
027    
028    import javax.naming.Binding;
029    import javax.naming.CompositeName;
030    import javax.naming.Name;
031    import javax.naming.directory.Attributes;
032    import javax.naming.directory.ModificationItem;
033    import javax.naming.ldap.LdapContext;
034    
035    /**
036     * @author Michael C. Han
037     * @author Brian Wing Shun Chan
038     */
039    public class PortalLDAPExporterImpl implements PortalLDAPExporter {
040    
041            public void exportToLDAP(
042                            Contact contact, Map<String, Serializable> contactExpandoAttributes)
043                    throws Exception {
044    
045                    long companyId = contact.getCompanyId();
046    
047                    if (!LDAPSettingsUtil.isAuthEnabled(companyId) ||
048                            !LDAPSettingsUtil.isExportEnabled(companyId)) {
049    
050                            return;
051                    }
052    
053                    User user = UserLocalServiceUtil.getUserByContactId(
054                            contact.getContactId());
055    
056                    long ldapServerId = PortalLDAPUtil.getLdapServerId(
057                            companyId, user.getScreenName());
058    
059                    LdapContext ldapContext = PortalLDAPUtil.getContext(
060                            ldapServerId, companyId);
061    
062                    try {
063                            if (ldapContext == null) {
064                                    return;
065                            }
066    
067                            Properties contactMappings = LDAPSettingsUtil.getContactMappings(
068                                    ldapServerId, companyId);
069                            Properties contactExpandoMappings =
070                                    LDAPSettingsUtil.getContactExpandoMappings(
071                                            ldapServerId, companyId);
072    
073                            Binding binding = PortalLDAPUtil.getUser(
074                                    ldapServerId, contact.getCompanyId(), user.getScreenName());
075    
076                            if (binding == null) {
077                                    Properties userMappings = LDAPSettingsUtil.getUserMappings(
078                                            ldapServerId, companyId);
079    
080                                    binding = addUser(
081                                            ldapServerId, ldapContext, user, userMappings);
082                            }
083    
084                            Name name = new CompositeName();
085    
086                            name.add(
087                                    PortalLDAPUtil.getNameInNamespace(
088                                            ldapServerId, companyId, binding));
089    
090                            Modifications modifications =
091                                    _portalToLDAPConverter.getLDAPContactModifications(
092                                            contact, contactExpandoAttributes,
093                                            contactMappings, contactExpandoMappings);
094    
095                            if (modifications == null) {
096                                    return;
097                            }
098    
099                            ModificationItem[] modificationItems = modifications.getItems();
100    
101                            ldapContext.modifyAttributes(name, modificationItems);
102                    }
103                    catch (Exception e) {
104                            throw e;
105                    }
106                    finally {
107                            if (ldapContext != null) {
108                                    ldapContext.close();
109                            }
110                    }
111            }
112    
113            public void exportToLDAP(
114                            User user, Map<String, Serializable> userExpandoAttributes)
115                    throws Exception {
116    
117                    long companyId = user.getCompanyId();
118    
119                    if (!LDAPSettingsUtil.isAuthEnabled(companyId) ||
120                            !LDAPSettingsUtil.isExportEnabled(companyId)) {
121    
122                            return;
123                    }
124    
125                    long ldapServerId = PortalLDAPUtil.getLdapServerId(
126                            companyId, user.getScreenName());
127    
128                    LdapContext ldapContext = PortalLDAPUtil.getContext(
129                            ldapServerId, companyId);
130    
131                    try {
132                            if (ldapContext == null) {
133                                    return;
134                            }
135    
136                            Properties userMappings = LDAPSettingsUtil.getUserMappings(
137                                    ldapServerId, companyId);
138                            Properties userExpandoMappings =
139                                    LDAPSettingsUtil.getUserExpandoMappings(
140                                            ldapServerId, companyId);
141    
142                            Binding binding = PortalLDAPUtil.getUser(
143                                    ldapServerId, user.getCompanyId(), user.getScreenName());
144    
145                            if (binding == null) {
146                                    binding = addUser(
147                                            ldapServerId, ldapContext, user, userMappings);
148                            }
149    
150                            Name name = new CompositeName();
151    
152                            name.add(
153                                    PortalLDAPUtil.getNameInNamespace(
154                                            ldapServerId, companyId, binding));
155    
156                            Modifications modifications =
157                                    _portalToLDAPConverter.getLDAPUserModifications(
158                                            user, userExpandoAttributes, userMappings,
159                                            userExpandoMappings);
160    
161                            if (modifications == null) {
162                                    return;
163                            }
164    
165                            ModificationItem[] modificationItems = modifications.getItems();
166    
167                            ldapContext.modifyAttributes(name, modificationItems);
168                    }
169                    catch (Exception e) {
170                            _log.error(e, e);
171                    }
172                    finally {
173                            if (ldapContext != null) {
174                                    ldapContext.close();
175                            }
176                    }
177            }
178    
179            public void setPortalToLDAPConverter(
180                    PortalToLDAPConverter portalToLDAPConverter) {
181    
182                    _portalToLDAPConverter = portalToLDAPConverter;
183            }
184    
185            protected Binding addUser(
186                            long ldapServerId, LdapContext ldapContext, User user,
187                            Properties userMappings)
188                    throws Exception {
189    
190                    Name name = new CompositeName();
191    
192                    name.add(
193                            _portalToLDAPConverter.getUserDNName(
194                                    ldapServerId, user, userMappings));
195    
196                    Attributes attributes = _portalToLDAPConverter.getLDAPUserAttributes(
197                            ldapServerId, user, userMappings);
198    
199                    ldapContext.bind(name, new PortalLDAPContext(attributes));
200    
201                    Binding binding = PortalLDAPUtil.getUser(
202                            ldapServerId, user.getCompanyId(), user.getScreenName());
203    
204                    return binding;
205            }
206    
207            private static Log _log = LogFactoryUtil.getLog(
208                    PortalLDAPExporterImpl.class);
209    
210            private PortalToLDAPConverter _portalToLDAPConverter;
211    
212    }