AccessControlService.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.service.accesscontrol;

  35. import java.util.Locale;

  36. import javax.servlet.http.HttpServletRequest;

  37. import org.springframework.beans.factory.BeanDefinitionStoreException;
  38. import org.springframework.beans.factory.CannotLoadBeanClassException;
  39. import org.springframework.beans.factory.NoSuchBeanDefinitionException;

  40. import fr.paris.lutece.api.user.User;
  41. import fr.paris.lutece.portal.business.accesscontrol.AccessControlSessionData;
  42. import fr.paris.lutece.portal.service.plugin.PluginService;
  43. import fr.paris.lutece.portal.service.spring.SpringContextService;
  44. import fr.paris.lutece.portal.web.xpages.XPage;
  45. import fr.paris.lutece.util.ReferenceList;

  46. /**
  47.  * AccessControlService
  48.  */
  49. public final class AccessControlService
  50. {
  51.     private static AccessControlService _singleton;
  52.     private boolean _bServiceAvailable = true;
  53.     private IAccessControlServiceProvider _provider;

  54.     /**
  55.      * Private constructor
  56.      */
  57.     private AccessControlService( )
  58.     {
  59.         try
  60.         {
  61.             _provider = SpringContextService.getBean( "accesscontrol.accessControlServiceProvider" );
  62.             _bServiceAvailable = ( _provider != null );
  63.         }
  64.         catch( CannotLoadBeanClassException | NoSuchBeanDefinitionException | BeanDefinitionStoreException e )
  65.         {
  66.             _bServiceAvailable = false;
  67.         }
  68.     }

  69.     /**
  70.      * Check if the access control service is available. To be available, the following conditions must be verified :
  71.      * <ul>
  72.      * <li>the Bean service is not null</li>
  73.      * <li>the plugin-accesscontrol must be enable</li>
  74.      * </ul>
  75.      *
  76.      * @return true if the workflow service is available
  77.      */
  78.     public boolean isAvailable( )
  79.     {
  80.         return _bServiceAvailable && ( _provider != null ) && PluginService.isPluginEnable( "accesscontrol" );
  81.     }

  82.     /**
  83.      * Returns the unique instance of the service
  84.      *
  85.      * @return The instance of the service
  86.      */
  87.     public static synchronized AccessControlService getInstance( )
  88.     {
  89.         if ( _singleton == null )
  90.         {
  91.             _singleton = new AccessControlService( );
  92.         }
  93.         return _singleton;
  94.     }

  95.     /**
  96.      * return a reference list which contains a list enabled AccessControl
  97.      *
  98.      * @param user
  99.      *            the User
  100.      * @param locale
  101.      *            the locale
  102.      * @return a reference list which contains a list enabled AccessControl
  103.      */
  104.     public ReferenceList getAccessControlsEnabled( User user, Locale locale )
  105.     {
  106.         return isAvailable( ) ? _provider.getAccessControlsEnabled( user, locale ) : null;
  107.     }

  108.     /**
  109.      * Find the access control used by a resource.
  110.      *
  111.      * @param idResource
  112.      * @param resourceType
  113.      * @return the id of the access control, -1 if none
  114.      */
  115.     public int findAccessControlForResource( int idResource, String resourceType )
  116.     {
  117.         return isAvailable( ) ? _provider.findAccessControlForResource( idResource, resourceType ) : -1;
  118.     }

  119.     /**
  120.      * Links the given resource to the given access control. <br />
  121.      * if idAccessControl = -1, deletes the link between the resource and any access control.
  122.      *
  123.      * @param idResource
  124.      * @param resourceType
  125.      * @param idAccessControl
  126.      */
  127.     public void linkResourceToAccessControl( int idResource, String resourceType, int idAccessControl )
  128.     {
  129.         if ( isAvailable( ) )
  130.         {
  131.             _provider.createOrUpdateAccessControlResource( idResource, resourceType, idAccessControl );
  132.         }
  133.     }

  134.     /**
  135.      * Redirects to the Access Control exists if the resource has an AccesControl tha has not already been validated
  136.      *
  137.      * @param request
  138.      * @param idResource
  139.      * @param resourceType
  140.      * @return
  141.      */
  142.     public XPage doExecuteAccessControl( HttpServletRequest request, int idResource, String resourceType, Object destination )
  143.     {
  144.         if ( isAvailable( ) )
  145.         {
  146.             int idAccessControl = findAccessControlForResource( idResource, resourceType );
  147.             if ( idAccessControl != -1 )
  148.             {
  149.                 AccessControlSessionData sessionData = _provider.getSessionDataForResource( request, idResource, resourceType );
  150.                 if ( sessionData == null || !sessionData.isAccessControlResult( ) )
  151.                 {
  152.                     return _provider.redirectToAccessControlXPage( request, idResource, resourceType, idAccessControl );
  153.                 }
  154.                 if ( sessionData.isAccessControlResult( ) )
  155.                 {
  156.                     _provider.applyPersistentData( sessionData, destination );
  157.                 }
  158.             }
  159.         }
  160.         return null;
  161.     }

  162.     /**
  163.      * Remove the Session Data for the give Data
  164.      *
  165.      * @param request
  166.      * @param idResource
  167.      * @param resourceType
  168.      */
  169.     public void cleanSessionData( HttpServletRequest request, int idResource, String resourceType )
  170.     {
  171.         if ( isAvailable( ) )
  172.         {
  173.             _provider.deleteSessionDataForResource( request, idResource, resourceType );
  174.         }
  175.     }
  176. }