RBACDAO.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.util.sql.DAOUtil;

  36. import java.sql.Statement;
  37. import java.util.ArrayList;
  38. import java.util.Collection;
  39. import java.util.stream.Collectors;

  40. /**
  41.  * This class provides Data Access methods for RBAC objects
  42.  */
  43. public final class RBACDAO implements IRBACDAO
  44. {
  45.     // Constants
  46.     private static final String SQL_QUERY_SELECT = " SELECT rbac_id, role_key, resource_type, resource_id, permission FROM core_admin_role_resource WHERE rbac_id = ?  ";
  47.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_admin_role_resource ( role_key, resource_type, resource_id, permission ) VALUES ( ?, ?, ?, ? ) ";
  48.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_admin_role_resource WHERE rbac_id = ?  ";
  49.     private static final String SQL_QUERY_UPDATE = " UPDATE core_admin_role_resource SET rbac_id = ?, role_key = ?, resource_type = ?, resource_id = ?, permission = ? WHERE rbac_id = ?  ";
  50.     private static final String SQL_QUERY_SELECTALL = " SELECT rbac_id, role_key, resource_type, resource_id, permission FROM core_admin_role_resource ";
  51.     private static final String SQL_QUERY_SELECT_BY_ROLE = " SELECT rbac_id, role_key, resource_type, resource_id, permission FROM core_admin_role_resource WHERE role_key = ?  ORDER BY resource_type,resource_id,permission ";
  52.     private static final String SQL_QUERY_UPDATE_ROLES = " UPDATE core_admin_role_resource SET  role_key = ? WHERE role_key = ?  ";
  53.     private static final String SQL_QUERY_DELETE_FOR_ROLE_KEY = " DELETE FROM core_admin_role_resource WHERE role_key = ? ";
  54.     private static final String SQL_QUERY_DELETE_BY_RESOURCE_TYPE_AND_RESOURCE_ID = " DELETE FROM core_admin_role_resource WHERE resource_type = ? AND resource_id = ? ";

  55.     // query used to retrieve the roles associeted with a resource
  56.     private static final String SQL_QUERY_SELECT_ROLE_KEYS = " SELECT DISTINCT role_key FROM core_admin_role_resource " + " WHERE resource_type = ? AND "
  57.             + "( resource_id = ? OR resource_id= ? ) AND" + "( permission = ? OR permission= ? )";
  58.     private static final String SQL_QUERY_SELECT_BY_PERMISSIONS_AND_ROLES_PART1 = " SELECT rbac_id, role_key, resource_type, resource_id, permission FROM core_admin_role_resource WHERE role_key IN (";
  59.     private static final String SQL_QUERY_SELECT_BY_PERMISSIONS_AND_ROLES_PART2 = ") AND ( permission IN (";
  60.     private static final String SQL_QUERY_SELECT_BY_PERMISSIONS_AND_ROLES_PART3 = ") OR permission = ? ) ";

  61.     /**
  62.      * Insert a new record in the table.
  63.      *
  64.      * @param rBAC
  65.      *            The rBAC object
  66.      */
  67.     public void insert( RBAC rBAC )
  68.     {
  69.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
  70.         {
  71.             int nIndex = 1;
  72.             daoUtil.setString( nIndex++, rBAC.getRoleKey( ) );
  73.             daoUtil.setString( nIndex++, rBAC.getResourceTypeKey( ) );
  74.             daoUtil.setString( nIndex++, rBAC.getResourceId( ) );
  75.             daoUtil.setString( nIndex, rBAC.getPermissionKey( ) );

  76.             daoUtil.executeUpdate( );

  77.             if ( daoUtil.nextGeneratedKey( ) )
  78.             {
  79.                 rBAC.setRBACId( daoUtil.getGeneratedKeyInt( 1 ) );
  80.             }

  81.         }
  82.     }

  83.     /**
  84.      * Load the data of RBAC from the table
  85.      *
  86.      * @param nRBACId
  87.      *            The identifier of RBAC
  88.      * @return the instance of the RBAC
  89.      */
  90.     public RBAC load( int nRBACId )
  91.     {
  92.         RBAC rBAC = null;
  93.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
  94.         {
  95.             daoUtil.setInt( 1, nRBACId );
  96.             daoUtil.executeQuery( );

  97.             if ( daoUtil.next( ) )
  98.             {
  99.                 rBAC = new RBAC( );
  100.                 rBAC.setRBACId( daoUtil.getInt( 1 ) );
  101.                 rBAC.setRoleKey( daoUtil.getString( 2 ) );
  102.                 rBAC.setResourceTypeKey( daoUtil.getString( 3 ) );
  103.                 rBAC.setResourceId( daoUtil.getString( 4 ) );
  104.                 rBAC.setPermissionKey( daoUtil.getString( 5 ) );
  105.             }

  106.         }

  107.         return rBAC;
  108.     }

  109.     /**
  110.      * Delete a record from the table
  111.      *
  112.      * @param nRBACId
  113.      *            The id of RBAC object to delete
  114.      */
  115.     public void delete( int nRBACId )
  116.     {
  117.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
  118.         {
  119.             daoUtil.setInt( 1, nRBACId );

  120.             daoUtil.executeUpdate( );
  121.         }
  122.     }

  123.     /**
  124.      * Update the record in the table
  125.      *
  126.      * @param rBAC
  127.      *            The reference of rBAC
  128.      */
  129.     public void store( RBAC rBAC )
  130.     {
  131.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
  132.         {
  133.             daoUtil.setInt( 1, rBAC.getRBACId( ) );
  134.             daoUtil.setString( 2, rBAC.getRoleKey( ) );
  135.             daoUtil.setString( 3, rBAC.getResourceTypeKey( ) );
  136.             daoUtil.setString( 4, rBAC.getResourceId( ) );
  137.             daoUtil.setString( 5, rBAC.getPermissionKey( ) );
  138.             daoUtil.setInt( 6, rBAC.getRBACId( ) );

  139.             daoUtil.executeUpdate( );
  140.         }
  141.     }

  142.     /**
  143.      * Load the list of rBACs
  144.      *
  145.      * @return The Collection of the RBACs
  146.      */
  147.     public Collection<RBAC> selectRBACList( )
  148.     {
  149.         Collection<RBAC> listRBACs = new ArrayList<>( );
  150.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
  151.         {
  152.             daoUtil.executeQuery( );

  153.             while ( daoUtil.next( ) )
  154.             {
  155.                 RBAC rBAC = new RBAC( );
  156.                 rBAC.setRBACId( daoUtil.getInt( 1 ) );
  157.                 rBAC.setRoleKey( daoUtil.getString( 2 ) );
  158.                 rBAC.setResourceTypeKey( daoUtil.getString( 3 ) );
  159.                 rBAC.setResourceId( daoUtil.getString( 4 ) );
  160.                 rBAC.setPermissionKey( daoUtil.getString( 5 ) );

  161.                 listRBACs.add( rBAC );
  162.             }

  163.         }

  164.         return listRBACs;
  165.     }

  166.     /**
  167.      * Find all the entries for a given role key
  168.      *
  169.      * @param strRoleKey
  170.      *            the role key to search for
  171.      * @return A collection of rBACs
  172.      */
  173.     public Collection<RBAC> selectRBACListByRoleKey( String strRoleKey )
  174.     {
  175.         Collection<RBAC> listRBACs = new ArrayList<>( );
  176.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ROLE ) )
  177.         {
  178.             daoUtil.setString( 1, strRoleKey );
  179.             daoUtil.executeQuery( );

  180.             while ( daoUtil.next( ) )
  181.             {
  182.                 RBAC rBAC = new RBAC( );
  183.                 rBAC.setRBACId( daoUtil.getInt( 1 ) );
  184.                 rBAC.setRoleKey( daoUtil.getString( 2 ) );
  185.                 rBAC.setResourceTypeKey( daoUtil.getString( 3 ) );
  186.                 rBAC.setResourceId( daoUtil.getString( 4 ) );
  187.                 rBAC.setPermissionKey( daoUtil.getString( 5 ) );

  188.                 listRBACs.add( rBAC );
  189.             }

  190.         }

  191.         return listRBACs;
  192.     }

  193.     @Override
  194.     public Collection<RBAC> selectByPermissionsAndRoles( Collection<String> permissions, Collection<String> roles )
  195.     {
  196.         String query = new StringBuilder( SQL_QUERY_SELECT_BY_PERMISSIONS_AND_ROLES_PART1 )
  197.                 .append( roles.stream( ).map( r -> "?" ).collect( Collectors.joining( "," ) ) )
  198.                 .append( SQL_QUERY_SELECT_BY_PERMISSIONS_AND_ROLES_PART2 )
  199.                 .append( permissions.stream( ).map( r -> "?" ).collect( Collectors.joining( "," ) ) )
  200.                 .append( SQL_QUERY_SELECT_BY_PERMISSIONS_AND_ROLES_PART3 ).toString( );
  201.         Collection<RBAC> listRBACs = new ArrayList<>( );
  202.         try ( DAOUtil daoUtil = new DAOUtil( query ) )
  203.         {
  204.             int nIndex = 1;
  205.             for ( String role : roles )
  206.             {
  207.                 daoUtil.setString( nIndex++, role );
  208.             }
  209.             for ( String permission : permissions )
  210.             {
  211.                 daoUtil.setString( nIndex++, permission );
  212.             }
  213.             daoUtil.setString( nIndex++, RBAC.WILDCARD_PERMISSIONS_KEY );
  214.             daoUtil.executeQuery( );

  215.             while ( daoUtil.next( ) )
  216.             {
  217.                 RBAC rBAC = new RBAC( );
  218.                 rBAC.setRBACId( daoUtil.getInt( 1 ) );
  219.                 rBAC.setRoleKey( daoUtil.getString( 2 ) );
  220.                 rBAC.setResourceTypeKey( daoUtil.getString( 3 ) );
  221.                 rBAC.setResourceId( daoUtil.getString( 4 ) );
  222.                 rBAC.setPermissionKey( daoUtil.getString( 5 ) );

  223.                 listRBACs.add( rBAC );
  224.             }

  225.         }
  226.         return listRBACs;
  227.     }

  228.     /**
  229.      * Update the role key of all the entries of a given role key
  230.      *
  231.      * @param strOldRoleKey
  232.      *            the role key to update
  233.      * @param strNewRoleKey
  234.      *            the new role key
  235.      */
  236.     public void updateRoleKey( String strOldRoleKey, String strNewRoleKey )
  237.     {
  238.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_ROLES ) )
  239.         {
  240.             daoUtil.setString( 1, strNewRoleKey );
  241.             daoUtil.setString( 2, strOldRoleKey );

  242.             daoUtil.executeUpdate( );
  243.         }
  244.     }

  245.     /**
  246.      * Remove all the entries of the given role key
  247.      *
  248.      * @param strRoleKey
  249.      *            the role key of the entries to remove
  250.      */
  251.     public void deleteForRoleKey( String strRoleKey )
  252.     {
  253.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FOR_ROLE_KEY ) )
  254.         {
  255.             daoUtil.setString( 1, strRoleKey );

  256.             daoUtil.executeUpdate( );
  257.         }
  258.     }

  259.     /**
  260.      * @param strTypeCode
  261.      *            The type code
  262.      * @param strId
  263.      *            the id
  264.      * @param strPermission
  265.      *            th permission
  266.      * @return listRoleKeys
  267.      */
  268.     public Collection<String> selectRoleKeys( String strTypeCode, String strId, String strPermission )
  269.     {
  270.         Collection<String> listRoleKeys = new ArrayList<>( );
  271.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ROLE_KEYS ) )
  272.         {
  273.             daoUtil.setString( 1, strTypeCode );

  274.             daoUtil.setString( 2, strId );
  275.             daoUtil.setString( 3, RBAC.WILDCARD_RESOURCES_ID );

  276.             daoUtil.setString( 4, strPermission );
  277.             daoUtil.setString( 5, RBAC.WILDCARD_PERMISSIONS_KEY );

  278.             daoUtil.executeQuery( );

  279.             while ( daoUtil.next( ) )
  280.             {
  281.                 daoUtil.getString( 1 );
  282.                 listRoleKeys.add( daoUtil.getString( 1 ) );
  283.             }

  284.         }

  285.         return listRoleKeys;
  286.     }

  287.     /**
  288.      * {@inheritDoc }
  289.      */
  290.     @Override
  291.     public void deleteForResourceTypeAndId( String strResourceType, String strResourceId )
  292.     {
  293.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_RESOURCE_TYPE_AND_RESOURCE_ID ) )
  294.         {
  295.             daoUtil.setString( 1, strResourceType );
  296.             daoUtil.setString( 2, strResourceId );

  297.             daoUtil.executeUpdate( );
  298.         }
  299.     }

  300. }