1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.model;
16  
17  import com.liferay.portal.ModelListenerException;
18  import com.liferay.portal.security.ldap.LDAPUserTransactionThreadLocal;
19  import com.liferay.portal.security.ldap.PortalLDAPExporterUtil;
20  import com.liferay.portal.service.ServiceContext;
21  import com.liferay.portal.service.ServiceContextThreadLocal;
22  
23  import java.io.Serializable;
24  
25  import java.util.Map;
26  
27  /**
28   * <a href="UserListener.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Scott Lee
31   * @author Brian Wing Shun Chan
32   * @author Raymond Augé
33   */
34  public class UserListener extends BaseModelListener<User> {
35  
36      public void onAfterCreate(User user) throws ModelListenerException {
37          try {
38              exportToLDAP(user);
39          }
40          catch (Exception e) {
41              throw new ModelListenerException(e);
42          }
43      }
44  
45      public void onAfterUpdate(User user) throws ModelListenerException {
46          try {
47              exportToLDAP(user);
48          }
49          catch (Exception e) {
50              throw new ModelListenerException(e);
51          }
52      }
53  
54      protected void exportToLDAP(User user) throws Exception {
55          if (user.isDefaultUser() ||
56              LDAPUserTransactionThreadLocal.isOriginatesFromLDAP()) {
57  
58              return;
59          }
60  
61          ServiceContext serviceContext =
62              ServiceContextThreadLocal.getServiceContext();
63  
64          Map<String, Serializable> expandoBridgeAttributes = null;
65  
66          if (serviceContext != null) {
67              expandoBridgeAttributes =
68                  serviceContext.getExpandoBridgeAttributes();
69          }
70  
71          PortalLDAPExporterUtil.exportToLDAP(user, expandoBridgeAttributes);
72      }
73  
74  }