001    /**
002     * Copyright (c) 2000-2011 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.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                    }
118            }
119    
120    }