RBAC.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.business.rbac;

  35. import fr.paris.lutece.portal.service.i18n.Localizable;
  36. import fr.paris.lutece.portal.service.rbac.Permission;
  37. import fr.paris.lutece.portal.service.rbac.ResourceIdService;
  38. import fr.paris.lutece.portal.service.rbac.ResourceType;
  39. import fr.paris.lutece.portal.service.rbac.ResourceTypeManager;

  40. import org.apache.commons.lang3.StringUtils;

  41. import java.util.Locale;

  42. /**
  43.  * This class is used for rbac control configuration. A role is associated to controls (given by the permission key) on resources (identified by a resource type
  44.  * and a resource id). Wilcards can be used for resource ids and permission keys.
  45.  */
  46. public class RBAC implements Localizable
  47. {
  48.     public static final String WILDCARD_RESOURCES_ID = "*";
  49.     public static final String WILDCARD_PERMISSIONS_KEY = "*";
  50.     private int _nRBACId;
  51.     private String _strRoleKey;
  52.     private String _strResourceTypeKey;
  53.     private String _strResourceId;
  54.     private String _strPermissionKey;
  55.     private Locale _locale;

  56.     /**
  57.      * Implements Localizable
  58.      *
  59.      * @param locale
  60.      *            The current locale
  61.      */
  62.     public void setLocale( Locale locale )
  63.     {
  64.         _locale = locale;
  65.     }

  66.     /**
  67.      * Returns the RBAC ID
  68.      *
  69.      * @return The RBAC ID
  70.      */
  71.     public int getRBACId( )
  72.     {
  73.         return _nRBACId;
  74.     }

  75.     /**
  76.      * Sets the RBAC ID
  77.      *
  78.      * @param nRBACId
  79.      *            The RBAC Id to set
  80.      */
  81.     public void setRBACId( int nRBACId )
  82.     {
  83.         _nRBACId = nRBACId;
  84.     }

  85.     /**
  86.      * Returns the Permission Key
  87.      *
  88.      * @return The Permission Key
  89.      */
  90.     public String getPermissionKey( )
  91.     {
  92.         return _strPermissionKey;
  93.     }

  94.     /**
  95.      * Sets the Permission Key
  96.      *
  97.      * @param strPermissionKey
  98.      *            The Permission Key to set
  99.      */
  100.     public void setPermissionKey( String strPermissionKey )
  101.     {
  102.         _strPermissionKey = strPermissionKey;
  103.     }

  104.     /**
  105.      * Returns the Resource Id
  106.      *
  107.      * @return The Resource Id
  108.      */
  109.     public String getResourceId( )
  110.     {
  111.         return _strResourceId;
  112.     }

  113.     /**
  114.      * Sets the Resource Id
  115.      *
  116.      * @param strResourceId
  117.      *            The Resource Id to set
  118.      */
  119.     public void setResourceId( String strResourceId )
  120.     {
  121.         _strResourceId = strResourceId;
  122.     }

  123.     /**
  124.      * Returns the Resource Type Key
  125.      *
  126.      * @return The Resource Type Key.
  127.      */
  128.     public String getResourceTypeKey( )
  129.     {
  130.         return _strResourceTypeKey;
  131.     }

  132.     /**
  133.      * Sets the Resource Type Key
  134.      *
  135.      * @param strResourceTypeKey
  136.      *            The Resource Type Key to set.
  137.      */
  138.     public void setResourceTypeKey( String strResourceTypeKey )
  139.     {
  140.         _strResourceTypeKey = strResourceTypeKey;
  141.     }

  142.     /**
  143.      * Returns the Role Key.
  144.      *
  145.      * @return The Role Key.
  146.      */
  147.     public String getRoleKey( )
  148.     {
  149.         return _strRoleKey;
  150.     }

  151.     /**
  152.      * Sets the Role Key
  153.      *
  154.      * @param strRoleKey
  155.      *            The Role Key to set.
  156.      */
  157.     public void setRoleKey( String strRoleKey )
  158.     {
  159.         _strRoleKey = strRoleKey;
  160.     }

  161.     /**
  162.      * Retrieve the label of the resource type from the resource type key
  163.      *
  164.      * @return the label of the resource type
  165.      */
  166.     public String getResourceTypeLabel( )
  167.     {
  168.         ResourceType resourceType = ResourceTypeManager.getResourceType( getResourceTypeKey( ) );

  169.         if ( resourceType != null )
  170.         {
  171.             return resourceType.getResourceTypeLabel( );
  172.         }

  173.         return StringUtils.EMPTY;
  174.     }

  175.     /**
  176.      * Retrieve the label of the resource from the resource id
  177.      *
  178.      * @return the label of the resource
  179.      */
  180.     public String getResourceIdLabel( )
  181.     {
  182.         if ( getResourceId( ).equals( WILDCARD_RESOURCES_ID ) )
  183.         {
  184.             return WILDCARD_RESOURCES_ID;
  185.         }
  186.         else
  187.         {
  188.             ResourceType resourceType = ResourceTypeManager.getResourceType( getResourceTypeKey( ) );

  189.             if ( resourceType != null )
  190.             {
  191.                 ResourceIdService resourceManagerService = resourceType.getResourceIdService( );
  192.                 String strTitle = StringUtils.EMPTY;

  193.                 if ( resourceManagerService != null )
  194.                 {
  195.                     strTitle = resourceManagerService.getTitle( getResourceId( ), _locale );
  196.                 }
  197.                 if ( strTitle != null && StringUtils.isNotEmpty( strTitle ) )
  198.                 {
  199.                     return strTitle;
  200.                 }
  201.                 else
  202.                 {
  203.                     return getResourceId( );
  204.                 }
  205.             }

  206.             return StringUtils.EMPTY;
  207.         }
  208.     }

  209.     /**
  210.      * Retrieve the label of the permission from the permission key
  211.      *
  212.      * @return the label of the permission
  213.      */
  214.     public String getPermissionLabel( )
  215.     {
  216.         if ( getPermissionKey( ).equals( WILDCARD_PERMISSIONS_KEY ) )
  217.         {
  218.             return WILDCARD_PERMISSIONS_KEY;
  219.         }
  220.         else
  221.         {
  222.             ResourceType resourceType = ResourceTypeManager.getResourceType( getResourceTypeKey( ) );
  223.             if ( resourceType != null )
  224.             {
  225.                 Permission permission = resourceType.getPermission( getPermissionKey( ) );
  226.                 if ( permission != null )
  227.                 {
  228.                     permission.setLocale( _locale );
  229.                     return permission.getPermissionTitle( );
  230.                 }
  231.             }
  232.         }
  233.         return StringUtils.EMPTY;
  234.     }
  235. }