View Javadoc
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  
36  import fr.paris.lutece.portal.business.right.Right;
37  import fr.paris.lutece.portal.business.right.RightHome;
38  import fr.paris.lutece.portal.business.user.AdminUser;
39  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
40  import fr.paris.lutece.portal.service.admin.AdminUserService;
41  import fr.paris.lutece.portal.service.admin.PasswordResetException;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.portal.service.util.AppPathService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.util.bean.BeanUtil;
47  import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
48  import fr.paris.lutece.util.beanvalidation.ValidationError;
49  import fr.paris.lutece.util.beanvalidation.ValidationErrorConfig;
50  import fr.paris.lutece.util.html.HtmlTemplate;
51  
52  import java.io.Serializable;
53  
54  import java.util.HashMap;
55  import java.util.List;
56  import java.util.Locale;
57  import java.util.Map;
58  import java.util.Set;
59  
60  import javax.servlet.http.HttpServletRequest;
61  
62  import javax.validation.ConstraintViolation;
63  
64  /**
65   * Provides generic methods for jspBeans
66   */
67  public abstract class AdminFeaturesPageJspBean implements Serializable
68  {
69      protected static final String JSP_TECHNICAL_ADMINISTRATION = "jsp/admin/AdminTechnicalMenu.jsp";
70      protected static final String ERROR_INVALID_TOKEN = "Invalid security token";
71  
72      /**
73       * Serial version UID
74       */
75      private static final long serialVersionUID = -7952383741759547934L;
76  
77      // template for all admin pages
78      private static final String TEMPLATE_MAIN = "/admin/feature_frameset.html";
79  
80      // bookmarks
81      private static final String MARK_FEATURE_URL = "feature_url";
82      private static final String MARK_FEATURE_TITLE = "feature_title";
83      private static final String MARK_FEATURE_ICON = "feature_icon";
84      private static final String MARK_FEATURE_DOCUMENTATION = "feature_documentation";
85      private static final String MARK_FEATURE_GROUP = "feature_group";
86      private static final String MARK_PAGE_TITLE = "page_title";
87      private static final String MARK_PAGE_CONTENT = "page_content";
88  
89      // Properties
90      private static final String PROPERTY_DEFAULT_FEATURE_ICON = "lutece.admin.feature.default.icon";
91      private static final String PROPERTY_RESET_EXCEPTION_MESSAGE = "User must reset his password.";
92  
93      // private fields
94      private String _strFeatureLabel;
95      private String _strFeatureUrl;
96      private String _strFeatureIcon;
97      private String _strFeatureDocumentation;
98      private String _strFeatureGroup;
99      private String _strPageTitleKey;
100     private Locale _locale;
101     private AdminUser _user;
102 
103     /**
104      * Initialize the jspbean data Allows to set the feature url and feature title associated
105      * 
106      * @param request
107      *            the HTTP request
108      * @param strRight
109      *            The right
110      * @throws AccessDeniedException
111      *             Access denied exception
112      * @throws PasswordResetException
113      *             Password reset exception
114      */
115     public void init( HttpServletRequest request, String strRight ) throws AccessDeniedException
116     {
117         _user = AdminUserService.getAdminUser( request );
118         Right right = RightHome.findByPrimaryKey( strRight );
119 
120         if ( right == null )
121         {
122             throw new AccessDeniedException( strRight + " right does not exist for user "+_user.getAccessCode( )+"." );
123         }
124         
125         if ( !_user.checkRight( strRight ) )
126         {
127             throw new AccessDeniedException( "User " + _user.getAccessCode( ) + " does not have " + strRight + " right." );
128         }
129 
130         if ( _user.isPasswordReset( ) )
131         {
132             throw new PasswordResetException( PROPERTY_RESET_EXCEPTION_MESSAGE );
133         }
134 
135         // get the locale
136         _locale = _user.getLocale( );
137 
138         right.setLocale( _locale );
139         _strFeatureLabel = right.getName( );
140         _strFeatureUrl = right.getUrl( );
141         _strFeatureIcon = right.getIconUrl( );
142         _strFeatureDocumentation = right.getDocumentationUrl( );
143         _strFeatureGroup = right.getFeatureGroup( );
144     }
145 
146     /**
147      * Set the page title property
148      * 
149      * @param strPageTitleKey
150      *            The page title property
151      */
152     public void setPageTitleProperty( String strPageTitleKey )
153     {
154         _strPageTitleKey = strPageTitleKey;
155     }
156 
157     /**
158      * Get the page title
159      * 
160      * @return The page title
161      */
162     public String getPageTitle( )
163     {
164         return ( _strPageTitleKey != null ) ? I18nService.getLocalizedString( _strPageTitleKey, getLocale( ) ) : "";
165     }
166 
167     /**
168      * Returns the Locale
169      *
170      * @return The Locale
171      */
172     public Locale getLocale( )
173     {
174         return _locale;
175     }
176 
177     /**
178      * Returns the AdminUser
179      *
180      * @return The AdminUser
181      */
182     public AdminUser getUser( )
183     {
184         return _user;
185     }
186 
187     /**
188      * Returns the feature home Url
189      * 
190      * @param request
191      *            The HTTP request
192      * @return The feature home Url
193      */
194     public String getHomeUrl( HttpServletRequest request )
195     {
196         return AppPathService.getBaseUrl( request ) + _strFeatureUrl;
197     }
198 
199     /**
200      * Returns the feature icon Url
201      * 
202      * @return The feature icon Url
203      */
204     public String getFeatureIcon( )
205     {
206         return _strFeatureIcon;
207     }
208 
209     /**
210      * Sets the feature icon url
211      * 
212      * @param strFeatureIcon
213      *            the feature icon url
214      */
215     public void setFeatureIcon( String strFeatureIcon )
216     {
217         _strFeatureIcon = strFeatureIcon;
218     }
219 
220     /**
221      * Sets the feature group
222      * 
223      * @param strFeatureGroup
224      *            the feature group
225      */
226     public void setFeatureGroup( String strFeatureGroup )
227     {
228         _strFeatureGroup = strFeatureGroup;
229     }
230 
231     /**
232      * Get the admin page from a content data
233      *
234      * @return the html code for the admin page for the given content
235      * @param strContent
236      *            the data to load in the admin page
237      */
238     public String getAdminPage( String strContent )
239     {
240         Map<String, String> rootModel = new HashMap<>( );
241 
242         rootModel.put( MARK_FEATURE_URL, _strFeatureUrl );
243         rootModel.put( MARK_FEATURE_TITLE, _strFeatureLabel );
244 
245         String strIconUrl = ( _strFeatureIcon != null ) ? _strFeatureIcon : AppPropertiesService.getProperty( PROPERTY_DEFAULT_FEATURE_ICON );
246         rootModel.put( MARK_FEATURE_ICON, strIconUrl );
247 
248         String strDocumentationUrl = null;
249 
250         if ( _strFeatureDocumentation != null )
251         {
252             strDocumentationUrl = _strFeatureDocumentation;
253         }
254 
255         rootModel.put( MARK_FEATURE_DOCUMENTATION, strDocumentationUrl );
256         rootModel.put( MARK_FEATURE_GROUP, _strFeatureGroup );
257 
258         rootModel.put( MARK_PAGE_TITLE, getPageTitle( ) );
259         rootModel.put( MARK_PAGE_CONTENT, strContent );
260 
261         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIN, getLocale( ), rootModel );
262 
263         return template.getHtml( );
264     }
265 
266     /**
267      * Populate a bean using parameters in http request
268      * 
269      * @param bean
270      *            bean to populate
271      * @param request
272      *            http request
273      */
274     protected void populate( Object bean, HttpServletRequest request )
275     {
276         populate( bean, request, null );
277     }
278 
279     /**
280      * Populate a bean using parameters in http request, with locale date format controls
281      * 
282      * @param bean
283      *            bean to populate
284      * @param request
285      *            http request
286      * @param locale
287      *            the locale
288      */
289     protected void populate( Object bean, HttpServletRequest request, Locale locale )
290     {
291         if ( locale == null )
292         {
293             BeanUtil.populate( bean, request,  getLocale( ) );
294         }
295         else
296         {
297             BeanUtil.populate( bean, request, locale );
298         }
299     }
300 
301     /**
302      * Validates a bean.
303      *
304      * @param <T>
305      *            the bean type
306      * @param bean
307      *            the bean to validate
308      * @return the sets of constraints that has been violated
309      */
310     public <T> Set<ConstraintViolation<T>> validate( T bean )
311     {
312         return BeanValidationUtil.validate( bean );
313     }
314 
315     /**
316      * Validates a bean
317      * 
318      * @param <T>
319      *            The bean type
320      * @param bean
321      *            The bean to validate
322      * @param strFieldsKeyPrefix
323      *            The fields keys prefix in resources files
324      * @return The error list
325      */
326     public <T> List<ValidationError> validate( T bean, String strFieldsKeyPrefix )
327     {
328         return BeanValidationUtil.validate( bean, getLocale( ), strFieldsKeyPrefix );
329     }
330 
331     /**
332      * Validates a bean
333      * 
334      * @param <T>
335      *            The bean type
336      * @param bean
337      *            The bean to validate
338      * @param config
339      *            The config for Error validation rendering
340      * @return The error list
341      */
342     public <T> List<ValidationError> validate( T bean, ValidationErrorConfig config )
343     {
344         return BeanValidationUtil.validate( bean, getLocale( ), config );
345     }
346 
347     /**
348      * Return the URL of the technical admin page
349      * 
350      * @param request
351      *            The request
352      * @param strAnchor
353      *            An anchor inside the page
354      * @return the URL
355      */
356     protected String getAdminDashboardsUrl( HttpServletRequest request, String strAnchor )
357     {
358         return AppPathService.getBaseUrl( request ) + JSP_TECHNICAL_ADMINISTRATION + "?#" + strAnchor;
359     }
360 
361     /**
362      * return true if the user is of higher level than the connected user or if the connected user is admin
363      * 
364      * @param user
365      * @return
366      */
367     protected boolean isUserHigherThanConnectedUser( AdminUser user )
368     {
369         return user.getUserLevel( ) > getUser( ).getUserLevel( ) || getUser( ).isAdmin( );
370     }
371 }