--- /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.userProperties.action;
+
+import java.math.BigInteger;
+import java.security.SecureRandom;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import javax.mail.internet.InternetAddress;
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.PortletConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import net.tanesha.recaptcha.ReCaptchaImpl;
+import net.tanesha.recaptcha.ReCaptchaResponse;
+
+import org.apache.struts.action.ActionForm;
+import org.apache.struts.action.ActionForward;
+import org.apache.struts.action.ActionMapping;
+
+import com.liferay.mail.service.MailServiceUtil;
+import com.liferay.portal.kernel.mail.MailMessage;
+import com.liferay.portal.kernel.util.ParamUtil;
+import com.liferay.portal.model.User;
+import com.liferay.portal.security.ldap.PasswordUtil;
+import com.liferay.portal.service.CompanyLocalServiceUtil;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portal.struts.ActionConstants;
+import com.liferay.portal.struts.PortletAction;
+import com.liferay.portal.util.PortalUtil;
+import com.pentila.entSavoie.userProperties.model.UserProperties;
+import com.pentila.entSavoie.userProperties.service.UserPropertiesLocalServiceUtil;
+import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
+
+/**
+ * Demande de recuperation de mot de passe
+ */
+public class ResetPasswordAction extends PortletAction {
+
+ public ActionForward strutsExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
+
+ try {
+ String key = ParamUtil.getString(request, "key");
+
+ String params = "";
+ if(!key.isEmpty()){
+ params = resetPassword(key);
+ } else {
+ String challenge = ParamUtil.getString(request, "recaptcha_challenge_field", "");
+ String uresponse = ParamUtil.getString(request, "recaptcha_response_field", "");
+ String login = ParamUtil.getString(request, "login", "").toLowerCase();
+ String mail = ParamUtil.getString(request, "mail", "").toLowerCase();
+
+ String remoteAddr = request.getRemoteAddr();
+
+ ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
+ reCaptcha.setPrivateKey(ENTMainUtilsLocalServiceUtil.getEntCaptchaPrivateKey());
+
+ ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
+
+ if (reCaptchaResponse.isValid()) {
+ params = sendKeyPasswordRecovery(login, mail, remoteAddr);
+ } else {
+ params = "?incorrect_captcha=true&mail=" + mail + "&login=" + login;
+ }
+ }
+
+ response.sendRedirect(ENTMainUtilsLocalServiceUtil.getEntResetPasswordUrl() + params);
+
+ return null;
+ }
+ catch (Exception e) {
+ PortalUtil.sendError(e, request, response);
+
+ return null;
+ }
+ }
+
+ public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
+ try {
+ String key = ParamUtil.getString(actionRequest, "key");
+
+ String params = "";
+ if(!key.isEmpty()){
+ params = resetPassword(key);
+ } else {
+ String challenge = ParamUtil.getString(actionRequest, "recaptcha_challenge_field", "");
+ String uresponse = ParamUtil.getString(actionRequest, "recaptcha_response_field", "");
+ String login = ParamUtil.getString(actionRequest, "login", "").toLowerCase();
+ String mail = ParamUtil.getString(actionRequest, "mail", "").toLowerCase();
+
+ String remoteAddr = PortalUtil.getHttpServletRequest(actionRequest).getRemoteAddr();
+
+ ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
+ reCaptcha.setPrivateKey(ENTMainUtilsLocalServiceUtil.getEntCaptchaPrivateKey());
+
+ ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
+
+ if (reCaptchaResponse.isValid()) {
+ params = sendKeyPasswordRecovery(login, mail, remoteAddr);
+ } else {
+ params = "?incorrect_captcha=true&mail=" + mail + "&login=" + login;
+ }
+ }
+
+ actionResponse.sendRedirect(ENTMainUtilsLocalServiceUtil.getEntResetPasswordUrl() + params);
+
+ setForward(actionRequest, ActionConstants.COMMON_NULL);
+ }
+ catch (Exception e) {
+ PortalUtil.sendError(e, actionRequest, actionResponse);
+ }
+ }
+
+ private String sendKeyPasswordRecovery(String login, String mail, String ip) throws Exception {
+ long companyId = CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId();
+ User user = null;
+
+ // Recupertation de l'utilisateur a partir de son login
+ boolean incorrectLogin = false;
+ if(!login.isEmpty()){
+ try {
+ user = UserLocalServiceUtil.getUserByScreenName(companyId, login);
+ } catch(Exception e){
+ // Le login entre est incorrect
+ incorrectLogin = true;
+ }
+ }
+
+ // Recupertation de l'utilisateur a partir de son adresse mail de recuperation
+ if(user == null){
+ if(!mail.isEmpty()){
+ try {
+ long userId = UserPropertiesLocalServiceUtil.getUserPropertiesByMailPasswordRecovery(mail).getUserId();
+ user = UserLocalServiceUtil.getUser(userId);
+ } catch(Exception e){
+ // Le mail entre est incorrect
+ return "?incorrect_mail=true&mail=" + mail + (incorrectLogin?"&incorrect_login=true&login=" + login:"") ;
+ }
+ } else {
+ return "?incorrect_login=true&login=" + login;
+ }
+ }
+
+ if(user != null){
+ mail = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(user.getUserId()).getMailPasswordRecovery();
+
+ // Obtention de l'adresse mail de recupertaion du mot de passe
+ UserProperties up = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(user.getUserId());
+ mail = up.getMailPasswordRecovery();
+
+ if(mail.isEmpty()){
+ return "?success=false&incorrect_rescue_mail=true";
+ }
+
+ // Set de la key permettant de recuperer l'identifiant et le mot de passe
+ SecureRandom random = new SecureRandom();
+ String key = new BigInteger(130, random).toString(32);
+ up.setKeyPasswordRecovery(key);
+ UserPropertiesLocalServiceUtil.updateUserProperties(up, false);
+
+ // Envoi du mot de passe a l'utilisateur
+ String noReplyMail = ENTMainUtilsLocalServiceUtil.getMailNoReply(companyId);
+
+ InternetAddress iaTo = new InternetAddress(mail);
+ InternetAddress iaFrom = new InternetAddress(noReplyMail);
+
+ MailMessage mailMessage = new MailMessage();
+ mailMessage.setFrom(iaFrom);
+ mailMessage.setTo(iaTo);
+
+ mailMessage.setHTMLFormat(true);
+
+ String entPlateformName = ENTMainUtilsLocalServiceUtil.getEntPlateformName();
+ String subject = "ENT " + entPlateformName + " : Réinitialisation de votre mot de passe (1/2)";
+ mailMessage.setSubject(subject);
+
+ SimpleDateFormat frenchDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
+ String body = "Bonjour " + user.getFullName() + ", </br></br>" +
+ "Pour réinitialiser votre mot de passe et obtenir vos nouveaux identifiants de connexion sur la plateforme " + entPlateformName + ", veuillez cliquer sur le lien ci-dessous : <br/>" +
+ "<a href='" + PortalUtil.getPortalProperties().getProperty("absolute.url") + "/c/userProperties/reset_password?key=" + key + "'>Réinitialiser mes identifiants de connexion</a> <br/>" +
+ "Un message vous sera transmis avec vos nouvelles données personnelles de connexion. <br/>" +
+ "Ce lien est unique et ne pourra pas être réutulisé. <br/><br/>" +
+ "<i>Si vous n'êtes pas l'initiateur de la demande de réinitialisation de votre mot de passe, merci de ne pas prendre en compte ce message.</i><br/><br/>" +
+ "Cordialement,<br/>" +
+ "L'équipe Technique. <br/><br/>" +
+ "<i style='color: grey; font-size: 11px;'>" +
+ "Ce message a été envoyé depuis la machine " + ip + " le " + frenchDateFormat.format(new Date()) + "." +
+ "</i>";
+ mailMessage.setBody(body);
+ MailServiceUtil.sendEmail(mailMessage);
+ return "?success=true";
+ }
+
+ return "?success=false";
+ }
+
+ private String resetPassword(String key) throws Exception {
+
+ long companyId = CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId();
+ User user = null;
+
+ // Recupertation de l'utilisateur a partir de son login
+ try {
+ UserProperties up = UserPropertiesLocalServiceUtil.getUserPropertiesByKeyPasswordRecovery(key);
+ user = UserLocalServiceUtil.getUser(up.getUserId());
+ } catch(Exception e){
+ // La key est incorrect
+ }
+
+ if(user != null){
+ // Obtention de l'adresse mail de recupertaion du mot de passe
+ UserProperties up = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(user.getUserId());
+ up.setKeyPasswordRecovery("");
+ UserPropertiesLocalServiceUtil.updateUserProperties(up, false);
+
+ String mail = up.getMailPasswordRecovery();
+
+ // Reset du mot de passe
+ String password = PasswordUtil.generatePassword();
+ UserLocalServiceUtil.updatePassword(user.getUserId(), password, password, true);
+
+ // Envoi du mot de passe a l'utilisateur
+ String noReplyMail = ENTMainUtilsLocalServiceUtil.getMailNoReply(companyId);
+
+ InternetAddress iaTo = new InternetAddress(mail);
+ InternetAddress iaFrom = new InternetAddress(noReplyMail);
+
+ MailMessage mailMessage = new MailMessage();
+ mailMessage.setFrom(iaFrom);
+ mailMessage.setTo(iaTo);
+
+ mailMessage.setHTMLFormat(true);
+
+ String entPlateformName = ENTMainUtilsLocalServiceUtil.getEntPlateformName();
+ String subject = "ENT " + entPlateformName + " : Réinitialisation de votre mot de passe (2/2)";
+ mailMessage.setSubject(subject);
+
+ String body = "Bonjour " + user.getFullName() + ", </br></br>" +
+ "Votre demande de réinitialisation de mot de passe a été traitée avec succès. <br/>" +
+ "Voici vos nouvelles informations de connexion sur la plateforme " + entPlateformName + " : <br/>" +
+ " • Identifiant : " + user.getScreenName() + "<br/>" +
+ " • Mot de passe : " + password + "<br/><br/>" +
+ "Pour des raisons de sécurité, il vous sera demandée de le modifier lors de votre prochaine connexion sur l'ENT.<br/><br/>" +
+ "Cordialement,<br/>" +
+ "L'équipe Technique.";
+ mailMessage.setBody(body);
+ MailServiceUtil.sendEmail(mailMessage);
+
+ return "?success_init=true";
+ }
+
+ return "?success_init=false";
+ }
+
+}