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 
119         if ( !_user.checkRight( strRight ) )
120         {
121             throw new AccessDeniedException( "User " + _user.getAccessCode( ) + " does not have " + strRight + " right." );
122         }
123 
124         if ( _user.isPasswordReset( ) )
125         {
126             throw new PasswordResetException( PROPERTY_RESET_EXCEPTION_MESSAGE );
127         }
128 
129         // get the locale
130         _locale = _user.getLocale( );
131 
132         Right right = RightHome.findByPrimaryKey( strRight );
133         right.setLocale( _locale );
134         _strFeatureLabel = right.getName( );
135         _strFeatureUrl = right.getUrl( );
136         _strFeatureIcon = right.getIconUrl( );
137         _strFeatureDocumentation = right.getDocumentationUrl( );
138         _strFeatureGroup = right.getFeatureGroup( );
139     }
140 
141     /**
142      * Set the page title property
143      * 
144      * @param strPageTitleKey
145      *            The page title property
146      */
147     public void setPageTitleProperty( String strPageTitleKey )
148     {
149         _strPageTitleKey = strPageTitleKey;
150     }
151 
152     /**
153      * Get the page title
154      * 
155      * @return The page title
156      */
157     public String getPageTitle( )
158     {
159         return ( _strPageTitleKey != null ) ? I18nService.getLocalizedString( _strPageTitleKey, getLocale( ) ) : "";
160     }
161 
162     /**
163      * Returns the Locale
164      *
165      * @return The Locale
166      */
167     public Locale getLocale( )
168     {
169         return _locale;
170     }
171 
172     /**
173      * Returns the AdminUser
174      *
175      * @return The AdminUser
176      */
177     public AdminUser getUser( )
178     {
179         return _user;
180     }
181 
182     /**
183      * Returns the feature home Url
184      * 
185      * @param request
186      *            The HTTP request
187      * @return The feature home Url
188      */
189     public String getHomeUrl( HttpServletRequest request )
190     {
191         return AppPathService.getBaseUrl( request ) + _strFeatureUrl;
192     }
193 
194     /**
195      * Returns the feature icon Url
196      * 
197      * @return The feature icon Url
198      */
199     public String getFeatureIcon( )
200     {
201         return _strFeatureIcon;
202     }
203 
204     /**
205      * Sets the feature icon url
206      * 
207      * @param strFeatureIcon
208      *            the feature icon url
209      */
210     public void setFeatureIcon( String strFeatureIcon )
211     {
212         _strFeatureIcon = strFeatureIcon;
213     }
214 
215     /**
216      * Sets the feature group
217      * 
218      * @param strFeatureGroup
219      *            the feature group
220      */
221     public void setFeatureGroup( String strFeatureGroup )
222     {
223         _strFeatureGroup = strFeatureGroup;
224     }
225 
226     /**
227      * Get the admin page from a content data
228      *
229      * @return the html code for the admin page for the given content
230      * @param strContent
231      *            the data to load in the admin page
232      */
233     public String getAdminPage( String strContent )
234     {
235         Map<String, String> rootModel = new HashMap<>( );
236 
237         rootModel.put( MARK_FEATURE_URL, _strFeatureUrl );
238         rootModel.put( MARK_FEATURE_TITLE, _strFeatureLabel );
239 
240         String strIconUrl = ( _strFeatureIcon != null ) ? _strFeatureIcon : AppPropertiesService.getProperty( PROPERTY_DEFAULT_FEATURE_ICON );
241         rootModel.put( MARK_FEATURE_ICON, strIconUrl );
242 
243         String strDocumentationUrl = null;
244 
245         if ( _strFeatureDocumentation != null )
246         {
247             strDocumentationUrl = _strFeatureDocumentation;
248         }
249 
250         rootModel.put( MARK_FEATURE_DOCUMENTATION, strDocumentationUrl );
251         rootModel.put( MARK_FEATURE_GROUP, _strFeatureGroup );
252 
253         rootModel.put( MARK_PAGE_TITLE, getPageTitle( ) );
254         rootModel.put( MARK_PAGE_CONTENT, strContent );
255 
256         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIN, getLocale( ), rootModel );
257 
258         return template.getHtml( );
259     }
260 
261     /**
262      * Populate a bean using parameters in http request
263      * 
264      * @param bean
265      *            bean to populate
266      * @param request
267      *            http request
268      */
269     protected void populate( Object bean, HttpServletRequest request )
270     {
271         populate( bean, request, null );
272     }
273 
274     /**
275      * Populate a bean using parameters in http request, with locale date format controls
276      * 
277      * @param bean
278      *            bean to populate
279      * @param request
280      *            http request
281      * @param locale
282      *            the locale
283      */
284     protected void populate( Object bean, HttpServletRequest request, Locale locale )
285     {
286         if ( locale == null )
287         {
288             BeanUtil.populate( bean, request,  getLocale( ) );
289         }
290         else
291         {
292             BeanUtil.populate( bean, request, locale );
293         }
294     }
295 
296     /**
297      * Validates a bean.
298      *
299      * @param <T>
300      *            the bean type
301      * @param bean
302      *            the bean to validate
303      * @return the sets of constraints that has been violated
304      */
305     public <T> Set<ConstraintViolation<T>> validate( T bean )
306     {
307         return BeanValidationUtil.validate( bean );
308     }
309 
310     /**
311      * Validates a bean
312      * 
313      * @param <T>
314      *            The bean type
315      * @param bean
316      *            The bean to validate
317      * @param strFieldsKeyPrefix
318      *            The fields keys prefix in resources files
319      * @return The error list
320      */
321     public <T> List<ValidationError> validate( T bean, String strFieldsKeyPrefix )
322     {
323         return BeanValidationUtil.validate( bean, getLocale( ), strFieldsKeyPrefix );
324     }
325 
326     /**
327      * Validates a bean
328      * 
329      * @param <T>
330      *            The bean type
331      * @param bean
332      *            The bean to validate
333      * @param config
334      *            The config for Error validation rendering
335      * @return The error list
336      */
337     public <T> List<ValidationError> validate( T bean, ValidationErrorConfig config )
338     {
339         return BeanValidationUtil.validate( bean, getLocale( ), config );
340     }
341 
342     /**
343      * Return the URL of the technical admin page
344      * 
345      * @param request
346      *            The request
347      * @param strAnchor
348      *            An anchor inside the page
349      * @return the URL
350      */
351     protected String getAdminDashboardsUrl( HttpServletRequest request, String strAnchor )
352     {
353         return AppPathService.getBaseUrl( request ) + JSP_TECHNICAL_ADMINISTRATION + "?#" + strAnchor;
354     }
355 
356     /**
357      * return true if the user is of higher level than the connected user or if the connected user is admin
358      * 
359      * @param user
360      * @return
361      */
362     protected boolean isUserHigherThanConnectedUser( AdminUser user )
363     {
364         return user.getUserLevel( ) > getUser( ).getUserLevel( ) || getUser( ).isAdmin( );
365     }
366 }