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.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    }