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.security.ldap.LDAPOperation;
019    import com.liferay.portal.security.ldap.LDAPUserTransactionThreadLocal;
020    import com.liferay.portal.security.ldap.PortalLDAPExporterUtil;
021    
022    /**
023     * @author Marcellus Tavares
024     */
025    public class UserGroupListener extends BaseModelListener<UserGroup> {
026    
027            @Override
028            public void onAfterAddAssociation(
029                            Object userGroupId, String associationClassName,
030                            Object associationClassPK)
031                    throws ModelListenerException {
032    
033                    try {
034                            if (associationClassName.equals(User.class.getName())) {
035                                    exportToLDAP(
036                                            (Long)associationClassPK, (Long)userGroupId,
037                                            LDAPOperation.ADD);
038                            }
039                    }
040                    catch (Exception e) {
041                            throw new ModelListenerException(e);
042                    }
043            }
044    
045            @Override
046            public void onAfterRemoveAssociation(
047                            Object userGroupId, String associationClassName,
048                            Object associationClassPK)
049                    throws ModelListenerException {
050    
051                    try {
052                            if (associationClassName.equals(User.class.getName())) {
053                                    exportToLDAP(
054                                            (Long)associationClassPK, (Long)userGroupId,
055                                            LDAPOperation.REMOVE);
056                            }
057                    }
058                    catch (Exception e) {
059                            throw new ModelListenerException(e);
060                    }
061            }
062    
063            protected void exportToLDAP(
064                            long userId, long userGroupId, LDAPOperation ldapOperation)
065                    throws Exception {
066    
067                    if (LDAPUserTransactionThreadLocal.isOriginatesFromLDAP()) {
068                            return;
069                    }
070    
071                    PortalLDAPExporterUtil.exportToLDAP(userId, userGroupId, ldapOperation);
072            }
073    
074    }