001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.model;
016    
017    import com.liferay.portal.ModelListenerException;
018    import com.liferay.portal.security.ldap.LDAPUserTransactionThreadLocal;
019    import com.liferay.portal.security.ldap.PortalLDAPExporterUtil;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.portal.service.ServiceContextThreadLocal;
022    
023    import java.io.Serializable;
024    
025    import java.util.Map;
026    
027    /**
028     * @author Scott Lee
029     * @author Brian Wing Shun Chan
030     * @author Raymond Augé
031     */
032    public class UserListener extends BaseModelListener<User> {
033    
034            public void onAfterCreate(User user) throws ModelListenerException {
035                    try {
036                            exportToLDAP(user);
037                    }
038                    catch (Exception e) {
039                            throw new ModelListenerException(e);
040                    }
041            }
042    
043            public void onAfterUpdate(User user) throws ModelListenerException {
044                    try {
045                            exportToLDAP(user);
046                    }
047                    catch (Exception e) {
048                            throw new ModelListenerException(e);
049                    }
050            }
051    
052            protected void exportToLDAP(User user) throws Exception {
053                    if (user.isDefaultUser() ||
054                            LDAPUserTransactionThreadLocal.isOriginatesFromLDAP()) {
055    
056                            return;
057                    }
058    
059                    ServiceContext serviceContext =
060                            ServiceContextThreadLocal.getServiceContext();
061    
062                    Map<String, Serializable> expandoBridgeAttributes = null;
063    
064                    if (serviceContext != null) {
065                            expandoBridgeAttributes =
066                                    serviceContext.getExpandoBridgeAttributes();
067                    }
068    
069                    PortalLDAPExporterUtil.exportToLDAP(user, expandoBridgeAttributes);
070            }
071    
072    }