View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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  
35  package fr.paris.lutece.plugins.vault.web;
36  
37  import com.bettercloud.vault.VaultException;
38  import com.bettercloud.vault.rest.RestException;
39  import fr.paris.lutece.plugins.vault.business.ApplicationHome;
40  import fr.paris.lutece.plugins.vault.service.EnvironnementUtil;
41  import fr.paris.lutece.plugins.vault.service.VaultService;
42  import fr.paris.lutece.plugins.vault.service.VaultUtil;
43  import fr.paris.lutece.portal.service.i18n.I18nService;
44  import fr.paris.lutece.portal.service.message.AdminMessage;
45  import fr.paris.lutece.portal.service.message.AdminMessageService;
46  import fr.paris.lutece.portal.service.security.SecurityTokenService;
47  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
48  import fr.paris.lutece.portal.service.util.AppException;
49  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
50  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
51  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
52  import fr.paris.lutece.util.ReferenceList;
53  import fr.paris.lutece.util.url.UrlItem;
54  import fr.paris.lutece.util.html.AbstractPaginator;
55  
56  import java.util.*;
57  import java.util.stream.Collectors;
58  import javax.servlet.http.HttpServletRequest;
59  import fr.paris.lutece.plugins.vault.business.Environnement;
60  import fr.paris.lutece.plugins.vault.business.EnvironnementHome;
61  
62  /**
63   * The type Environnement jsp bean.
64   */
65  @Controller( controllerJsp = "ManageEnvironnements.jsp", controllerPath = "jsp/admin/plugins/vault/", right = "VAULT_MANAGEMENT" )
66  public class EnvironnementJspBean extends AbstractManageApplicationJspBean<Integer, Environnement>
67  {
68      // Templates
69      private static final String TEMPLATE_MANAGE_ENVIRONNEMENTS = "/admin/plugins/vault/manage_environnements.html";
70      private static final String TEMPLATE_CREATE_ENVIRONNEMENT = "/admin/plugins/vault/create_environnement.html";
71      private static final String TEMPLATE_MODIFY_ENVIRONNEMENT = "/admin/plugins/vault/modify_environnement.html";
72  
73      // Parameters
74      private static final String PARAMETER_ID_ENVIRONNEMENT = "id";
75  
76      private static final String PARAMETER_ID_APPLICATION = "idApp";
77  
78      // Properties for page titles
79      private static final String PROPERTY_PAGE_TITLE_MANAGE_ENVIRONNEMENTS = "vault.manage_environnements.pageTitle";
80      private static final String PROPERTY_PAGE_TITLE_MODIFY_ENVIRONNEMENT = "vault.modify_environnement.pageTitle";
81      private static final String PROPERTY_PAGE_TITLE_CREATE_ENVIRONNEMENT = "vault.create_environnement.pageTitle";
82  
83      // Markers
84      private static final String MARK_ENVIRONNEMENT_LIST = "environnement_list";
85      private static final String MARK_ENVIRONNEMENT_NAMES_EXAMPLE = "environnement_names_example";
86  
87      /**
88       * The constant MARK_ENVIRONNEMENT.
89       */
90      public static final String MARK_ENVIRONNEMENT = "environnement";
91  
92      private static final String JSP_MANAGE_ENVIRONNEMENTS = "jsp/admin/plugins/vault/ManageEnvironnements.jsp";
93  
94      // Properties
95      private static final String MESSAGE_CONFIRM_REMOVE_ENVIRONNEMENT = "vault.message.confirmRemoveEnvironnement";
96  
97      // Validations
98      private static final String VALIDATION_ATTRIBUTES_PREFIX = "vault.model.entity.environnement.attribute.";
99  
100     // Views
101     private static final String VIEW_MANAGE_ENVIRONNEMENTS = "manageEnvironnements";
102     private static final String VIEW_CREATE_ENVIRONNEMENT = "createEnvironnement";
103     private static final String VIEW_MODIFY_ENVIRONNEMENT = "modifyEnvironnement";
104 
105     // Actions
106     private static final String ACTION_CREATE_ENVIRONNEMENT = "createEnvironnement";
107     private static final String ACTION_MODIFY_ENVIRONNEMENT = "modifyEnvironnement";
108     private static final String ACTION_REMOVE_ENVIRONNEMENT = "removeEnvironnement";
109     private static final String ACTION_CONFIRM_REMOVE_ENVIRONNEMENT = "confirmRemoveEnvironnement";
110     private static final String ACTION_REGENERATE_TOKEN = "regenerateToken";
111 
112     // Infos
113     private static final String INFO_ENVIRONNEMENT_CREATED = "vault.info.environnement.created";
114     private static final String INFO_TOKEN_REGENERATE = "vault.info.token.regenerate";
115 
116     private static final String INFO_ENVIRONNEMENT_UPDATED = "vault.info.environnement.updated";
117     private static final String INFO_ENVIRONNEMENT_REMOVED = "vault.info.environnement.removed";
118 
119     // Errors
120     private static final String ERROR_RESOURCE_NOT_FOUND = "Resource not found";
121 
122     // Session variable to store working values
123     private Environnement _environnement;
124 
125     private List<Integer> _listIdEnvironnements;
126     private ReferenceList _listNamesEnvironnements;
127 
128     /**
129      * Gets manage environnements.
130      *
131      * @param request
132      *            the request
133      * @return the manage environnements
134      * @throws VaultException
135      *             the vault exception
136      */
137     @View( value = VIEW_MANAGE_ENVIRONNEMENTS, defaultView = true )
138     public String getManageEnvironnements( HttpServletRequest request ) throws VaultException
139     {
140         _environnement = ( _environnement != null ) ? _environnement : new Environnement( );
141 
142         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPLICATION ) );
143 
144         if ( request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX ) == null || _listIdEnvironnements.isEmpty( ) )
145         {
146             _listIdEnvironnements = EnvironnementHome.getIdEnvironnementsListByApp( nId );
147         }
148 
149         if ( request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX ) == null || _listNamesEnvironnements.isEmpty( ) )
150         {
151             _listNamesEnvironnements = EnvironnementUtil.getEnvironnementNames( getLocale( ) );
152         }
153 
154         Map<String, Object> model = getPaginatedListModel( request, MARK_ENVIRONNEMENT_LIST, _listIdEnvironnements, JSP_MANAGE_ENVIRONNEMENTS );
155         model.put( ApplicationJspBean.MARK_APPLICATION, ApplicationHome.findByPrimaryKey( nId ).get( ) );
156         model.put( MARK_ENVIRONNEMENT_NAMES_EXAMPLE, _listNamesEnvironnements );
157         model.put( MARK_ENVIRONNEMENT, _environnement );
158         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_ENVIRONNEMENT ) );
159 
160         return getPage( PROPERTY_PAGE_TITLE_MANAGE_ENVIRONNEMENTS, TEMPLATE_MANAGE_ENVIRONNEMENTS, model );
161     }
162 
163     @Override
164     List<Environnement> getItemsFromIds( List<Integer> listIds )
165     {
166         List<Environnement> listEnvironnement = EnvironnementHome.getEnvironnementsListByIds( listIds );
167 
168         // keep original order
169         return listEnvironnement.stream( ).sorted( Comparator.comparingInt( notif -> listIds.indexOf( notif.getId( ) ) ) ).collect( Collectors.toList( ) );
170     }
171 
172     /**
173      * Reset list id.
174      */
175     public void resetListId( )
176     {
177         _listIdEnvironnements = new ArrayList<>( );
178     }
179 
180     /**
181      * Gets create environnement.
182      *
183      * @param request
184      *            the request
185      * @return the create environnement
186      */
187     @View( VIEW_CREATE_ENVIRONNEMENT )
188     public String getCreateEnvironnement( HttpServletRequest request )
189     {
190         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPLICATION ) );
191 
192         _environnement = ( _environnement != null ) ? _environnement : new Environnement( );
193 
194         if ( request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX ) == null || _listNamesEnvironnements.isEmpty( ) )
195         {
196             _listNamesEnvironnements = EnvironnementUtil.getEnvironnementNames( getLocale( ) );
197         }
198         Map<String, Object> model = getModel( );
199         model.put( ApplicationJspBean.MARK_APPLICATION, ApplicationHome.findByPrimaryKey( nId ).get( ) );
200         model.put( MARK_ENVIRONNEMENT_NAMES_EXAMPLE, _listNamesEnvironnements );
201         model.put( MARK_ENVIRONNEMENT, _environnement );
202         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_ENVIRONNEMENT ) );
203 
204         return getPage( PROPERTY_PAGE_TITLE_CREATE_ENVIRONNEMENT, TEMPLATE_CREATE_ENVIRONNEMENT, model );
205     }
206 
207     /**
208      * Do create environnement string.
209      *
210      * @param request
211      *            the request
212      * @return the string
213      * @throws AccessDeniedException
214      *             the access denied exception
215      * @throws VaultException
216      *             the vault exception
217      * @throws RestException
218      *             the rest exception
219      */
220     @Action( ACTION_CREATE_ENVIRONNEMENT )
221     public String doCreateEnvironnement( HttpServletRequest request ) throws AccessDeniedException, VaultException, RestException
222     {
223 
224         populate( _environnement, request, getLocale( ) );
225 
226         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_ENVIRONNEMENT ) )
227         {
228             throw new AccessDeniedException( "Invalid security token" );
229         }
230 
231         // Check constraints
232         if ( !validateBean( _environnement, VALIDATION_ATTRIBUTES_PREFIX ) )
233         {
234             return redirectView( request, VIEW_CREATE_ENVIRONNEMENT );
235         }
236 
237         List<Environnement> listEnv = EnvironnementHome.getEnvironnementListByType( _environnement.getType( ) );
238         _environnement.setCode( _environnement.getType( ) + listEnv.size( ) );
239         EnvironnementHome.create( _environnement );
240         addInfo( INFO_ENVIRONNEMENT_CREATED, getLocale( ) );
241         String strToken = VaultService.getInstance( )
242                 .createEnvironnementToken( ApplicationHome.findByPrimaryKey( _environnement.getIdapplication( ) ).get( ).getCode( ), _environnement );
243         Object [ ] tabObj = {
244                 strToken
245         };
246         addWarning( I18nService.getLocalizedString( "vault.manage_environnement.create_token", tabObj, getLocale( ) ) );
247 
248         resetListId( );
249         return redirect( request, VIEW_MANAGE_ENVIRONNEMENTS, PARAMETER_ID_APPLICATION, _environnement.getIdapplication( ) );
250     }
251 
252     /**
253      * Do regenerate token string.
254      *
255      * @param request
256      *            the request
257      * @return the string
258      * @throws VaultException
259      *             the vault exception
260      */
261     @Action( ACTION_REGENERATE_TOKEN )
262     public String doRegenerateToken( HttpServletRequest request ) throws VaultException
263     {
264 
265         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENVIRONNEMENT ) );
266 
267         if ( _environnement == null || ( _environnement.getId( ) != nId ) )
268         {
269             Optional<Environnement> optEnvironnement = EnvironnementHome.findByPrimaryKey( nId );
270             _environnement = optEnvironnement.orElseThrow( ( ) -> new AppException( ERROR_RESOURCE_NOT_FOUND ) );
271         }
272 
273         addInfo( INFO_TOKEN_REGENERATE, getLocale( ) );
274         String strToken = VaultService.getInstance( ).regenerateToken( ApplicationHome.findByPrimaryKey( _environnement.getIdapplication( ) ).get( ).getCode( ),
275                 _environnement );
276         Object [ ] tabObj = {
277                 strToken
278         };
279         addWarning( I18nService.getLocalizedString( "vault.manage_environnement.create_token", tabObj, getLocale( ) ) );
280         return redirect( request, VIEW_MANAGE_ENVIRONNEMENTS, PARAMETER_ID_APPLICATION, _environnement.getIdapplication( ) );
281     }
282 
283     /**
284      * Gets confirm remove environnement.
285      *
286      * @param request
287      *            the request
288      * @return the confirm remove environnement
289      */
290     @Action( ACTION_CONFIRM_REMOVE_ENVIRONNEMENT )
291     public String getConfirmRemoveEnvironnement( HttpServletRequest request )
292     {
293         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENVIRONNEMENT ) );
294         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_ENVIRONNEMENT ) );
295         url.addParameter( PARAMETER_ID_ENVIRONNEMENT, nId );
296 
297         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ENVIRONNEMENT, url.getUrl( ),
298                 AdminMessage.TYPE_CONFIRMATION );
299 
300         return redirect( request, strMessageUrl );
301     }
302 
303     /**
304      * Do remove environnement string.
305      *
306      * @param request
307      *            the request
308      * @return the string
309      * @throws VaultException
310      *             the vault exception
311      */
312     @Action( ACTION_REMOVE_ENVIRONNEMENT )
313     public String doRemoveEnvironnement( HttpServletRequest request ) throws VaultException
314     {
315         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENVIRONNEMENT ) );
316         int nIdApp = EnvironnementHome.findByPrimaryKey( nId ).get( ).getIdapplication( );
317 
318         VaultService.getInstance( ).removeEnv( EnvironnementHome.findByPrimaryKey( nId ).get( ).getToken( ),
319                 ApplicationHome.findByPrimaryKey( nIdApp ).get( ).getCode( ), EnvironnementHome.findByPrimaryKey( nId ).get( ) );
320         EnvironnementHome.remove( nId );
321         addInfo( INFO_ENVIRONNEMENT_REMOVED, getLocale( ) );
322         resetListId( );
323 
324         return redirect( request, VIEW_MANAGE_ENVIRONNEMENTS, PARAMETER_ID_APPLICATION, nIdApp );
325     }
326 
327     /**
328      * Gets modify environnement.
329      *
330      * @param request
331      *            the request
332      * @return the modify environnement
333      * @throws VaultException
334      *             the vault exception
335      */
336     @View( VIEW_MODIFY_ENVIRONNEMENT )
337     public String getModifyEnvironnement( HttpServletRequest request ) throws VaultException
338     {
339          int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENVIRONNEMENT ) );
340 
341         if ( _environnement == null || ( _environnement.getId( ) != nId ) )
342         {
343             Optional<Environnement> optEnvironnement = EnvironnementHome.findByPrimaryKey( nId );
344             _environnement = optEnvironnement.orElseThrow( ( ) -> new AppException( ERROR_RESOURCE_NOT_FOUND ) );
345         }
346 
347         if ( request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX ) == null || _listNamesEnvironnements.isEmpty( ) )
348         {
349             _listNamesEnvironnements = EnvironnementUtil.getEnvironnementNames( getLocale( ) );
350         }
351 
352         Map<String, Object> model = getModel( );
353         model.put( MARK_ENVIRONNEMENT, _environnement );
354         model.put( ApplicationJspBean.MARK_APPLICATION, ApplicationHome.findByPrimaryKey( _environnement.getIdapplication( ) ).get( ) );
355         model.put( MARK_ENVIRONNEMENT_NAMES_EXAMPLE, _listNamesEnvironnements );
356         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_ENVIRONNEMENT ) );
357 
358         return getPage( PROPERTY_PAGE_TITLE_MODIFY_ENVIRONNEMENT, TEMPLATE_MODIFY_ENVIRONNEMENT, model );
359     }
360 
361     /**
362      * Do modify environnement string.
363      *
364      * @param request
365      *            the request
366      * @return the string
367      * @throws AccessDeniedException
368      *             the access denied exception
369      */
370     @Action( ACTION_MODIFY_ENVIRONNEMENT )
371     public String doModifyEnvironnement( HttpServletRequest request ) throws AccessDeniedException, VaultException {
372 
373         String strOldCode = request.getParameter( "oldCode" );
374         String strOldToken = request.getParameter( "oldToken" );
375 
376         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENVIRONNEMENT ) );
377         int nIdApp = EnvironnementHome.findByPrimaryKey( nId ).get( ).getIdapplication( );
378 
379         populate( _environnement, request, getLocale( ) );
380 
381         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_ENVIRONNEMENT ) )
382         {
383             throw new AccessDeniedException( "Invalid security token" );
384         }
385 
386         // Check constraints
387         if ( !validateBean( _environnement, VALIDATION_ATTRIBUTES_PREFIX ) )
388         {
389             return redirect( request, VIEW_MODIFY_ENVIRONNEMENT, PARAMETER_ID_ENVIRONNEMENT, _environnement.getId( ) );
390         }
391 
392         addInfo( INFO_ENVIRONNEMENT_UPDATED, getLocale( ) );
393 
394 
395         String strToken = VaultService.getInstance( )
396                 .createEnvironnementToken( ApplicationHome.findByPrimaryKey( _environnement.getIdapplication( ) ).get( ).getCode( ), _environnement );
397         Object [ ] tabObj = {
398                 strToken
399         };
400         addWarning( I18nService.getLocalizedString( "vault.manage_environnement.create_token", tabObj, getLocale( ) ) );
401 
402         EnvironnementHome.update( _environnement, strOldCode, strOldToken );
403 
404         resetListId( );
405 
406         return redirect( request, VIEW_MANAGE_ENVIRONNEMENTS, PARAMETER_ID_APPLICATION, _environnement.getIdapplication( ) );
407 
408     }
409 }