PorteDelegateRuoliAdd.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.pd;

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

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

  26. import org.apache.struts.action.Action;
  27. import org.apache.struts.action.ActionForm;
  28. import org.apache.struts.action.ActionForward;
  29. import org.apache.struts.action.ActionMapping;
  30. import org.openspcoop2.core.commons.Liste;
  31. import org.openspcoop2.core.config.AutorizzazioneRuoli;
  32. import org.openspcoop2.core.config.PortaDelegata;
  33. import org.openspcoop2.core.config.Ruolo;
  34. import org.openspcoop2.core.config.constants.TipoAutorizzazione;
  35. import org.openspcoop2.core.registry.constants.RuoloContesto;
  36. import org.openspcoop2.core.registry.constants.RuoloTipologia;
  37. import org.openspcoop2.core.registry.driver.FiltroRicercaRuoli;
  38. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  39. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  40. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  41. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  42. import org.openspcoop2.web.lib.mvc.DataElement;
  43. import org.openspcoop2.web.lib.mvc.ForwardParams;
  44. import org.openspcoop2.web.lib.mvc.GeneralData;
  45. import org.openspcoop2.web.lib.mvc.PageData;
  46. import org.openspcoop2.web.lib.mvc.Parameter;
  47. import org.openspcoop2.web.lib.mvc.ServletUtils;
  48. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  49. /**
  50.  * PorteDelegateRuoliAdd
  51.  *
  52.  * @author Andrea Poli (apoli@link.it)
  53.  * @author Stefano Corallo (corallo@link.it)
  54.  * @author Sandra Giangrandi (sandra@link.it)
  55.  * @author $Author$
  56.  * @version $Rev$, $Date$
  57.  *
  58.  */
  59. public final class PorteDelegateRuoliAdd extends Action {

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

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

  63.         // Inizializzo PageData
  64.         PageData pd = new PageData();

  65.         GeneralHelper generalHelper = new GeneralHelper(session);

  66.         // Inizializzo GeneralData
  67.         GeneralData gd = generalHelper.initGeneralData(request);

  68.         try {
  69.             PorteDelegateHelper porteDelegateHelper = new PorteDelegateHelper(request, pd, session);
  70.             // prelevo il flag che mi dice da quale pagina ho acceduto la sezione delle porte delegate
  71.             Integer parentPD = ServletUtils.getIntegerAttributeFromSession(PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT, session, request);
  72.             if(parentPD == null) parentPD = PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_NONE;
  73.             String id = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID);
  74.             int idInt = Integer.parseInt(id);
  75.             String idsogg = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO);
  76.             String nome = porteDelegateHelper.getParameter(CostantiControlStation.PARAMETRO_RUOLO);

  77.             String idAsps = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS);
  78.             if(idAsps == null)
  79.                 idAsps = "";
  80.            
  81.             String idFruizione = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE);
  82.             if(idFruizione == null)
  83.                 idFruizione = "";
  84.            
  85.             String tokenList = porteDelegateHelper.getParameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TOKEN_AUTHORIZATION);
  86.             boolean isToken = tokenList!=null && !"".equals(tokenList) && Boolean.valueOf(tokenList);
  87.            
  88.             // Preparo il menu
  89.             porteDelegateHelper.makeMenu();

  90.             String userLogin = ServletUtils.getUserLoginFromSession(session);

  91.            
  92.             // Prendo nome, tipo e pdd del soggetto
  93.             PorteDelegateCore porteDelegateCore = new PorteDelegateCore();
  94.                        
  95.             // Prendo nome della porta delegata
  96.             PortaDelegata pde = porteDelegateCore.getPortaDelegata(idInt);
  97.             String idporta = pde.getNome();

  98.             // Cerco Ruoli
  99.             FiltroRicercaRuoli filtroRuoli = new FiltroRicercaRuoli();
  100.             filtroRuoli.setContesto(RuoloContesto.PORTA_DELEGATA);
  101.             filtroRuoli.setTipologia(RuoloTipologia.QUALSIASI);
  102.             if(isToken) {
  103.                 if(pde.getAutorizzazioneToken()!=null && pde.getAutorizzazioneToken().getTipologiaRuoli()!=null) {
  104.                     RuoloTipologia r = RuoloTipologia.toEnumConstant(pde.getAutorizzazioneToken().getTipologiaRuoli().getValue());
  105.                     filtroRuoli.setTipologia(r);
  106.                 }
  107.             }
  108.             else {
  109.                 if(TipoAutorizzazione.isInternalRolesRequired(pde.getAutorizzazione()) ){
  110.                     filtroRuoli.setTipologia(RuoloTipologia.INTERNO);
  111.                 }
  112.                 else if(TipoAutorizzazione.isExternalRolesRequired(pde.getAutorizzazione()) ){
  113.                     filtroRuoli.setTipologia(RuoloTipologia.ESTERNO);
  114.                 }
  115.             }
  116.                        
  117.             List<String> ruoli = new ArrayList<>();
  118.             if(isToken) {
  119.                 if(pde.getAutorizzazioneToken()!=null && pde.getAutorizzazioneToken().getRuoli()!=null && pde.getAutorizzazioneToken().getRuoli().getRuoloList()!=null && !pde.getAutorizzazioneToken().getRuoli().getRuoloList().isEmpty()){
  120.                     for (Ruolo ruolo : pde.getAutorizzazioneToken().getRuoli().getRuoloList()) {
  121.                         ruoli.add(ruolo.getNome());
  122.                     }
  123.                 }
  124.             }
  125.             else {
  126.                 if(pde.getRuoli()!=null && pde.getRuoli().getRuoloList()!=null && !pde.getRuoli().getRuoloList().isEmpty()){
  127.                     for (Ruolo ruolo : pde.getRuoli().getRuoloList()) {
  128.                         ruoli.add(ruolo.getNome());
  129.                     }
  130.                 }
  131.             }
  132.            
  133.             Parameter pId = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID, id);
  134.             Parameter pIdSoggetto = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO, idsogg);
  135.             Parameter pIdAsps = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_ASPS, idAsps);
  136.             Parameter pIdFrizione = new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE, idFruizione);
  137.             Parameter[] urlParms = { pId,pIdSoggetto,pIdAsps,pIdFrizione,
  138.                     new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_TOKEN_AUTHORIZATION, isToken+"") };
  139.            
  140.             List<Parameter> lstParam = porteDelegateHelper.getTitoloPD(parentPD, idsogg, idAsps, idFruizione);
  141.            
  142.             String labelPerPorta = null;
  143.             if(parentPD!=null && (parentPD.intValue() == PorteDelegateCostanti.ATTRIBUTO_PORTE_DELEGATE_PARENT_CONFIGURAZIONE)) {
  144.                 labelPerPorta = porteDelegateCore.getLabelRegolaMappingFruizionePortaDelegata(
  145.                         PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_CONTROLLO_ACCESSI_CONFIG_DI,
  146.                         PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_CONTROLLO_ACCESSI,
  147.                         pde);
  148.             }
  149.             else {
  150.                 labelPerPorta = PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_CONTROLLO_ACCESSI_CONFIG_DI+idporta;
  151.             }
  152.            
  153.             lstParam.add(new Parameter(labelPerPorta, PorteDelegateCostanti.SERVLET_NAME_PORTE_DELEGATE_CONTROLLO_ACCESSI,
  154.                     new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID, "" + pde.getId()),
  155.                     new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_NOME_PORTA, pde.getNome()),
  156.                     new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_SOGGETTO, pde.getIdSoggetto() + ""),
  157.                     pIdAsps, new Parameter(PorteDelegateCostanti.PARAMETRO_PORTE_DELEGATE_ID_FRUIZIONE, idFruizione+ "")));
  158.            
  159.             String labelPagLista =
  160.                     (
  161.                             isToken ?
  162.                             CostantiControlStation.LABEL_PARAMETRO_PORTE_AUTORIZZAZIONE_TOKEN
  163.                             :
  164.                             CostantiControlStation.LABEL_PARAMETRO_PORTE_AUTORIZZAZIONE_TRASPORTO
  165.                     )
  166.                     + " - " +
  167.                     PorteDelegateCostanti.LABEL_PARAMETRO_PORTE_DELEGATE_RUOLI_CONFIG;
  168.            
  169.             lstParam.add(new Parameter(labelPagLista,
  170.                     PorteDelegateCostanti.SERVLET_NAME_PORTE_DELEGATE_RUOLI_LIST,urlParms));
  171.             lstParam.add(ServletUtils.getParameterAggiungi());
  172.            
  173.             // Se servizioApplicativohid = null, devo visualizzare la pagina per
  174.             // l'inserimento dati
  175.             if( porteDelegateHelper.isEditModeInProgress()){
  176.                 // setto la barra del titolo
  177.                 ServletUtils.setPageDataTitle(pd, lstParam);

  178.                 // preparo i campi
  179.                 List<DataElement> dati = new ArrayList<>();
  180.                
  181.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  182.                 dati = porteDelegateHelper.addRuoliToDati(TipoOperazione.ADD, dati, false, filtroRuoli, nome, ruoli, false, true, true, null, isToken);

  183.                 dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, idsogg, null, idAsps,
  184.                         idFruizione, pde.getTipoSoggettoProprietario(), pde.getNomeSoggettoProprietario(), dati);

  185.                 pd.setDati(dati);

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

  187.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping,
  188.                         PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_RUOLI, ForwardParams.ADD());

  189.             }

  190.             // Controlli sui campi immessi
  191.             boolean isOk = porteDelegateHelper.ruoloCheckData(TipoOperazione.ADD, nome, ruoli);
  192.             if (!isOk) {
  193.                 // setto la barra del titolo
  194.                 ServletUtils.setPageDataTitle(pd, lstParam);

  195.                 // preparo i campi
  196.                 List<DataElement> dati = new ArrayList<>();

  197.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  198.                
  199.                 dati = porteDelegateHelper.addRuoliToDati(TipoOperazione.ADD, dati, false, filtroRuoli, nome, ruoli, false, true, true, null, isToken);

  200.                 dati = porteDelegateHelper.addHiddenFieldsToDati(TipoOperazione.ADD, id, idsogg, null, idAsps,
  201.                         idFruizione, pde.getTipoSoggettoProprietario(), pde.getNomeSoggettoProprietario(), dati);
  202.  
  203.                 pd.setDati(dati);

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

  205.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping,
  206.                         PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_RUOLI,
  207.                         ForwardParams.ADD());
  208.             }

  209.             // Inserisco il ruolo nel db
  210.             Ruolo ruolo = new Ruolo();
  211.             ruolo.setNome(nome);
  212.             if(isToken) {
  213.                 if(pde.getAutorizzazioneToken().getRuoli()==null) {
  214.                     pde.getAutorizzazioneToken().setRuoli(new AutorizzazioneRuoli());
  215.                 }
  216.                 pde.getAutorizzazioneToken().getRuoli().addRuolo(ruolo);
  217.             }
  218.             else {
  219.                 if(pde.getRuoli()==null){
  220.                     pde.setRuoli(new AutorizzazioneRuoli());
  221.                 }
  222.                 pde.getRuoli().addRuolo(ruolo);
  223.             }

  224.             porteDelegateCore.performUpdateOperation(userLogin, porteDelegateHelper.smista(), pde);

  225.             // Preparo la lista
  226.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  227.            
  228.             int idLista = Liste.PORTE_DELEGATE_RUOLI;
  229.             if(isToken) {
  230.                 idLista = Liste.PORTE_DELEGATE_TOKEN_RUOLI;
  231.             }

  232.             ricerca = porteDelegateHelper.checkSearchParameters(idLista, ricerca);

  233.             List<String> lista = isToken ?
  234.                         porteDelegateCore.portaDelegataRuoliTokenList(Integer.parseInt(id), ricerca)
  235.                         :
  236.                         porteDelegateCore.portaDelegataRuoliList(Integer.parseInt(id), ricerca);                

  237.             porteDelegateHelper.preparePorteDelegateRuoliList(idporta, ricerca, lista);

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

  239.             // Forward control to the specified success URI
  240.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_RUOLI,
  241.                     ForwardParams.ADD());
  242.         } catch (Exception e) {
  243.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  244.                     PorteDelegateCostanti.OBJECT_NAME_PORTE_DELEGATE_RUOLI,
  245.                     ForwardParams.ADD());
  246.         }  
  247.     }


  248. }