TipoAutorizzazione.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.core.config.constants;

  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.generic_project.beans.IEnumeration;
  25. import org.openspcoop2.generic_project.exception.NotFoundException;
  26. import org.openspcoop2.utils.UtilsRuntimeException;

  27. /**    
  28.  * Enumeration TipoAutorizzazione
  29.  *
  30.  * @author Poli Andrea (poli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public enum TipoAutorizzazione implements IEnumeration , Serializable , Cloneable {

  35.     DISABILITATO (CostantiConfigurazione.AUTORIZZAZIONE_NONE,"disabilitato"),
  36.     AUTHENTICATED (CostantiConfigurazione.AUTORIZZAZIONE_AUTHENTICATED,"authenticated"),
  37.     ROLES (CostantiConfigurazione.AUTORIZZAZIONE_ROLES,"roles"),
  38.     INTERNAL_ROLES (CostantiConfigurazione.AUTORIZZAZIONE_INTERNAL_ROLES,"internalRoles"),
  39.     EXTERNAL_ROLES (CostantiConfigurazione.AUTORIZZAZIONE_EXTERNAL_ROLES,"externalRoles"),
  40.     AUTHENTICATED_ROLES (CostantiConfigurazione.AUTORIZZAZIONE_AUTHENTICATED_OR_ROLES,"authenticatedOrRoles"),
  41.     AUTHENTICATED_INTERNAL_ROLES (CostantiConfigurazione.AUTORIZZAZIONE_AUTHENTICATED_OR_INTERNAL_ROLES,"authenticatedOrInternalRoles"),
  42.     AUTHENTICATED_EXTERNAL_ROLES (CostantiConfigurazione.AUTORIZZAZIONE_AUTHENTICATED_OR_EXTERNAL_ROLES,"authenticatedOrExternalRoles"),
  43.     XACML_POLICY (CostantiConfigurazione.AUTORIZZAZIONE_XACML_POLICY,"xacmlPolicy"),
  44.     INTERNAL_XACML_POLICY (CostantiConfigurazione.AUTORIZZAZIONE_INTERNAL_XACML_POLICY,"internalXacmlPolicy"),
  45.     EXTERNAL_XACML_POLICY (CostantiConfigurazione.AUTORIZZAZIONE_EXTERNAL_XACML_POLICY,"externalXacmlPolicy"),
  46.     TOKEN (CostantiConfigurazione.AUTORIZZAZIONE_TOKEN,"token"),
  47.     SIGNAL_HUB_PUSH (CostantiConfigurazione.SIGNAL_HUB_PUSH,"signalHubPush");

  48.    
  49.     /** Value */
  50.     private String value;
  51.     private String label;
  52.     public String getLabel() {
  53.         return this.label;
  54.     }
  55.     @Override
  56.     public String getValue()
  57.     {
  58.         return this.value;
  59.     }


  60.     /** Official Constructor */
  61.     TipoAutorizzazione(String value,String label)
  62.     {
  63.         this.value = value;
  64.         this.label = label;
  65.     }


  66.    
  67.     @Override
  68.     public String toString(){
  69.         return this.value;
  70.     }
  71.     public boolean equals(String object){
  72.         if(object==null)
  73.             return false;
  74.         return object.equals(this.getValue());  
  75.     }
  76.    
  77.        
  78.    
  79.     /** compatibility with the generated bean (reflection) */
  80.     public boolean equals(Object object,List<String> fieldsNotCheck){
  81.         if(fieldsNotCheck!=null) {
  82.             // nop
  83.         }
  84.         if( !(object instanceof TipoAutorizzazione) ){
  85.             throw new UtilsRuntimeException("Wrong type: "+object.getClass().getName());
  86.         }
  87.         return this.equals((object));
  88.     }
  89.     public String toString(boolean reportHTML){
  90.         if(reportHTML) {
  91.             // nop
  92.         }
  93.         return toString();
  94.     }
  95.     public String toString(boolean reportHTML,List<String> fieldsNotIncluded){
  96.         if(reportHTML && fieldsNotIncluded!=null) {
  97.             // nop
  98.         }
  99.         return toString();
  100.     }
  101.     public String diff(Object object,StringBuilder bf,boolean reportHTML){
  102.         if(object!=null && reportHTML) {
  103.             // nop
  104.         }
  105.         return bf.toString();
  106.     }
  107.     public String diff(Object object,StringBuilder bf,boolean reportHTML,List<String> fieldsNotIncluded){
  108.         if(object!=null && reportHTML && fieldsNotIncluded!=null) {
  109.             // nop
  110.         }
  111.         return bf.toString();
  112.     }
  113.    
  114.    
  115.     /** Utilities */
  116.    
  117.     public static boolean isInternalRolesRequired(String autorizzazione){
  118.         return isInternalRolesRequired(TipoAutorizzazione.toEnumConstant(autorizzazione));
  119.     }
  120.     public static boolean isInternalRolesRequired(TipoAutorizzazione autorizzazione){
  121.         return TipoAutorizzazione.INTERNAL_ROLES.equals(autorizzazione) ||
  122.                 TipoAutorizzazione.AUTHENTICATED_INTERNAL_ROLES.equals(autorizzazione);
  123.     }
  124.    
  125.     public static boolean isExternalRolesRequired(String autorizzazione){
  126.         return isExternalRolesRequired(TipoAutorizzazione.toEnumConstant(autorizzazione));
  127.     }
  128.     public static boolean isExternalRolesRequired(TipoAutorizzazione autorizzazione){
  129.         return TipoAutorizzazione.EXTERNAL_ROLES.equals(autorizzazione) ||
  130.                 TipoAutorizzazione.AUTHENTICATED_EXTERNAL_ROLES.equals(autorizzazione);
  131.     }
  132.    
  133.     public static boolean isRolesRequired(String autorizzazione){
  134.         return isRolesRequired(TipoAutorizzazione.toEnumConstant(autorizzazione));
  135.     }
  136.     public static boolean isRolesRequired(TipoAutorizzazione autorizzazione){
  137.         return TipoAutorizzazione.ROLES.equals(autorizzazione) ||
  138.                 TipoAutorizzazione.AUTHENTICATED_ROLES.equals(autorizzazione) ||
  139.                 TipoAutorizzazione.INTERNAL_ROLES.equals(autorizzazione) ||
  140.                 TipoAutorizzazione.AUTHENTICATED_INTERNAL_ROLES.equals(autorizzazione) ||
  141.                 TipoAutorizzazione.EXTERNAL_ROLES.equals(autorizzazione) ||
  142.                 TipoAutorizzazione.AUTHENTICATED_EXTERNAL_ROLES.equals(autorizzazione) ;
  143.     }
  144.    
  145.     public static boolean isAuthenticationRequired(String autorizzazione){
  146.         return isAuthenticationRequired(TipoAutorizzazione.toEnumConstant(autorizzazione));
  147.     }
  148.    
  149.     public static boolean isAuthenticationRequired(TipoAutorizzazione autorizzazione){
  150.         return TipoAutorizzazione.AUTHENTICATED.equals(autorizzazione) ||
  151.                 isSignalHubPush(autorizzazione) ||
  152.                 TipoAutorizzazione.AUTHENTICATED_EXTERNAL_ROLES.equals(autorizzazione)  ||
  153.                 TipoAutorizzazione.AUTHENTICATED_INTERNAL_ROLES.equals(autorizzazione)  ||
  154.                 TipoAutorizzazione.AUTHENTICATED_ROLES.equals(autorizzazione);  
  155.     }
  156.    
  157.     public static boolean isXacmlPolicyRequired(String autorizzazione){
  158.         return isXacmlPolicyRequired(TipoAutorizzazione.toEnumConstant(autorizzazione));
  159.     }
  160.     public static boolean isXacmlPolicyRequired(TipoAutorizzazione autorizzazione){
  161.         return TipoAutorizzazione.XACML_POLICY.equals(autorizzazione) ||
  162.                 TipoAutorizzazione.EXTERNAL_XACML_POLICY.equals(autorizzazione)  ||
  163.                 TipoAutorizzazione.INTERNAL_XACML_POLICY.equals(autorizzazione);
  164.     }
  165.    
  166.     public static boolean isSignalHubPush(String autorizzazione){
  167.         return isSignalHubPush(TipoAutorizzazione.toEnumConstant(autorizzazione));
  168.     }
  169.     public static boolean isSignalHubPush(TipoAutorizzazione autorizzazione){
  170.         return TipoAutorizzazione.SIGNAL_HUB_PUSH.equals(autorizzazione);
  171.     }
  172.    
  173.     public static List<String> getAllValues(){
  174.         List<String> l = new ArrayList<>();
  175.         for (TipoAutorizzazione tmp : values()) {
  176.             l.add(tmp.getValue());
  177.         }
  178.         return l;
  179.     }
  180.     public static List<String> getValues(boolean authenticated,boolean optionalAuthenticated){
  181.         List<String> l = new ArrayList<>();
  182.         for (TipoAutorizzazione tmp : values()) {
  183.            
  184.             if(isAuthenticationRequired(tmp) &&
  185.                 // se l'autenticazione non è attivata, le autorizzazioni basate sull'autenticazione non hanno senso
  186.                 !authenticated){
  187.                 continue;
  188.             }
  189.            
  190.             if(isAuthenticationRequired(tmp) && isRolesRequired(tmp)){
  191.                 // i metodi con sia autenticazione che roles sono permessi solo se l'autenticazione è opzionale, altrimenti i ruoli non verranno mai presi in considerazione
  192.                 if(!optionalAuthenticated){
  193.                     continue;
  194.                 }
  195.             }
  196.             else if(isInternalRolesRequired(tmp) || INTERNAL_XACML_POLICY.equals(tmp)){
  197.                 // sia per i ruoli interni che per la xacml policy interna è richiesta l'autenticazione obbligatoria. Altrimenti la xacml policy request non sarebbe valorizzabile.
  198.                 if(optionalAuthenticated){
  199.                     continue;
  200.                 }
  201.             }
  202.            
  203.             l.add(tmp.getValue());
  204.         }
  205.         return l;
  206.     }
  207.     public static List<String> getLabels(){
  208.         List<String> l = new ArrayList<>();
  209.         for (TipoAutorizzazione tmp : values()) {
  210.             l.add(tmp.getLabel());
  211.         }
  212.         return l;
  213.     }
  214.    
  215.     public static String[] toArray(){
  216.         String[] res = new String[values().length];
  217.         int i=0;
  218.         for (TipoAutorizzazione tmp : values()) {
  219.             res[i]=tmp.getValue();
  220.             i++;
  221.         }
  222.         return res;
  223.     }  
  224.     public static String[] toStringArray(){
  225.         String[] res = new String[values().length];
  226.         int i=0;
  227.         for (TipoAutorizzazione tmp : values()) {
  228.             res[i]=tmp.toString();
  229.             i++;
  230.         }
  231.         return res;
  232.     }
  233.     public static String[] toEnumNameArray(){
  234.         String[] res = new String[values().length];
  235.         int i=0;
  236.         for (TipoAutorizzazione tmp : values()) {
  237.             res[i]=tmp.name();
  238.             i++;
  239.         }
  240.         return res;
  241.     }
  242.    
  243.     public static boolean contains(String value){
  244.         return toEnumConstant(value)!=null;
  245.     }
  246.    
  247.     public static TipoAutorizzazione toEnumConstant(String value){
  248.         try{
  249.             return toEnumConstant(value,false);
  250.         }catch(NotFoundException notFound){
  251.             return null;
  252.         }
  253.     }
  254.     public static TipoAutorizzazione toEnumConstant(String value, boolean throwNotFoundException) throws NotFoundException{
  255.         TipoAutorizzazione res = null;
  256.         for (TipoAutorizzazione tmp : values()) {
  257.             if(tmp.getValue().equals(value)){
  258.                 res = tmp;
  259.                 break;
  260.             }
  261.         }
  262.         if(res==null && throwNotFoundException){
  263.             throw new NotFoundException("Enum with value ["+value+"] not found");
  264.         }
  265.         return res;
  266.     }
  267.    
  268.     public static IEnumeration toEnumConstantFromString(String value){
  269.         try{
  270.             return toEnumConstantFromString(value,false);
  271.         }catch(NotFoundException notFound){
  272.             return null;
  273.         }
  274.     }
  275.     public static IEnumeration toEnumConstantFromString(String value, boolean throwNotFoundException) throws NotFoundException{
  276.         TipoAutorizzazione res = null;
  277.         for (TipoAutorizzazione tmp : values()) {
  278.             if(tmp.toString().equals(value)){
  279.                 res = tmp;
  280.                 break;
  281.             }
  282.         }
  283.         if(res==null && throwNotFoundException){
  284.             throw new NotFoundException("Enum with value ["+value+"] not found");
  285.         }
  286.         return res;
  287.     }
  288. }