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.model;
016    
017    import com.liferay.portal.ModelListenerException;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.model.impl.UserModelImpl;
021    import com.liferay.portal.security.auth.PrincipalThreadLocal;
022    import com.liferay.portal.security.ldap.LDAPUserTransactionThreadLocal;
023    import com.liferay.portal.security.ldap.PortalLDAPExporterUtil;
024    import com.liferay.portal.service.MembershipRequestLocalServiceUtil;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextThreadLocal;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    
029    import java.io.Serializable;
030    
031    import java.util.List;
032    import java.util.Map;
033    
034    /**
035     * @author Scott Lee
036     * @author Brian Wing Shun Chan
037     * @author Raymond Aug??
038     * @author Vilmos Papp
039     */
040    public class UserListener extends BaseModelListener<User> {
041    
042            @Override
043            public void onAfterAddAssociation(
044                            Object classPK, String associationClassName,
045                            Object associationClassPK)
046                    throws ModelListenerException {
047    
048                    try {
049                            if (associationClassName.equals(Group.class.getName())) {
050                                    long userId = ((Long)classPK).longValue();
051                                    long groupId = ((Long)associationClassPK).longValue();
052    
053                                    updateMembershipRequestStatus(userId, groupId);
054                            }
055                    }
056                    catch (Exception e) {
057                            throw new ModelListenerException(e);
058                    }
059            }
060    
061            @Override
062            public void onAfterCreate(User user) throws ModelListenerException {
063                    try {
064                            exportToLDAP(user);
065                    }
066                    catch (Exception e) {
067                            throw new ModelListenerException(e);
068                    }
069            }
070    
071            @Override
072            public void onAfterUpdate(User user) throws ModelListenerException {
073                    try {
074                            exportToLDAP(user);
075                    }
076                    catch (Exception e) {
077                            throw new ModelListenerException(e);
078                    }
079            }
080    
081            @Override
082            public void onBeforeUpdate(User user) {
083                    UserModelImpl userModelImpl = (UserModelImpl)user;
084    
085                    LDAPUserTransactionThreadLocal.setOriginalEmailAddress(
086                            userModelImpl.getOriginalEmailAddress());
087            }
088    
089            protected void exportToLDAP(User user) throws Exception {
090                    if (user.isDefaultUser() ||
091                            LDAPUserTransactionThreadLocal.isOriginatesFromLDAP()) {
092    
093                            return;
094                    }
095    
096                    ServiceContext serviceContext =
097                            ServiceContextThreadLocal.getServiceContext();
098    
099                    Map<String, Serializable> expandoBridgeAttributes = null;
100    
101                    if (serviceContext != null) {
102                            expandoBridgeAttributes =
103                                    serviceContext.getExpandoBridgeAttributes();
104                    }
105    
106                    PortalLDAPExporterUtil.exportToLDAP(user, expandoBridgeAttributes);
107            }
108    
109            protected void updateMembershipRequestStatus(long userId, long groupId)
110                    throws Exception {
111    
112                    long principalUserId = GetterUtil.getLong(
113                            PrincipalThreadLocal.getName());
114    
115                    User user = UserLocalServiceUtil.getUser(userId);
116    
117                    List<MembershipRequest> membershipRequests =
118                            MembershipRequestLocalServiceUtil.getMembershipRequests(
119                                    userId, groupId, MembershipRequestConstants.STATUS_PENDING);
120    
121                    for (MembershipRequest membershipRequest : membershipRequests) {
122                            MembershipRequestLocalServiceUtil.updateStatus(
123                                    principalUserId, membershipRequest.getMembershipRequestId(),
124                                    LanguageUtil.get(
125                                            user.getLocale(), "your-membership-has-been-approved"),
126                                    MembershipRequestConstants.STATUS_APPROVED, false,
127                                    new ServiceContext());
128                    }
129            }
130    
131    }