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.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.security.ldap.LDAPOperation;
020    import com.liferay.portal.security.ldap.LDAPUserTransactionThreadLocal;
021    import com.liferay.portal.security.ldap.PortalLDAPExporterUtil;
022    
023    /**
024     * @author Marcellus Tavares
025     */
026    public class UserGroupListener extends BaseModelListener<UserGroup> {
027    
028            @Override
029            public void onAfterAddAssociation(
030                    Object userGroupId, String associationClassName,
031                    Object associationClassPK) {
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                            _log.error(
042                                    "Unable to export user group with user ID " +
043                                            associationClassPK + " to LDAP on after add association",
044                                    e);
045                    }
046            }
047    
048            @Override
049            public void onAfterRemoveAssociation(
050                    Object userGroupId, String associationClassName,
051                    Object associationClassPK) {
052    
053                    try {
054                            if (associationClassName.equals(User.class.getName())) {
055                                    exportToLDAP(
056                                            (Long)associationClassPK, (Long)userGroupId,
057                                            LDAPOperation.REMOVE);
058                            }
059                    }
060                    catch (Exception e) {
061                            _log.error(
062                                    "Unable to export user group with user ID " +
063                                            associationClassPK +
064                                                    " to LDAP on after remove association",
065                                    e);
066                    }
067            }
068    
069            protected void exportToLDAP(
070                            long userId, long userGroupId, LDAPOperation ldapOperation)
071                    throws Exception {
072    
073                    if (LDAPUserTransactionThreadLocal.isOriginatesFromLDAP()) {
074                            return;
075                    }
076    
077                    PortalLDAPExporterUtil.exportToLDAP(userId, userGroupId, ldapOperation);
078            }
079    
080            private static Log _log = LogFactoryUtil.getLog(UserGroupListener.class);
081    
082    }