001    /**
002     * Copyright (c) 2000-2013 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.PwdEncryptorException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Contact;
027    import com.liferay.portal.model.Image;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.model.UserGroup;
030    import com.liferay.portal.security.pwd.PasswordEncryptorUtil;
031    import com.liferay.portal.service.ImageLocalServiceUtil;
032    import com.liferay.portal.util.PrefsPropsUtil;
033    import com.liferay.portlet.expando.model.ExpandoBridge;
034    import com.liferay.portlet.expando.util.ExpandoConverterUtil;
035    
036    import java.io.Serializable;
037    
038    import java.util.HashMap;
039    import java.util.List;
040    import java.util.Map;
041    import java.util.Properties;
042    
043    import javax.naming.Binding;
044    import javax.naming.directory.Attribute;
045    import javax.naming.directory.Attributes;
046    import javax.naming.directory.BasicAttribute;
047    import javax.naming.directory.BasicAttributes;
048    import javax.naming.directory.DirContext;
049    
050    import org.apache.commons.beanutils.PropertyUtils;
051    
052    /**
053     * @author Michael C. Han
054     * @author Brian Wing Shun Chan
055     * @author Marcellus Tavares
056     * @author Wesley Gong
057     */
058    public class DefaultPortalToLDAPConverter implements PortalToLDAPConverter {
059    
060            public DefaultPortalToLDAPConverter() {
061                    _reservedUserFieldNames.put(
062                            UserConverterKeys.GROUP, UserConverterKeys.GROUP);
063                    _reservedUserFieldNames.put(
064                            UserConverterKeys.PASSWORD, UserConverterKeys.PASSWORD);
065                    _reservedUserFieldNames.put(
066                            UserConverterKeys.PORTRAIT, UserConverterKeys.PORTRAIT);
067                    _reservedUserFieldNames.put(
068                            UserConverterKeys.SCREEN_NAME, UserConverterKeys.SCREEN_NAME);
069            }
070    
071            @Override
072            public String getGroupDNName(
073                            long ldapServerId, UserGroup userGroup, Properties groupMappings)
074                    throws Exception {
075    
076                    Binding groupBinding = PortalLDAPUtil.getGroup(
077                            ldapServerId, userGroup.getCompanyId(), userGroup.getName());
078    
079                    if (groupBinding != null) {
080                            return PortalLDAPUtil.getNameInNamespace(
081                                    ldapServerId, userGroup.getCompanyId(), groupBinding);
082                    }
083    
084                    StringBundler sb = new StringBundler(5);
085    
086                    sb.append(
087                            GetterUtil.getString(
088                                    groupMappings.getProperty(_groupDNFieldName), _DEFAULT_DN));
089                    sb.append(StringPool.EQUAL);
090                    sb.append(userGroup.getName());
091                    sb.append(StringPool.COMMA);
092                    sb.append(
093                            PortalLDAPUtil.getGroupsDN(ldapServerId, userGroup.getCompanyId()));
094    
095                    return sb.toString();
096            }
097    
098            @Override
099            public Modifications getLDAPContactModifications(
100                            Contact contact, Map<String, Serializable> contactExpandoAttributes,
101                            Properties contactMappings, Properties contactExpandoMappings)
102                    throws Exception {
103    
104                    if (contactMappings.isEmpty() && contactExpandoMappings.isEmpty()) {
105                            return null;
106                    }
107    
108                    Modifications modifications = getModifications(
109                            contact, contactMappings, _reservedContactFieldNames);
110    
111                    populateCustomAttributeModifications(
112                            contact, contact.getExpandoBridge(), contactExpandoAttributes,
113                            contactExpandoMappings, modifications);
114    
115                    return modifications;
116            }
117    
118            @Override
119            public Attributes getLDAPGroupAttributes(
120                            long ldapServerId, UserGroup userGroup, User user,
121                            Properties groupMappings, Properties userMappings)
122                    throws Exception {
123    
124                    Attributes attributes = new BasicAttributes(true);
125    
126                    Attribute objectClass = new BasicAttribute(_OBJECT_CLASS);
127    
128                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
129    
130                    String[] defaultObjectClasses = PrefsPropsUtil.getStringArray(
131                            userGroup.getCompanyId(),
132                            PropsKeys.LDAP_GROUP_DEFAULT_OBJECT_CLASSES + postfix,
133                            StringPool.COMMA);
134    
135                    for (int i = 0; i < defaultObjectClasses.length; i++) {
136                            objectClass.add(defaultObjectClasses[i]);
137                    }
138    
139                    attributes.put(objectClass);
140    
141                    addAttributeMapping(
142                            groupMappings.getProperty(GroupConverterKeys.GROUP_NAME),
143                            userGroup.getName(), attributes);
144                    addAttributeMapping(
145                            groupMappings.getProperty(GroupConverterKeys.DESCRIPTION),
146                            userGroup.getDescription(), attributes);
147                    addAttributeMapping(
148                            groupMappings.getProperty(GroupConverterKeys.USER),
149                            getUserDNName(ldapServerId, user, userMappings), attributes);
150    
151                    return attributes;
152            }
153    
154            @Override
155            public Modifications getLDAPGroupModifications(
156                            long ldapServerId, UserGroup userGroup, User user,
157                            Properties groupMappings, Properties userMappings,
158                            LDAPOperation ldapOperation)
159                    throws Exception {
160    
161                    Modifications modifications = Modifications.getInstance();
162    
163                    String groupDN = getGroupDNName(ldapServerId, userGroup, groupMappings);
164                    String userDN = getUserDNName(ldapServerId, user, userMappings);
165    
166                    if (PortalLDAPUtil.isGroupMember(
167                                    ldapServerId, user.getCompanyId(), groupDN, userDN)) {
168    
169                            if (ldapOperation == LDAPOperation.REMOVE) {
170                                    modifications.addItem(
171                                            DirContext.REMOVE_ATTRIBUTE,
172                                            groupMappings.getProperty(GroupConverterKeys.USER), userDN);
173                            }
174                    }
175                    else {
176                            if (ldapOperation == LDAPOperation.ADD) {
177                                    modifications.addItem(
178                                            DirContext.ADD_ATTRIBUTE,
179                                            groupMappings.getProperty(GroupConverterKeys.USER), userDN);
180                            }
181                    }
182    
183                    return modifications;
184            }
185    
186            @Override
187            public Attributes getLDAPUserAttributes(
188                            long ldapServerId, User user, Properties userMappings)
189                    throws SystemException {
190    
191                    Attributes attributes = new BasicAttributes(true);
192    
193                    Attribute objectClass = new BasicAttribute(_OBJECT_CLASS);
194    
195                    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
196    
197                    String[] defaultObjectClasses = PrefsPropsUtil.getStringArray(
198                            user.getCompanyId(),
199                            PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES + postfix,
200                            StringPool.COMMA);
201    
202                    for (int i = 0; i < defaultObjectClasses.length; i++) {
203                            objectClass.add(defaultObjectClasses[i]);
204                    }
205    
206                    attributes.put(objectClass);
207    
208                    addAttributeMapping(
209                            userMappings.getProperty(UserConverterKeys.UUID), user.getUuid(),
210                            attributes);
211                    addAttributeMapping(
212                            userMappings.getProperty(UserConverterKeys.SCREEN_NAME),
213                            user.getScreenName(), attributes);
214                    addAttributeMapping(
215                            userMappings.getProperty(UserConverterKeys.PASSWORD),
216                            getEncryptedPasswordForLDAP(user), attributes);
217                    addAttributeMapping(
218                            userMappings.getProperty(UserConverterKeys.EMAIL_ADDRESS),
219                            user.getEmailAddress(), attributes);
220                    addAttributeMapping(
221                            userMappings.getProperty(UserConverterKeys.FULL_NAME),
222                            user.getFullName(), attributes);
223                    addAttributeMapping(
224                            userMappings.getProperty(UserConverterKeys.FIRST_NAME),
225                            user.getFirstName(), attributes);
226                    addAttributeMapping(
227                            userMappings.getProperty(UserConverterKeys.MIDDLE_NAME),
228                            user.getMiddleName(), attributes);
229                    addAttributeMapping(
230                            userMappings.getProperty(UserConverterKeys.LAST_NAME),
231                            user.getLastName(), attributes);
232                    addAttributeMapping(
233                            userMappings.getProperty(UserConverterKeys.JOB_TITLE),
234                            user.getJobTitle(), attributes);
235                    addAttributeMapping(
236                            userMappings.getProperty(UserConverterKeys.PORTRAIT),
237                            getUserPortrait(user), attributes);
238                    addAttributeMapping(
239                            userMappings.getProperty(UserConverterKeys.STATUS),
240                            String.valueOf(user.getStatus()), attributes);
241    
242                    return attributes;
243            }
244    
245            @Override
246            public Modifications getLDAPUserGroupModifications(
247                            long ldapServerId, List<UserGroup> userGroups, User user,
248                            Properties userMappings)
249                    throws Exception {
250    
251                    Modifications modifications = Modifications.getInstance();
252    
253                    String groupMappingAttributeName = userMappings.getProperty(
254                            UserConverterKeys.GROUP);
255    
256                    if (Validator.isNull(groupMappingAttributeName)) {
257                            return modifications;
258                    }
259    
260                    Properties groupMappings = LDAPSettingsUtil.getGroupMappings(
261                            ldapServerId, user.getCompanyId());
262    
263                    String userDN = getUserDNName(ldapServerId, user, userMappings);
264    
265                    for (UserGroup userGroup : userGroups) {
266                            String groupDN = getGroupDNName(
267                                    ldapServerId, userGroup, groupMappings);
268    
269                            if (PortalLDAPUtil.isUserGroupMember(
270                                            ldapServerId, user.getCompanyId(), groupDN, userDN)) {
271    
272                                    continue;
273                            }
274    
275                            modifications.addItem(
276                                    DirContext.ADD_ATTRIBUTE, groupMappingAttributeName, groupDN);
277                    }
278    
279                    return modifications;
280            }
281    
282            @Override
283            public Modifications getLDAPUserModifications(
284                            User user, Map<String, Serializable> userExpandoAttributes,
285                            Properties userMappings, Properties userExpandoMappings)
286                    throws Exception {
287    
288                    Modifications modifications = getModifications(
289                            user, userMappings, _reservedUserFieldNames);
290    
291                    if (user.isPasswordModified() &&
292                            Validator.isNotNull(user.getPasswordUnencrypted())) {
293    
294                            String newPassword = getEncryptedPasswordForLDAP(user);
295    
296                            String passwordKey = userMappings.getProperty(
297                                    UserConverterKeys.PASSWORD);
298    
299                            if (passwordKey.equals("unicodePwd")) {
300                                    String newQuotedPassword = StringPool.QUOTE.concat(
301                                            newPassword).concat(StringPool.QUOTE);
302    
303                                    byte[] newUnicodePassword = newQuotedPassword.getBytes(
304                                            "UTF-16LE");
305    
306                                    addModificationItem(
307                                            new BasicAttribute(passwordKey, newUnicodePassword),
308                                            modifications);
309                            }
310                            else {
311                                    addModificationItem(passwordKey, newPassword, modifications);
312                            }
313                    }
314    
315                    String portraitKey = userMappings.getProperty(
316                            UserConverterKeys.PORTRAIT);
317    
318                    if (Validator.isNotNull(portraitKey)) {
319                            addModificationItem(
320                                    new BasicAttribute(portraitKey, getUserPortrait(user)),
321                                    modifications);
322                    }
323    
324                    populateCustomAttributeModifications(
325                            user, user.getExpandoBridge(), userExpandoAttributes,
326                            userExpandoMappings, modifications);
327    
328                    return modifications;
329            }
330    
331            @Override
332            public String getUserDNName(
333                            long ldapServerId, User user, Properties userMappings)
334                    throws Exception {
335    
336                    Binding userBinding = PortalLDAPUtil.getUser(
337                            ldapServerId, user.getCompanyId(), user.getScreenName(),
338                            user.getEmailAddress());
339    
340                    if (userBinding != null) {
341                            return PortalLDAPUtil.getNameInNamespace(
342                                    ldapServerId, user.getCompanyId(), userBinding);
343                    }
344    
345                    StringBundler sb = new StringBundler(5);
346    
347                    sb.append(
348                            GetterUtil.getString(
349                                    userMappings.getProperty(_userDNFieldName), _DEFAULT_DN));
350                    sb.append(StringPool.EQUAL);
351                    sb.append(PropertyUtils.getProperty(user, _userDNFieldName));
352                    sb.append(StringPool.COMMA);
353                    sb.append(PortalLDAPUtil.getUsersDN(ldapServerId, user.getCompanyId()));
354    
355                    return sb.toString();
356            }
357    
358            public void setContactReservedFieldNames(
359                    List<String> reservedContactFieldNames) {
360    
361                    for (String reservedContactFieldName : reservedContactFieldNames) {
362                            _reservedContactFieldNames.put(
363                                    reservedContactFieldName, reservedContactFieldName);
364                    }
365            }
366    
367            public void setUserDNFieldName(String userDNFieldName) {
368                    _userDNFieldName = userDNFieldName;
369            }
370    
371            public void setUserReservedFieldNames(List<String> reservedUserFieldNames) {
372                    for (String reservedUserFieldName : reservedUserFieldNames) {
373                            _reservedUserFieldNames.put(
374                                    reservedUserFieldName, reservedUserFieldName);
375                    }
376            }
377    
378            protected void addAttributeMapping(
379                    String attributeName, Object attributeValue, Attributes attributes) {
380    
381                    if (Validator.isNotNull(attributeName) && (attributeValue != null)) {
382                            attributes.put(attributeName, attributeValue);
383                    }
384            }
385    
386            protected void addAttributeMapping(
387                    String attributeName, String attributeValue, Attributes attributes) {
388    
389                    if (Validator.isNotNull(attributeName) &&
390                            Validator.isNotNull(attributeValue)) {
391    
392                            attributes.put(attributeName, attributeValue);
393                    }
394            }
395    
396            protected void addModificationItem(
397                    BasicAttribute basicAttribute, Modifications modifications) {
398    
399                    if (Validator.isNotNull(basicAttribute)) {
400                            modifications.addItem(basicAttribute);
401                    }
402            }
403    
404            protected void addModificationItem(
405                    String attributeName, String attributeValue,
406                    Modifications modifications) {
407    
408                    if (Validator.isNotNull(attributeName)) {
409                            modifications.addItem(attributeName, attributeValue);
410                    }
411            }
412    
413            protected String getEncryptedPasswordForLDAP(User user)
414                    throws SystemException {
415    
416                    String password = user.getPasswordUnencrypted();
417    
418                    if (Validator.isNull(password)) {
419                            return password;
420                    }
421    
422                    String algorithm = PrefsPropsUtil.getString(
423                            user.getCompanyId(),
424                            PropsKeys.LDAP_AUTH_PASSWORD_ENCRYPTION_ALGORITHM);
425    
426                    if (Validator.isNull(algorithm)) {
427                            return password;
428                    }
429    
430                    try {
431                            StringBundler sb = new StringBundler(4);
432    
433                            if (!algorithm.equals(PasswordEncryptorUtil.TYPE_NONE)) {
434                                    sb.append(StringPool.OPEN_CURLY_BRACE);
435                                    sb.append(algorithm);
436                                    sb.append(StringPool.CLOSE_CURLY_BRACE);
437                            }
438    
439                            sb.append(PasswordEncryptorUtil.encrypt(algorithm, password, null));
440    
441                            return sb.toString();
442                    }
443                    catch (PwdEncryptorException pee) {
444                            throw new SystemException(pee);
445                    }
446            }
447    
448            protected Modifications getModifications(
449                    Object object, Properties objectMappings,
450                    Map<String, String> reservedFieldNames) {
451    
452                    Modifications modifications = Modifications.getInstance();
453    
454                    for (Map.Entry<Object, Object> entry : objectMappings.entrySet()) {
455                            String fieldName = (String)entry.getKey();
456    
457                            if (reservedFieldNames.containsKey(fieldName)) {
458                                    continue;
459                            }
460    
461                            String ldapAttributeName = (String)entry.getValue();
462    
463                            try {
464                                    Object attributeValue = PropertyUtils.getProperty(
465                                            object, fieldName);
466    
467                                    if (attributeValue != null) {
468                                            addModificationItem(
469                                                    ldapAttributeName, attributeValue.toString(),
470                                                    modifications);
471                                    }
472                            }
473                            catch (Exception e) {
474                                    if (_log.isWarnEnabled()) {
475                                            _log.warn(
476                                                    "Unable to map field " + fieldName + " to class " +
477                                                            object.getClass(),
478                                                    e);
479                                    }
480                            }
481                    }
482    
483                    return modifications;
484            }
485    
486            protected byte[] getUserPortrait(User user) {
487                    byte[] bytes = null;
488    
489                    if (user.getPortraitId() == 0) {
490                            return bytes;
491                    }
492    
493                    Image image = null;
494    
495                    try {
496                            image = ImageLocalServiceUtil.getImage(user.getPortraitId());
497    
498                            if (image != null) {
499                                    bytes = image.getTextObj();
500                            }
501                    }
502                    catch (Exception e) {
503                            if (_log.isWarnEnabled()) {
504                                    _log.warn(
505                                            "Unable to get the portrait for user " + user.getUserId(),
506                                            e);
507                            }
508                    }
509    
510                    return bytes;
511            }
512    
513            protected void populateCustomAttributeModifications(
514                    Object object, ExpandoBridge expandoBridge,
515                    Map<String, Serializable> expandoAttributes, Properties expandoMappings,
516                    Modifications modifications) {
517    
518                    if ((expandoAttributes == null) || expandoAttributes.isEmpty()) {
519                            return;
520                    }
521    
522                    for (Map.Entry<Object, Object> entry : expandoMappings.entrySet()) {
523                            String fieldName = (String)entry.getKey();
524                            String ldapAttributeName = (String)entry.getValue();
525    
526                            Serializable fieldValue = expandoAttributes.get(fieldName);
527    
528                            if (fieldValue == null) {
529                                    continue;
530                            }
531    
532                            try {
533                                    int type = expandoBridge.getAttributeType(fieldName);
534    
535                                    String value = ExpandoConverterUtil.getStringFromAttribute(
536                                            type, fieldValue);
537    
538                                    addModificationItem(ldapAttributeName, value, modifications);
539                            }
540                            catch (Exception e) {
541                                    if (_log.isWarnEnabled()) {
542                                            _log.warn(
543                                                    "Unable to map field " + fieldName + " to class " +
544                                                            object.getClass(),
545                                                    e);
546                                    }
547                            }
548                    }
549            }
550    
551            private static final String _DEFAULT_DN = "cn";
552    
553            private static final String _OBJECT_CLASS = "objectclass";
554    
555            private static Log _log = LogFactoryUtil.getLog(
556                    DefaultPortalToLDAPConverter.class);
557    
558            private String _groupDNFieldName = GroupConverterKeys.GROUP_NAME;
559            private Map<String, String> _reservedContactFieldNames =
560                    new HashMap<String, String>();
561            private Map<String, String> _reservedUserFieldNames =
562                    new HashMap<String, String>();
563            private String _userDNFieldName = UserConverterKeys.SCREEN_NAME;
564    
565    }