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