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