View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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.plugins.identitystore.web;
35  
36  import fr.paris.lutece.plugins.identitystore.business.application.ClientApplication;
37  import fr.paris.lutece.plugins.identitystore.business.application.ClientApplicationHome;
38  import fr.paris.lutece.plugins.identitystore.business.contract.ServiceContract;
39  import fr.paris.lutece.plugins.identitystore.service.contract.ServiceContractService;
40  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
41  import fr.paris.lutece.portal.service.message.AdminMessage;
42  import fr.paris.lutece.portal.service.message.AdminMessageService;
43  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46  import fr.paris.lutece.util.url.UrlItem;
47  import org.apache.commons.lang3.StringUtils;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import java.util.List;
51  import java.util.Map;
52  import java.util.stream.Collectors;
53  
54  /**
55   * This class provides the user interface to manage ClientApplication management features ( manage, create, modify, remove )
56   */
57  @Controller( controllerJsp = "ManageClientApplications.jsp", controllerPath = "jsp/admin/plugins/identitystore/", right = "IDENTITYSTORE_ADMIN_MANAGEMENT" )
58  public class ManageClientApplicationJspBean extends ManageIdentitiesJspBean
59  {
60      private static final long serialVersionUID = 1L;
61  
62      // Templates
63      private static final String TEMPLATE_MANAGE_CLIENTAPPLICATION = "/admin/plugins/identitystore/clientapplication/manage_clientapplications.html";
64      private static final String TEMPLATE_DISPLAY_CLIENTAPPLICATION = "/admin/plugins/identitystore/clientapplication/view_clientapplication.html";
65      private static final String TEMPLATE_CREATE_CLIENTAPPLICATION = "/admin/plugins/identitystore/clientapplication/create_clientapplication.html";
66      private static final String TEMPLATE_MODIFY_CLIENTAPPLICATION = "/admin/plugins/identitystore/clientapplication/modify_clientapplication.html";
67  
68      // Query param
69      private static final String QUERY_PARAM_CLIENT_APPLICATION_NAME = "client_application_name";
70      private static final String QUERY_PARAM_APPLICATION_CODE = "application_code_search";
71      private static final String QUERY_PARAM_CLIENT_CODE = "client_code_search";
72  
73      // Parameters
74      private static final String PARAMETER_ID_CLIENTAPPLICATION = "id";
75  
76      private static final String PARAMETER_CERTIFIERS_AUTH = "certif_auth";
77      private static final String PARAMETER_CERTIFIER_CODE = "certif_code";
78  
79      // Properties for page titles
80      private static final String PROPERTY_PAGE_TITLE_MANAGE_CLIENTAPPLICATIONS = "identitystore.manage_clientapplications.pageTitle";
81      private static final String PROPERTY_PAGE_TITLE_MODIFY_CLIENTAPPLICATION = "identitystore.modify_clientapplication.pageTitle";
82      private static final String PROPERTY_PAGE_TITLE_CREATE_CLIENTAPPLICATION = "identitystore.create_clientapplication.pageTitle";
83      private static final String PROPERTY_PAGE_TITLE_MODIFY_CLIENTAPPLICATION_CERTIFICATOR = "identitystore.modify_clientapplication_certificator.pageTitle";
84  
85      // Markers
86      private static final String MARK_CLIENTAPPLICATION_LIST = "clientapplication_list";
87      private static final String MARK_CLIENTAPPLICATION = "clientapplication";
88      private static final String MARK_SERVICECONTRACTS = "servicecontract_list";
89      private static final String MARK_ACTIVECONTRACT = "servicecontract_active";
90      private static final String MARK_CLIENTAPPLICATION_CERTIF_LIST = "clientapplication_certifs";
91      private static final String MARK_CLIENTAPPLICATION_CERTIF_CODE_MAP = "map_clientapplication_certifs";
92      private static final String MARK_CERTIFIERS = "certifiers";
93      private static final String MARK_CLIENTAPPLICATION_RIGHT_LIST = "clientapplication_rights_list";
94      private static final String JSP_MANAGE_CLIENTAPPLICATIONS = "jsp/admin/plugins/identitystore/ManageClientApplications.jsp";
95  
96      // Properties
97      private static final String MESSAGE_CONFIRM_REMOVE_CLIENTAPPLICATION = "identitystore.message.confirmRemoveClientApplication";
98  
99      // Validations
100     private static final String VALIDATION_ATTRIBUTES_PREFIX = "identitystore.model.entity.clientapplication.attribute.";
101 
102     // Views
103     private static final String VIEW_MANAGE_CLIENTAPPLICATIONS = "manageClientApplications";
104     private static final String VIEW_DISPLAY_CLIENTAPPLICATIONS = "displayClientApplication";
105     private static final String VIEW_CREATE_CLIENTAPPLICATION = "createClientApplication";
106     private static final String VIEW_MODIFY_CLIENTAPPLICATION = "modifyClientApplication";
107     private static final String VIEW_MANAGE_CLIENTAPPLICATION_CERTIFICATOR = "manageClientApplicationCertificate";
108 
109     // Actions
110     private static final String ACTION_CREATE_CLIENTAPPLICATION = "createClientApplication";
111     private static final String ACTION_MODIFY_CLIENTAPPLICATION = "modifyClientApplication";
112     private static final String ACTION_MANAGE_CLIENTAPPLICATION_CERTIFICATOR = "manageClientApplicationCertificate";
113     private static final String ACTION_REMOVE_CLIENTAPPLICATION = "removeClientApplication";
114     private static final String ACTION_CONFIRM_REMOVE_CLIENTAPPLICATION = "confirmRemoveClientApplication";
115     private static final String ACTION_REMOVE_CLIENTAPPLICATION_CERTIFICATOR = "removeClientApplicationCertificate";
116 
117     // Infos
118     private static final String INFO_CLIENTAPPLICATION_CREATED = "identitystore.info.clientapplication.created";
119     private static final String INFO_CLIENTAPPLICATION_UPDATED = "identitystore.info.clientapplication.updated";
120     private static final String INFO_CLIENTAPPLICATION_REMOVED = "identitystore.info.clientapplication.removed";
121 
122     // Session variable to store working values
123     private ClientApplication _clientApplication;
124     private String _name;
125     private String _appCode;
126     private String _clientCode;
127 
128     /**
129      * Build the Manage View
130      *
131      * @param request
132      *            The HTTP request
133      * @return The page
134      */
135     @View( value = VIEW_MANAGE_CLIENTAPPLICATIONS, defaultView = true )
136     public String getManageClientApplications( HttpServletRequest request )
137     {
138         _clientApplication = null;
139 
140         initSearchParameters(request);
141 
142         final List<ClientApplication> listClientApplications = ClientApplicationHome.selectApplicationList( ).stream( )
143                 .filter( ca -> StringUtils.isBlank( _name ) || ( ca.getName( ) != null && ca.getName( ).toLowerCase( ).contains( _name.toLowerCase( ) ) ) )
144                 .filter( ca -> StringUtils.isBlank( _appCode )
145                         || ( ca.getApplicationCode( ) != null && ca.getApplicationCode( ).toLowerCase( ).contains( _appCode.toLowerCase( ) ) ) )
146                 .filter( ca -> StringUtils.isBlank( _clientCode )
147                         || ( ca.getClientCode( ) != null && ca.getClientCode( ).toLowerCase( ).contains( _clientCode.toLowerCase( ) ) ) )
148                 .sorted( ( a, b ) -> {
149                     final int compare = StringUtils.compare( a.getApplicationCode( ), b.getApplicationCode( ), false );
150                     if ( compare == 0 )
151                     {
152                         return StringUtils.compare( a.getClientCode( ), b.getClientCode( ), false );
153                     }
154                     return compare;
155                 } ).collect( Collectors.toList( ) );
156 
157         Map<String, Object> model = getPaginatedListModel( request, MARK_CLIENTAPPLICATION_LIST, listClientApplications, JSP_MANAGE_CLIENTAPPLICATIONS );
158         model.put( QUERY_PARAM_CLIENT_APPLICATION_NAME, _name );
159         model.put( QUERY_PARAM_CLIENT_CODE, _clientCode );
160         model.put( QUERY_PARAM_APPLICATION_CODE, _appCode );
161 
162         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CLIENTAPPLICATIONS, TEMPLATE_MANAGE_CLIENTAPPLICATION, model );
163     }
164 
165     /**
166      * Build the Manage View
167      *
168      * @param request
169      *            The HTTP request
170      * @return The page
171      */
172     @View( value = VIEW_DISPLAY_CLIENTAPPLICATIONS )
173     public String getDisplayClientApplications( HttpServletRequest request )
174     {
175         _clientApplication = null;
176 
177         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CLIENTAPPLICATION ) );
178 
179         Map<String, Object> model = getModel( );
180 
181         _clientApplication = ClientApplicationHome.findByPrimaryKey( nId );
182         List<ServiceContract> serviceContracts = ClientApplicationHome.selectServiceContracts( _clientApplication.getId( ) );
183         _clientApplication.setServiceContracts( serviceContracts );
184 
185         model.put( MARK_CLIENTAPPLICATION, _clientApplication );
186         model.put( QUERY_PARAM_CLIENT_APPLICATION_NAME, _name );
187         model.put( QUERY_PARAM_CLIENT_CODE, _clientCode );
188         model.put( QUERY_PARAM_APPLICATION_CODE, _appCode );
189 
190         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CLIENTAPPLICATIONS, TEMPLATE_DISPLAY_CLIENTAPPLICATION, model );
191     }
192 
193     /**
194      * Returns the form to create a clientapplication
195      *
196      * @param request
197      *            The Http request
198      * @return the html code of the clientapplication form
199      */
200     @View( VIEW_CREATE_CLIENTAPPLICATION )
201     public String getCreateClientApplication( HttpServletRequest request )
202     {
203         _clientApplication = new ClientApplication( );
204 
205         Map<String, Object> model = getModel( );
206         model.put( MARK_CLIENTAPPLICATION, _clientApplication );
207         model.put( QUERY_PARAM_CLIENT_APPLICATION_NAME, _name );
208         model.put( QUERY_PARAM_CLIENT_CODE, _clientCode );
209         model.put( QUERY_PARAM_APPLICATION_CODE, _appCode );
210 
211         return getPage( PROPERTY_PAGE_TITLE_CREATE_CLIENTAPPLICATION, TEMPLATE_CREATE_CLIENTAPPLICATION, model );
212     }
213 
214     /**
215      * Process the data capture form of a new clientapplication
216      *
217      * @param request
218      *            The Http Request
219      * @return The Jsp URL of the process result
220      */
221     @Action( ACTION_CREATE_CLIENTAPPLICATION )
222     public String doCreateClientApplication( HttpServletRequest request )
223     {
224         populate( _clientApplication, request );
225         _clientApplication.setAuthorName(this.getUser().getEmail());
226 
227         // Check constraints
228         if ( !validateBean( _clientApplication, VALIDATION_ATTRIBUTES_PREFIX ) )
229         {
230             return redirectView( request, VIEW_CREATE_CLIENTAPPLICATION );
231         }
232 
233         ClientApplicationHome.create( _clientApplication );
234 
235         addInfo( INFO_CLIENTAPPLICATION_CREATED, getLocale( ) );
236 
237         return redirectView( request, VIEW_MANAGE_CLIENTAPPLICATIONS );
238     }
239 
240     /**
241      * Manages the removal form of a clientapplication whose identifier is in the http request
242      *
243      * @param request
244      *            The Http request
245      * @return the html code to confirm
246      */
247     @Action( ACTION_CONFIRM_REMOVE_CLIENTAPPLICATION )
248     public String getConfirmRemoveClientApplication( HttpServletRequest request )
249     {
250         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CLIENTAPPLICATION ) );
251         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CLIENTAPPLICATION ) );
252         url.addParameter( PARAMETER_ID_CLIENTAPPLICATION, nId );
253 
254         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CLIENTAPPLICATION, url.getUrl( ),
255                 AdminMessage.TYPE_CONFIRMATION );
256 
257         return redirect( request, strMessageUrl );
258     }
259 
260     /**
261      * Handles the removal form of a clientapplication
262      *
263      * @param request
264      *            The Http request
265      * @return the jsp URL to display the form to manage clientapplications
266      */
267     @Action( ACTION_REMOVE_CLIENTAPPLICATION )
268     public String doRemoveClientApplication( HttpServletRequest request )
269     {
270         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CLIENTAPPLICATION ) );
271 
272         ClientApplication clientApplication = ClientApplicationHome.findByPrimaryKey( nId );
273         try
274         {
275             ServiceContractService.instance( ).deleteApplication( clientApplication );
276         }
277         catch( IdentityStoreException e )
278         {
279             addError( e.getMessage( ) );
280             return redirectView( request, VIEW_MANAGE_CLIENTAPPLICATIONS );
281         }
282         addInfo( INFO_CLIENTAPPLICATION_REMOVED, getLocale( ) );
283 
284         return redirectView( request, VIEW_MANAGE_CLIENTAPPLICATIONS );
285     }
286 
287     /**
288      * Returns the form to update info about a clientapplication
289      *
290      * @param request
291      *            The Http request
292      * @return The HTML form to update info
293      */
294     @View( VIEW_MODIFY_CLIENTAPPLICATION )
295     public String getModifyClientApplication( HttpServletRequest request )
296     {
297         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CLIENTAPPLICATION ) );
298 
299         if ( ( _clientApplication == null ) || ( _clientApplication.getId( ) != nId ) )
300         {
301             _clientApplication = ClientApplicationHome.findByPrimaryKey( nId );
302         }
303 
304         Map<String, Object> model = getModel( );
305         model.put( MARK_CLIENTAPPLICATION, _clientApplication );
306         model.put( QUERY_PARAM_CLIENT_APPLICATION_NAME, _name );
307         model.put( QUERY_PARAM_CLIENT_CODE, _clientCode );
308         model.put( QUERY_PARAM_APPLICATION_CODE, _appCode );
309 
310         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CLIENTAPPLICATION, TEMPLATE_MODIFY_CLIENTAPPLICATION, model );
311     }
312 
313     /**
314      * Process the change form of a clientapplication
315      *
316      * @param request
317      *            The Http request
318      * @return The Jsp URL of the process result
319      */
320     @Action( ACTION_MODIFY_CLIENTAPPLICATION )
321     public String doModifyClientApplication( HttpServletRequest request )
322     {
323         populate( _clientApplication, request );
324         _clientApplication.setAuthorName(this.getUser().getEmail());
325 
326         // Check constraints
327         if ( !validateBean( _clientApplication, VALIDATION_ATTRIBUTES_PREFIX ) )
328         {
329             return redirect( request, VIEW_MODIFY_CLIENTAPPLICATION, PARAMETER_ID_CLIENTAPPLICATION, _clientApplication.getId( ) );
330         }
331 
332         ClientApplicationHome.update( _clientApplication );
333         addInfo( INFO_CLIENTAPPLICATION_UPDATED, getLocale( ) );
334 
335         return redirectView( request, VIEW_MANAGE_CLIENTAPPLICATIONS );
336     }
337 
338     private void initSearchParameters( HttpServletRequest request )
339     {
340         //si il n'y a pas de paramètres alors c'est qu'on vient de revenir sur la page à partir d'un autre onglet
341         //on réinitialise alors la recherche
342         if(request.getParameterMap().isEmpty())
343         {
344             _name = "";
345             _appCode = "";
346             _clientCode = "";
347         }
348         else
349         {
350             if (request.getParameter(QUERY_PARAM_CLIENT_APPLICATION_NAME) != null)
351             {
352                 _name = request.getParameter(QUERY_PARAM_CLIENT_APPLICATION_NAME);
353             }
354             if (request.getParameter(QUERY_PARAM_APPLICATION_CODE) != null)
355             {
356                 _appCode = request.getParameter(QUERY_PARAM_APPLICATION_CODE);
357             }
358             if (request.getParameter(QUERY_PARAM_CLIENT_CODE) != null)
359             {
360                 _clientCode = request.getParameter(QUERY_PARAM_CLIENT_CODE);
361             }
362         }
363     }
364 
365 }