--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie.listeners;
+
+import java.util.List;
+
+import javax.naming.Binding;
+import javax.naming.directory.Attributes;
+import javax.naming.ldap.LdapContext;
+
+import org.springframework.security.providers.ldap.authenticator.LdapShaPasswordEncoder;
+
+import com.liferay.portal.ModelListenerException;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.model.ModelListener;
+import com.liferay.portal.model.User;
+import com.liferay.portal.security.ldap.PortalLDAPUtil;
+import com.pentila.entSavoie.internalMessaging.model.InternalFolderMessage;
+import com.pentila.entSavoie.internalMessaging.model.InternalMessage;
+import com.pentila.entSavoie.internalMessaging.service.InternalFolderMessageLocalServiceUtil;
+import com.pentila.entSavoie.internalMessaging.service.InternalMessageLocalServiceUtil;
+import com.pentila.entSavoie.userProperties.NoSuchNotifyConfigException;
+import com.pentila.entSavoie.userProperties.service.NotifyConfigLocalServiceUtil;
+import com.pentila.entSavoie.userProperties.service.UserPropertiesLocalServiceUtil;
+import com.pentila.entSavoie.utils.XmppUtil;
+
+/**
+ * @author Lancelot SIX
+ *
+ *
+ * Ce listener est destiné à être appelé après chaque modification d'un objet
+ * User. Si le mot de passe de cet utilisateur à été changé, alors, celui-ci
+ * est exporté vers la base LDAP pour pouvoir est utilisable lors de la prochaine
+ * connection.
+ *
+ */
+public class UserListener implements ModelListener<User> {
+
+ public void onAfterAddAssociation(Object arg0, String arg1, Object arg2)
+ throws ModelListenerException {}
+
+ public void onAfterCreate(User user) throws ModelListenerException {}
+
+ public void onAfterRemove(User user) throws ModelListenerException {}
+
+ public void onAfterRemoveAssociation(Object arg0, String arg1, Object arg2)
+ throws ModelListenerException {}
+
+ public void onAfterUpdate(User user) throws ModelListenerException {
+
+// try {
+// if (true) {
+// //If we can access the password on an unencrypted form, then we export
+// //it to the ldap.
+// if (user.getPasswordUnencrypted()!=null) {
+//
+// System.out.println("onAfterUpdate UserListener");
+// System.out.println("user pwd reset: " + user.isPasswordReset());
+//
+// //Recupere l'user dans le lda
+// Binding ldapUser = PortalLDAPUtil.getUser(user.getCompanyId(), user.getScreenName());
+// System.out.println("+++++ after get user in LDAP1");
+// final String userDN = PortalLDAPUtil.getNameInNamespace(user.getCompanyId(), ldapUser);
+// System.out.println("+++++ after get user in LDAP2 : userDN = "+userDN);
+//
+// final LdapContext ctx = PortalLDAPUtil.getContext(user.getCompanyId());
+// System.out.println("+++++ after get context");
+// try {
+// Attributes userAttrs = ctx.getAttributes(userDN);
+// userAttrs.remove("userPassword");
+// System.out.println("+++++ after removing userPassword attribute");
+// System.out.println("+++++ unencrypted password = "+user.getPasswordUnencrypted());
+// String encryptedPassword = encryptPassword(user.getPasswordUnencrypted());
+// System.out.println("+++++ encrypted password = "+encryptedPassword);
+//
+// userAttrs.put("userPassword", encryptedPassword);
+// ctx.rebind(userDN, null, userAttrs);
+// } finally {
+// if (ctx != null) {
+// ctx.close();
+// }
+// }
+//
+// if (_log.isDebugEnabled()) {
+// _log.debug("Exporting new password to ldap for user "+user.getScreenName());
+// }
+//
+// }
+// System.out.println("+++++ Exported password");
+// }
+// } catch (Exception e) {
+// e.printStackTrace();
+// if (_log.isInfoEnabled()) {
+// _log.info(e);
+// }
+// throw new ModelListenerException (e);
+// }
+ }
+
+ public void onBeforeAddAssociation(Object arg0, String arg1, Object arg2)
+ throws ModelListenerException {}
+
+ public void onBeforeCreate(User arg0) throws ModelListenerException {}
+
+ public void onBeforeRemove(User user) throws ModelListenerException {
+
+ // All this code is now included in the tool UserCleanup or the scheduled task UserPurgeImpl
+
+ }
+
+ public void onBeforeRemoveAssociation(Object arg0, String arg1, Object arg2)
+ throws ModelListenerException {}
+
+ public void onBeforeUpdate(User arg0) throws ModelListenerException {}
+
+ protected String encryptPassword(String pass) {
+ LdapShaPasswordEncoder encoder = new LdapShaPasswordEncoder();
+ return encoder.encodePassword(pass, null);
+ }
+
+ private Log _log = LogFactoryUtil.getLog(this.getClass());
+
+}