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.attribute.AttributeKey;
37  import fr.paris.lutece.plugins.identitystore.business.attribute.AttributeKeyHome;
38  import fr.paris.lutece.plugins.identitystore.business.attribute.AttributeValue;
39  import fr.paris.lutece.plugins.identitystore.business.attribute.KeyType;
40  import fr.paris.lutece.plugins.identitystore.service.attribute.IdentityAttributeService;
41  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
42  import fr.paris.lutece.portal.service.admin.AdminUserService;
43  import fr.paris.lutece.portal.service.message.AdminMessage;
44  import fr.paris.lutece.portal.service.message.AdminMessageService;
45  import fr.paris.lutece.portal.service.util.AppPathService;
46  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
47  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
48  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
49  import fr.paris.lutece.util.url.UrlItem;
50  
51  import javax.servlet.http.HttpServletRequest;
52  import java.util.ArrayList;
53  import java.util.Comparator;
54  import java.util.List;
55  import java.util.Map;
56  
57  /**
58   * This class provides the user interface to manage AttributeKey features ( manage, create, modify, remove )
59   */
60  @Controller( controllerJsp = "ManageAttributeKeys.jsp", controllerPath = "jsp/admin/plugins/identitystore/", right = "IDENTITYSTORE_ADMIN_MANAGEMENT" )
61  public class AttributeKeyJspBean extends AdminIdentitiesJspBean
62  {
63      // Templates
64      private static final String TEMPLATE_MANAGE_ATTRIBUTEKEYS = "/admin/plugins/identitystore/manage_attributekeys.html";
65      private static final String TEMPLATE_CREATE_ATTRIBUTEKEY = "/admin/plugins/identitystore/create_attributekey.html";
66      private static final String TEMPLATE_MODIFY_ATTRIBUTEKEY = "/admin/plugins/identitystore/create_attributekey.html";
67      private static final String TEMPLATE_APP_RIGHT_ATTRIBUTES = "/admin/plugins/identitystore/view_apprightattributes.html";
68  
69      // Parameters
70      private static final String PARAMETER_ID_ATTRIBUTEKEY = "id";
71      private static final String PARAMETER_ID_KEY_TYPE = "id_keytype";
72      private static final String PARAMETER_ATTRIBUTE_VALUES_VALUE = "attribute_value_value";
73      private static final String PARAMETER_ATTRIBUTE_VALUES_LABEL = "attribute_value_label";
74  
75      // Properties for page titles
76      private static final String PROPERTY_PAGE_TITLE_MANAGE_ATTRIBUTEKEYS = "identitystore.manage_attributekeys.pageTitle";
77      private static final String PROPERTY_PAGE_TITLE_MODIFY_ATTRIBUTEKEY = "identitystore.modify_attributekey.pageTitle";
78      private static final String PROPERTY_PAGE_TITLE_CREATE_ATTRIBUTEKEY = "identitystore.create_attributekey.pageTitle";
79      private static final String PROPERTY_PAGE_TITLE_APP_RIGHT_ATTRIBUTES = "identitystore.view_appRightAttributes.pageTitle";
80      private static final String PROPERTY_MANAGE_ATTRIBUTEKEYS_DUPLICATE_ERROR_MESSAGE = "identitystore.validation.attributekey.KeyName.duplicate";
81  
82      // Markers
83      private static final String MARK_ATTRIBUTEKEY_LIST = "attributekey_list";
84      private static final String MARK_ATTRIBUTEKEY = "attributekey";
85      private static final String MARK_KEYTYPE_LIST = "keytype_list";
86      private static final String MARK_WEBAPP_URL = "webapp_url";
87      private static final String MARK_LOCALE = "locale";
88      private static final String MARK_ACTION = "action";
89      private static final String JSP_MANAGE_ATTRIBUTEKEYS = "jsp/admin/plugins/identitystore/ManageAttributeKeys.jsp";
90  
91      // Properties
92      private static final String MESSAGE_CONFIRM_REMOVE_ATTRIBUTEKEY = "identitystore.message.confirmRemoveAttributeKey";
93      private static final String MESSAGE_CANNOT_REMOVE_REFERENCE_ATTRIBUTE_EXISTS = "identitystore.message.cannotRemoveReferenceAttributeExists";
94  
95      // Validations
96      private static final String VALIDATION_ATTRIBUTES_PREFIX = "identitystore.model.entity.attributekey.attribute.";
97  
98      // Views
99      private static final String VIEW_MANAGE_ATTRIBUTEKEYS = "manageAttributeKeys";
100     private static final String VIEW_CREATE_ATTRIBUTEKEY = "createAttributeKey";
101     private static final String VIEW_MODIFY_ATTRIBUTEKEY = "modifyAttributeKey";
102     private static final String VIEW_APP_RIGHT_ATTRIBUTES = "appRightAttributes";
103 
104     // Actions
105     private static final String ACTION_CREATE_ATTRIBUTEKEY = "createAttributeKey";
106     private static final String ACTION_MODIFY_ATTRIBUTEKEY = "modifyAttributeKey";
107     private static final String ACTION_REMOVE_ATTRIBUTEKEY = "removeAttributeKey";
108     private static final String ACTION_CONFIRM_REMOVE_ATTRIBUTEKEY = "confirmRemoveAttributeKey";
109 
110     // Infos
111     private static final String INFO_ATTRIBUTEKEY_CREATED = "identitystore.info.attributekey.created";
112     private static final String INFO_ATTRIBUTEKEY_UPDATED = "identitystore.info.attributekey.updated";
113     private static final String INFO_ATTRIBUTEKEY_REMOVED = "identitystore.info.attributekey.removed";
114 
115     // Session variable to store working values
116     private AttributeKey _attributekey;
117 
118     /**
119      * Build the Manage View
120      *
121      * @param request
122      *            The HTTP request
123      * @return The page
124      */
125     @View( value = VIEW_MANAGE_ATTRIBUTEKEYS, defaultView = true )
126     public String getManageAttributeKeys( HttpServletRequest request )
127     {
128         _attributekey = null;
129 
130         final List<AttributeKey> listAttributeKeys = AttributeKeyHome.getAttributeKeysList( true );
131         listAttributeKeys.sort( Comparator.comparing( AttributeKey::getId ) );
132         Map<String, Object> model = getPaginatedListModel( request, MARK_ATTRIBUTEKEY_LIST, listAttributeKeys, JSP_MANAGE_ATTRIBUTEKEYS );
133 
134         return getPage( PROPERTY_PAGE_TITLE_MANAGE_ATTRIBUTEKEYS, TEMPLATE_MANAGE_ATTRIBUTEKEYS, model );
135     }
136 
137     /**
138      * Returns the form to create a attributekey
139      *
140      * @param request
141      *            The Http request
142      * @return the html code of the attributekey form
143      */
144     @View( VIEW_CREATE_ATTRIBUTEKEY )
145     public String getCreateAttributeKey( HttpServletRequest request )
146     {
147         _attributekey = new AttributeKey( );
148         final Map<String, Object> model = getModel( );
149         model.put( MARK_ACTION, "action_createAttributeKey" );
150         model.put( MARK_ATTRIBUTEKEY, _attributekey );
151         model.put( MARK_KEYTYPE_LIST, KeyType.getReferenceList( request.getLocale( ) ) );
152         storeRichText( request, model );
153 
154         return getPage( PROPERTY_PAGE_TITLE_CREATE_ATTRIBUTEKEY, TEMPLATE_CREATE_ATTRIBUTEKEY, model );
155     }
156 
157     /**
158      * Process the data capture form of a new attributekey
159      *
160      * @param request
161      *            The Http Request
162      * @return The Jsp URL of the process result
163      */
164     @Action( ACTION_CREATE_ATTRIBUTEKEY )
165     public String doCreateAttributeKey( HttpServletRequest request )
166     {
167         populate( _attributekey, request );
168         _attributekey.getAttributeValues( ).addAll( this.extractAttributeValues( request ) );
169 
170         // Check constraints
171         if ( !validateBean( _attributekey, VALIDATION_ATTRIBUTES_PREFIX ) )
172         {
173             return redirectView( request, VIEW_CREATE_ATTRIBUTEKEY );
174         }
175 
176         final String strIdKeyType = request.getParameter( PARAMETER_ID_KEY_TYPE );
177         final int nKeyType = Integer.parseInt( strIdKeyType );
178         final KeyType keyType = KeyType.valueOf( nKeyType );
179         _attributekey.setKeyType( keyType );
180 
181         if ( AttributeKeyHome.findByKey( _attributekey.getKeyName( ), false ) != null )
182         {
183             addError( PROPERTY_MANAGE_ATTRIBUTEKEYS_DUPLICATE_ERROR_MESSAGE, getLocale( ) );
184             return redirectView( request, VIEW_CREATE_ATTRIBUTEKEY );
185         }
186 
187         try
188         {
189             IdentityAttributeService.instance( ).createAttributeKey( _attributekey );
190         }
191         catch( IdentityStoreException e )
192         {
193             addError( e.getMessage( ) );
194             return redirectView( request, VIEW_CREATE_ATTRIBUTEKEY );
195         }
196         addInfo( INFO_ATTRIBUTEKEY_CREATED, getLocale( ) );
197         _attributekey = null;
198 
199         return redirectView( request, VIEW_MANAGE_ATTRIBUTEKEYS );
200     }
201 
202     /**
203      * Manages the removal form of a attributekey whose identifier is in the http request
204      *
205      * @param request
206      *            The Http request
207      * @return the html code to confirm
208      */
209     @Action( ACTION_CONFIRM_REMOVE_ATTRIBUTEKEY )
210     public String getConfirmRemoveAttributeKey( HttpServletRequest request )
211     {
212         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ATTRIBUTEKEY ) );
213 
214         if ( AttributeKeyHome.checkAttributeId( nId ) )
215         {
216             return redirect( request, AdminMessageService.getMessageUrl( request, MESSAGE_CANNOT_REMOVE_REFERENCE_ATTRIBUTE_EXISTS, AdminMessage.TYPE_ERROR ) );
217         }
218 
219         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_ATTRIBUTEKEY ) );
220         url.addParameter( PARAMETER_ID_ATTRIBUTEKEY, nId );
221 
222         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ATTRIBUTEKEY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
223 
224         return redirect( request, strMessageUrl );
225     }
226 
227     /**
228      * Handles the removal form of a attributekey
229      *
230      * @param request
231      *            The Http request
232      * @return the jsp URL to display the form to manage attributekeys
233      */
234     @Action( ACTION_REMOVE_ATTRIBUTEKEY )
235     public String doRemoveAttributeKey( HttpServletRequest request )
236     {
237         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ATTRIBUTEKEY ) );
238         final AttributeKey attributeKey = AttributeKeyHome.findByPrimaryKey( nId, false );
239         if ( attributeKey == null )
240         {
241             return redirect( request, AdminMessageService.getMessageUrl( request, MESSAGE_CANNOT_REMOVE_REFERENCE_ATTRIBUTE_EXISTS, AdminMessage.TYPE_ERROR ) );
242         }
243         try
244         {
245             IdentityAttributeService.instance( ).deleteAttributeKey( attributeKey );
246         }
247         catch( IdentityStoreException e )
248         {
249             addError( e.getMessage( ) );
250             return redirectView( request, VIEW_MANAGE_ATTRIBUTEKEYS );
251         }
252         addInfo( INFO_ATTRIBUTEKEY_REMOVED, getLocale( ) );
253 
254         return redirectView( request, VIEW_MANAGE_ATTRIBUTEKEYS );
255     }
256 
257     /**
258      * Returns the form to update info about a attributekey
259      *
260      * @param request
261      *            The Http request
262      * @return The HTML form to update info
263      */
264     @View( VIEW_MODIFY_ATTRIBUTEKEY )
265     public String getModifyAttributeKey( HttpServletRequest request )
266     {
267         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ATTRIBUTEKEY ) );
268 
269         if ( ( _attributekey == null ) || ( _attributekey.getId( ) != nId ) )
270         {
271             _attributekey = AttributeKeyHome.findByPrimaryKey( nId, true );
272         }
273 
274         Map<String, Object> model = getModel( );
275         model.put( MARK_ACTION, "action_modifyAttributeKey" );
276         model.put( MARK_ATTRIBUTEKEY, _attributekey );
277         model.put( MARK_KEYTYPE_LIST, KeyType.getReferenceList( request.getLocale( ) ) );
278         storeRichText( request, model );
279 
280         return getPage( PROPERTY_PAGE_TITLE_MODIFY_ATTRIBUTEKEY, TEMPLATE_MODIFY_ATTRIBUTEKEY, model );
281     }
282 
283     /**
284      * Process the change form of a attributekey
285      *
286      * @param request
287      *            The Http request
288      * @return The Jsp URL of the process result
289      */
290     @Action( ACTION_MODIFY_ATTRIBUTEKEY )
291     public String doModifyAttributeKey( HttpServletRequest request )
292     {
293         populate( _attributekey, request );
294         _attributekey.getAttributeValues( ).clear( );
295         _attributekey.getAttributeValues( ).addAll( this.extractAttributeValues( request ) );
296 
297         // Check constraints
298         if ( !validateBean( _attributekey, VALIDATION_ATTRIBUTES_PREFIX ) )
299         {
300             return redirect( request, VIEW_MODIFY_ATTRIBUTEKEY, PARAMETER_ID_ATTRIBUTEKEY, _attributekey.getId( ) );
301         }
302 
303         try
304         {
305             IdentityAttributeService.instance( ).updateAttributeKey( _attributekey );
306         }
307         catch( IdentityStoreException e )
308         {
309             addError( e.getMessage( ) );
310             return redirectView( request, VIEW_MODIFY_ATTRIBUTEKEY );
311         }
312         addInfo( INFO_ATTRIBUTEKEY_UPDATED, getLocale( ) );
313         _attributekey = null;
314         return redirectView( request, VIEW_MANAGE_ATTRIBUTEKEYS );
315     }
316 
317     /**
318      * fill model for richText
319      * 
320      * @param request
321      *            the request
322      * @param model
323      *            the model
324      */
325     private static void storeRichText( HttpServletRequest request, Map<String, Object> model )
326     {
327         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
328         model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage( ) );
329     }
330 
331     /**
332      * Returns the display of attribute with application rights
333      *
334      * @param request
335      *            The Http request
336      * @return The HTML info
337      */
338     @View( VIEW_APP_RIGHT_ATTRIBUTES )
339     public String getApplicationRight( HttpServletRequest request )
340     {
341         List<AttributeKey> listAttributeKeys = AttributeKeyHome.getAttributeKeysList( false );
342         Map<String, Object> model = getPaginatedListModel( request, MARK_ATTRIBUTEKEY_LIST, listAttributeKeys,
343                 JSP_MANAGE_ATTRIBUTEKEYS + "?view=" + VIEW_APP_RIGHT_ATTRIBUTES );
344 
345         // TODO
346         // Map<String, AttributeApplicationsRight> mapAttributeApplicationsRight = ClientApplicationHome.getAttributeApplicationsRight( );
347         // model.put( MARK_ATTRIBUTE_APPS_RIGHT_MAP, mapAttributeApplicationsRight );
348 
349         return getPage( PROPERTY_PAGE_TITLE_APP_RIGHT_ATTRIBUTES, TEMPLATE_APP_RIGHT_ATTRIBUTES, model );
350     }
351 
352     private List<AttributeValue> extractAttributeValues( final HttpServletRequest request )
353     {
354         final List<AttributeValue> values = new ArrayList<>( );
355         request.getParameterMap( ).entrySet( ).stream( ).filter( stringEntry -> stringEntry.getKey( ).startsWith( PARAMETER_ATTRIBUTE_VALUES_VALUE ) )
356                 .forEach( stringEntry -> {
357                     final String index = stringEntry.getKey( ).replace( PARAMETER_ATTRIBUTE_VALUES_VALUE, "" );
358                     final String value = request.getParameter( PARAMETER_ATTRIBUTE_VALUES_VALUE + index );
359                     final String label = request.getParameter( PARAMETER_ATTRIBUTE_VALUES_LABEL + index );
360                     final AttributeValuebusiness/attribute/AttributeValue.html#AttributeValue">AttributeValue attributeValue = new AttributeValue( );
361                     final String id = request.getParameter( PARAMETER_ID_ATTRIBUTEKEY );
362                     if ( id != null && !id.isEmpty( ) )
363                     {
364                         attributeValue.setAttributeId( Integer.parseInt( id ) );
365                     }
366                     attributeValue.setValue( value );
367                     attributeValue.setLabel( label );
368                     values.add( attributeValue );
369                 } );
370 
371         return values;
372     }
373 }