AdminFeaturesPageJspBean.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.web.admin;

  35. import fr.paris.lutece.portal.business.right.Right;
  36. import fr.paris.lutece.portal.business.right.RightHome;
  37. import fr.paris.lutece.portal.business.user.AdminUser;
  38. import fr.paris.lutece.portal.service.admin.AccessDeniedException;
  39. import fr.paris.lutece.portal.service.admin.AdminUserService;
  40. import fr.paris.lutece.portal.service.admin.PasswordResetException;
  41. import fr.paris.lutece.portal.service.i18n.I18nService;
  42. import fr.paris.lutece.portal.service.template.AppTemplateService;
  43. import fr.paris.lutece.portal.service.util.AppPathService;
  44. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  45. import fr.paris.lutece.util.bean.BeanUtil;
  46. import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
  47. import fr.paris.lutece.util.beanvalidation.ValidationError;
  48. import fr.paris.lutece.util.beanvalidation.ValidationErrorConfig;
  49. import fr.paris.lutece.util.html.HtmlTemplate;

  50. import java.io.Serializable;

  51. import java.util.HashMap;
  52. import java.util.List;
  53. import java.util.Locale;
  54. import java.util.Map;
  55. import java.util.Set;

  56. import javax.servlet.http.HttpServletRequest;

  57. import javax.validation.ConstraintViolation;

  58. /**
  59.  * Provides generic methods for jspBeans
  60.  */
  61. public abstract class AdminFeaturesPageJspBean implements Serializable
  62. {
  63.     protected static final String JSP_TECHNICAL_ADMINISTRATION = "jsp/admin/AdminTechnicalMenu.jsp";
  64.     protected static final String ERROR_INVALID_TOKEN = "Invalid security token";

  65.     /**
  66.      * Serial version UID
  67.      */
  68.     private static final long serialVersionUID = -7952383741759547934L;

  69.     // template for all admin pages
  70.     private static final String TEMPLATE_MAIN = "/admin/feature_frameset.html";

  71.     // bookmarks
  72.     private static final String MARK_FEATURE_URL = "feature_url";
  73.     private static final String MARK_FEATURE_TITLE = "feature_title";
  74.     private static final String MARK_FEATURE_ICON = "feature_icon";
  75.     private static final String MARK_FEATURE_DOCUMENTATION = "feature_documentation";
  76.     private static final String MARK_FEATURE_GROUP = "feature_group";
  77.     private static final String MARK_PAGE_TITLE = "page_title";
  78.     private static final String MARK_PAGE_CONTENT = "page_content";

  79.     // Properties
  80.     private static final String PROPERTY_DEFAULT_FEATURE_ICON = "lutece.admin.feature.default.icon";
  81.     private static final String PROPERTY_RESET_EXCEPTION_MESSAGE = "User must reset his password.";

  82.     // private fields
  83.     private String _strFeatureLabel;
  84.     private String _strFeatureUrl;
  85.     private String _strFeatureIcon;
  86.     private String _strFeatureDocumentation;
  87.     private String _strFeatureGroup;
  88.     private String _strPageTitleKey;
  89.     private Locale _locale;
  90.     private AdminUser _user;

  91.     /**
  92.      * Initialize the jspbean data Allows to set the feature url and feature title associated
  93.      *
  94.      * @param request
  95.      *            the HTTP request
  96.      * @param strRight
  97.      *            The right
  98.      * @throws AccessDeniedException
  99.      *             Access denied exception
  100.      * @throws PasswordResetException
  101.      *             Password reset exception
  102.      */
  103.     public void init( HttpServletRequest request, String strRight ) throws AccessDeniedException
  104.     {
  105.         _user = AdminUserService.getAdminUser( request );
  106.         Right right = RightHome.findByPrimaryKey( strRight );

  107.         if ( right == null )
  108.         {
  109.             throw new AccessDeniedException( strRight + " right does not exist for user "+_user.getAccessCode( )+"." );
  110.         }
  111.        
  112.         if ( !_user.checkRight( strRight ) )
  113.         {
  114.             throw new AccessDeniedException( "User " + _user.getAccessCode( ) + " does not have " + strRight + " right." );
  115.         }

  116.         if ( _user.isPasswordReset( ) )
  117.         {
  118.             throw new PasswordResetException( PROPERTY_RESET_EXCEPTION_MESSAGE );
  119.         }

  120.         // get the locale
  121.         _locale = _user.getLocale( );

  122.         right.setLocale( _locale );
  123.         _strFeatureLabel = right.getName( );
  124.         _strFeatureUrl = right.getUrl( );
  125.         _strFeatureIcon = right.getIconUrl( );
  126.         _strFeatureDocumentation = right.getDocumentationUrl( );
  127.         _strFeatureGroup = right.getFeatureGroup( );
  128.     }

  129.     /**
  130.      * Set the page title property
  131.      *
  132.      * @param strPageTitleKey
  133.      *            The page title property
  134.      */
  135.     public void setPageTitleProperty( String strPageTitleKey )
  136.     {
  137.         _strPageTitleKey = strPageTitleKey;
  138.     }

  139.     /**
  140.      * Get the page title
  141.      *
  142.      * @return The page title
  143.      */
  144.     public String getPageTitle( )
  145.     {
  146.         return ( _strPageTitleKey != null ) ? I18nService.getLocalizedString( _strPageTitleKey, getLocale( ) ) : "";
  147.     }

  148.     /**
  149.      * Returns the Locale
  150.      *
  151.      * @return The Locale
  152.      */
  153.     public Locale getLocale( )
  154.     {
  155.         return _locale;
  156.     }

  157.     /**
  158.      * Returns the AdminUser
  159.      *
  160.      * @return The AdminUser
  161.      */
  162.     public AdminUser getUser( )
  163.     {
  164.         return _user;
  165.     }

  166.     /**
  167.      * Returns the feature home Url
  168.      *
  169.      * @param request
  170.      *            The HTTP request
  171.      * @return The feature home Url
  172.      */
  173.     public String getHomeUrl( HttpServletRequest request )
  174.     {
  175.         return AppPathService.getBaseUrl( request ) + _strFeatureUrl;
  176.     }

  177.     /**
  178.      * Returns the feature icon Url
  179.      *
  180.      * @return The feature icon Url
  181.      */
  182.     public String getFeatureIcon( )
  183.     {
  184.         return _strFeatureIcon;
  185.     }

  186.     /**
  187.      * Sets the feature icon url
  188.      *
  189.      * @param strFeatureIcon
  190.      *            the feature icon url
  191.      */
  192.     public void setFeatureIcon( String strFeatureIcon )
  193.     {
  194.         _strFeatureIcon = strFeatureIcon;
  195.     }

  196.     /**
  197.      * Sets the feature group
  198.      *
  199.      * @param strFeatureGroup
  200.      *            the feature group
  201.      */
  202.     public void setFeatureGroup( String strFeatureGroup )
  203.     {
  204.         _strFeatureGroup = strFeatureGroup;
  205.     }

  206.     /**
  207.      * Get the admin page from a content data
  208.      *
  209.      * @return the html code for the admin page for the given content
  210.      * @param strContent
  211.      *            the data to load in the admin page
  212.      */
  213.     public String getAdminPage( String strContent )
  214.     {
  215.         Map<String, String> rootModel = new HashMap<>( );

  216.         rootModel.put( MARK_FEATURE_URL, _strFeatureUrl );
  217.         rootModel.put( MARK_FEATURE_TITLE, _strFeatureLabel );

  218.         String strIconUrl = ( _strFeatureIcon != null ) ? _strFeatureIcon : AppPropertiesService.getProperty( PROPERTY_DEFAULT_FEATURE_ICON );
  219.         rootModel.put( MARK_FEATURE_ICON, strIconUrl );

  220.         String strDocumentationUrl = null;

  221.         if ( _strFeatureDocumentation != null )
  222.         {
  223.             strDocumentationUrl = _strFeatureDocumentation;
  224.         }

  225.         rootModel.put( MARK_FEATURE_DOCUMENTATION, strDocumentationUrl );
  226.         rootModel.put( MARK_FEATURE_GROUP, _strFeatureGroup );

  227.         rootModel.put( MARK_PAGE_TITLE, getPageTitle( ) );
  228.         rootModel.put( MARK_PAGE_CONTENT, strContent );

  229.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIN, getLocale( ), rootModel );

  230.         return template.getHtml( );
  231.     }

  232.     /**
  233.      * Populate a bean using parameters in http request
  234.      *
  235.      * @param bean
  236.      *            bean to populate
  237.      * @param request
  238.      *            http request
  239.      */
  240.     protected void populate( Object bean, HttpServletRequest request )
  241.     {
  242.         populate( bean, request, null );
  243.     }

  244.     /**
  245.      * Populate a bean using parameters in http request, with locale date format controls
  246.      *
  247.      * @param bean
  248.      *            bean to populate
  249.      * @param request
  250.      *            http request
  251.      * @param locale
  252.      *            the locale
  253.      */
  254.     protected void populate( Object bean, HttpServletRequest request, Locale locale )
  255.     {
  256.         if ( locale == null )
  257.         {
  258.             BeanUtil.populate( bean, request,  getLocale( ) );
  259.         }
  260.         else
  261.         {
  262.             BeanUtil.populate( bean, request, locale );
  263.         }
  264.     }

  265.     /**
  266.      * Validates a bean.
  267.      *
  268.      * @param <T>
  269.      *            the bean type
  270.      * @param bean
  271.      *            the bean to validate
  272.      * @return the sets of constraints that has been violated
  273.      */
  274.     public <T> Set<ConstraintViolation<T>> validate( T bean )
  275.     {
  276.         return BeanValidationUtil.validate( bean );
  277.     }

  278.     /**
  279.      * Validates a bean
  280.      *
  281.      * @param <T>
  282.      *            The bean type
  283.      * @param bean
  284.      *            The bean to validate
  285.      * @param strFieldsKeyPrefix
  286.      *            The fields keys prefix in resources files
  287.      * @return The error list
  288.      */
  289.     public <T> List<ValidationError> validate( T bean, String strFieldsKeyPrefix )
  290.     {
  291.         return BeanValidationUtil.validate( bean, getLocale( ), strFieldsKeyPrefix );
  292.     }

  293.     /**
  294.      * Validates a bean
  295.      *
  296.      * @param <T>
  297.      *            The bean type
  298.      * @param bean
  299.      *            The bean to validate
  300.      * @param config
  301.      *            The config for Error validation rendering
  302.      * @return The error list
  303.      */
  304.     public <T> List<ValidationError> validate( T bean, ValidationErrorConfig config )
  305.     {
  306.         return BeanValidationUtil.validate( bean, getLocale( ), config );
  307.     }

  308.     /**
  309.      * Return the URL of the technical admin page
  310.      *
  311.      * @param request
  312.      *            The request
  313.      * @param strAnchor
  314.      *            An anchor inside the page
  315.      * @return the URL
  316.      */
  317.     protected String getAdminDashboardsUrl( HttpServletRequest request, String strAnchor )
  318.     {
  319.         return AppPathService.getBaseUrl( request ) + JSP_TECHNICAL_ADMINISTRATION + "?#" + strAnchor;
  320.     }

  321.     /**
  322.      * return true if the user is of higher level than the connected user or if the connected user is admin
  323.      *
  324.      * @param user
  325.      * @return
  326.      */
  327.     protected boolean isUserHigherThanConnectedUser( AdminUser user )
  328.     {
  329.         return user.getUserLevel( ) > getUser( ).getUserLevel( ) || getUser( ).isAdmin( );
  330.     }
  331. }