UtentePasswordChange.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.web.ctrlstat.servlet.utenti;

  21. import java.util.ArrayList;
  22. import java.util.Date;
  23. import java.util.List;

  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import javax.servlet.http.HttpSession;

  27. import org.apache.struts.action.Action;
  28. import org.apache.struts.action.ActionForm;
  29. import org.apache.struts.action.ActionForward;
  30. import org.apache.struts.action.ActionMapping;
  31. import org.openspcoop2.utils.crypt.PasswordVerifier;
  32. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  33. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  34. import org.openspcoop2.web.ctrlstat.gestori.GestoreConsistenzaDati;
  35. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  36. import org.openspcoop2.web.ctrlstat.servlet.login.LoginCostanti;
  37. import org.openspcoop2.web.ctrlstat.servlet.login.LoginSessionUtilities;
  38. import org.openspcoop2.web.lib.mvc.Costanti;
  39. import org.openspcoop2.web.lib.mvc.DataElement;
  40. import org.openspcoop2.web.lib.mvc.ForwardParams;
  41. import org.openspcoop2.web.lib.mvc.GeneralData;
  42. import org.openspcoop2.web.lib.mvc.MessageType;
  43. import org.openspcoop2.web.lib.mvc.PageData;
  44. import org.openspcoop2.web.lib.mvc.ServletUtils;
  45. import org.openspcoop2.web.lib.mvc.TipoOperazione;
  46. import org.openspcoop2.web.lib.users.dao.User;
  47. import org.openspcoop2.web.lib.users.dao.UserPassword;

  48. /**
  49.  * UtentePasswordChange
  50.  *
  51.  * @author Andrea Poli (apoli@link.it)
  52.  * @author Giuliano Pintori (pintori@link.it)
  53.  * @author $Author$
  54.  * @version $Rev$, $Date$
  55.  *
  56.  */
  57. public final class UtentePasswordChange extends Action {

  58.     @Override
  59.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

  60.         HttpSession session = request.getSession(true);

  61.         // Inizializzo PageData
  62.         PageData pd = new PageData();

  63.         GeneralHelper generalHelper = new GeneralHelper(session);

  64.         // Inizializzo GeneralData
  65.         GeneralData gd = generalHelper.initGeneralData(request);

  66.         try {
  67.             UtentiHelper utentiHelper = new UtentiHelper(request, pd, session);

  68.             String newpw = utentiHelper.getParameter(UtentiCostanti.PARAMETRO_UTENTE_NUOVA_PW);
  69.             String first = utentiHelper.getParameter(UtentiCostanti.PARAMETRO_UTENTI_FIRST);

  70.             PageData oldPD = ServletUtils.getObjectFromSession(request, session, PageData.class, Costanti.SESSION_ATTRIBUTE_PAGE_DATA_REDIRECT);
  71.            
  72.             UtentiCore utentiCore = new UtentiCore();
  73.            
  74.             String userToUpdate = ServletUtils.getObjectFromSession(request, session, String.class, LoginCostanti.ATTRIBUTO_MODALITA_CAMBIA_PWD_SCADUTA);
  75.            
  76.             User user = utentiCore.getUser(userToUpdate);
  77.            
  78.             if(utentiHelper.isEditModeInProgress()){
  79.                
  80.                 pd.setIncludiMenuLateraleSx(false);
  81.                
  82.                 if(first == null && oldPD != null) {
  83.                     pd.setMessage(oldPD.getMessage(), MessageType.fromValue(oldPD.getMessageType()));
  84.                 }
  85.                
  86.                 List<DataElement> dati = new ArrayList<>();

  87.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  88.                
  89.                 utentiHelper.addUtenteChangePasswordScadutaToDati(dati, TipoOperazione.CHANGE);
  90.                
  91.                 pd.setDati(dati);
  92.        
  93.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  94.                
  95.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, UtentiCostanti.OBJECT_NAME_UTENTE_PASSWORD,ForwardParams.CHANGE());
  96.             }
  97.            
  98.            
  99.             // controllo della password inserita
  100.             boolean isOk = utentiHelper.changePwScadutaCheckData();
  101.             if (!isOk) {
  102.                
  103.                 pd.setIncludiMenuLateraleSx(false);
  104.                
  105.                 // preparo i campi
  106.                 List<DataElement> dati = new ArrayList<>();

  107.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  108.                 utentiHelper.addUtenteChangePasswordScadutaToDati(dati, TipoOperazione.CHANGE);

  109.                 pd.setDati(dati);

  110.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  111.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, UtentiCostanti.OBJECT_NAME_UTENTE_PASSWORD,ForwardParams.CHANGE());

  112.             }
  113.            
  114.             User myS = null;
  115.             //tutto ok modifico pwd
  116.             // Cripto la nuova password
  117.             newpw = utentiCore.getUtenzePasswordManager().crypt(newpw);

  118.             // Modifico i dati della pw nel db
  119.             myS = utentiCore.getUser(userToUpdate);
  120.            
  121.            
  122.             PasswordVerifier passwordVerifier = utentiCore.getUtenzePasswordVerifier();
  123.            
  124.             // aggiornamento della password nello storico e nella data
  125.             if(passwordVerifier.isHistory()) {
  126.                 List<UserPassword> precedentiPassword = myS.getPrecedentiPassword();
  127.                
  128.                 UserPassword userPassword = new UserPassword();
  129.                 userPassword.setDatePassword(user.getLastUpdatePassword());
  130.                 userPassword.setPassword(user.getPassword());
  131.                 precedentiPassword.add(userPassword );
  132.             }
  133.            
  134.             myS.setLastUpdatePassword(new Date());
  135.             myS.setPassword(newpw);

  136.             utentiCore.performUpdateOperation(userToUpdate, utentiHelper.smista(), myS);
  137.            
  138.            
  139.             // eliminare stato password scaduta dalla sessione
  140.             ServletUtils.removeObjectFromSession(request, session, LoginCostanti.ATTRIBUTO_MODALITA_CAMBIA_PWD_SCADUTA);

  141.             // porto l'utente dove andrebbe dopo il login effettuato con successo.
  142.             LoginSessionUtilities.setLoginParametersSession(request, session, utentiCore, userToUpdate);
  143.             //utentiHelper.updateTipoInterfaccia();
  144.            
  145.             utentiCore.performAuditLogin(userToUpdate);
  146.            
  147.             // Preparo il menu
  148.             utentiHelper.makeMenu();
  149.    
  150.             // Inizializzo parametri di ricerca
  151.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  152.             utentiHelper.initializeFilter(ricerca);
  153.            
  154.             // Boolean verifico configurazione
  155.             StringBuilder verificaConfigurazioneProtocolli = new StringBuilder();
  156.             boolean configurazioneCorretta = utentiCore.verificaConfigurazioneProtocolliRispettoSoggettiDefault(verificaConfigurazioneProtocolli);
  157.            
  158.             if(!configurazioneCorretta) {
  159.                 pd.setMessage(LoginCostanti.LABEL_LOGIN_EFFETTUATO_CON_SUCCESSO+"<br/><br/><b>Attenzione</b>: il controllo di consistenza tra Profili di Interoperabilità attivati e la configurazione sul Gateway ha rilevato inconsistenze: \n"+verificaConfigurazioneProtocolli.toString(),
  160.                         Costanti.MESSAGE_TYPE_ERROR);
  161.             }
  162.             else if(GestoreConsistenzaDati.gestoreConsistenzaDatiEseguitoConErrore){
  163.                 pd.setMessage(LoginCostanti.LABEL_LOGIN_EFFETTUATO_CON_SUCCESSO+"<br/><br/><b>Attenzione</b>: il controllo sulla consistenza dei dati è terminato con errore; esaminare i log per maggiori dettagli",
  164.                         Costanti.MESSAGE_TYPE_INFO);
  165.             }
  166.             else{
  167.                 pd.setMessage(LoginCostanti.LABEL_LOGIN_PASSWORD_AGGIORNATA_CON_SUCCESSO,Costanti.MESSAGE_TYPE_INFO_SINTETICO);
  168.             }

  169.             // Reinit general data per aggiornare lo stato della barra dell'header a dx.
  170.             gd = generalHelper.initGeneralData(request);

  171.             // Refresh Menu' Preparo il menu
  172.             utentiHelper.makeMenu();

  173.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  174.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, UtentiCostanti.OBJECT_NAME_UTENTE_PASSWORD, ForwardParams.CHANGE());

  175.         } catch (Exception e) {
  176.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping, UtentiCostanti.OBJECT_NAME_UTENTE_PASSWORD, ForwardParams.CHANGE());
  177.         }
  178.     }
  179. }