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.features;
35  
36  import java.util.Collection;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import fr.paris.lutece.portal.business.right.FeatureGroup;
43  import fr.paris.lutece.portal.business.right.FeatureGroupHome;
44  import fr.paris.lutece.portal.business.right.Level;
45  import fr.paris.lutece.portal.business.right.LevelHome;
46  import fr.paris.lutece.portal.business.right.Right;
47  import fr.paris.lutece.portal.business.right.RightHome;
48  import fr.paris.lutece.portal.business.user.AdminUser;
49  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
50  import fr.paris.lutece.portal.service.admin.AdminUserService;
51  import fr.paris.lutece.portal.service.message.AdminMessage;
52  import fr.paris.lutece.portal.service.message.AdminMessageService;
53  import fr.paris.lutece.portal.service.security.SecurityTokenService;
54  import fr.paris.lutece.portal.service.template.AppTemplateService;
55  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
56  import fr.paris.lutece.util.ReferenceList;
57  import fr.paris.lutece.util.html.HtmlTemplate;
58  
59  /**
60   *
61   * @author closea
62   */
63  public class ExternalFeaturesJspBean extends AdminFeaturesPageJspBean
64  {
65      private static final long serialVersionUID = -5179996626579180810L;
66      // Properties
67      private static final String PROPERTY_CREATE_EXTERNAL_FEATURE_PAGETITLE = "portal.features.create_external_feature.pageTitle";
68      private static final String PROPERTY_MODIFY_EXTERNAL_FEATURE_PAGETITLE = "portal.features.modify_external_feature.pageTitle";
69  
70      // Templates
71      private static final String TEMPLATE_CREATE_EXTERNAL_FEATURE = "admin/features/create_external_feature.html";
72      private static final String TEMPLATE_MODIFY_EXTERNAL_FEATURE = "admin/features/modify_external_feature.html";
73  
74      // Messages
75      private static final String MESSAGE_CONFIRM_DELETE = "portal.features.delete_external_feature.confirmDeleteExternalFeature";
76  
77      // Parameters
78      private static final String PARAMETER_ID_FEATURE_GROUP = "feature_group_id";
79      private static final String PARAMETER_ID_EXTERNAL_FEATURE = "external_feature_id";
80      private static final String PARAMETER_ID_LEVEL = "level_id";
81  
82      // JSP
83      private static final String JSP_DELETE_EXTERNAL_FEATURE = "jsp/admin/features/DoRemoveExternalFeature.jsp";
84  
85      // Rights
86      public static final String RIGHT_EXTERNAL_FEATURES_MANAGEMENT = "CORE_FEATURES_MANAGEMENT";
87  
88      // Markers
89      private static final String MARK_EXTERNAL_FEATURE = "external_feature";
90      private static final String MARK_FEATURES_GROUPS_REFERENCE_LIST = "features_groups_labels_list";
91      private static final String MARK_RIGHT_LEVELS_REFERENCE_LIST = "right_levels_labels_list";
92  
93      private static final String ANCHOR_ADMIN_DASHBOARDS = "external_features";
94  
95      private Right _externalFeature;
96  
97      public String getCreateExternalFeature( HttpServletRequest request )
98      {
99          setPageTitleProperty( PROPERTY_CREATE_EXTERNAL_FEATURE_PAGETITLE );
100 
101         Map<String, Object> model = new HashMap<>( );
102 
103         Collection<FeatureGroup> featureGroups = FeatureGroupHome.getFeatureGroupsList( );
104         ReferenceListList">ReferenceList featureGroupsReferenceList = new ReferenceList( );
105         for ( FeatureGroup featureGroup : featureGroups )
106         {
107             featureGroup.setLocale( getUser( ).getLocale( ) );
108             featureGroupsReferenceList.add( featureGroup.getReferenceItem( ) );
109         }
110         model.put( MARK_FEATURES_GROUPS_REFERENCE_LIST, featureGroupsReferenceList );
111 
112         Collection<Level> rightLevels = LevelHome.getLevelsList( );
113         ReferenceListceList">ReferenceList rightLevelsReferenceList = new ReferenceList( );
114         for ( Level rightLevel : rightLevels )
115         {
116             rightLevelsReferenceList.add( rightLevel.getReferenceItem( ) );
117         }
118         model.put( MARK_RIGHT_LEVELS_REFERENCE_LIST, rightLevelsReferenceList );
119         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_EXTERNAL_FEATURE ) );
120 
121         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_EXTERNAL_FEATURE, getLocale( ), model );
122 
123         return getAdminPage( template.getHtml( ) );
124     }
125 
126     public String doCreateExternalFeature( HttpServletRequest request ) throws AccessDeniedException
127     {
128         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_EXTERNAL_FEATURE ) )
129         {
130             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
131         }
132         _externalFeature = new Right( );
133         populate( _externalFeature, request );
134         _externalFeature.setFeatureGroup( FeatureGroupHome.findByPrimaryKey( request.getParameter( PARAMETER_ID_FEATURE_GROUP ) ).getId( ) );
135         _externalFeature.setExternalFeature( true );
136         _externalFeature.setLevel( Integer.parseInt( request.getParameter( PARAMETER_ID_LEVEL ) ) );
137 
138         RightHome.create( _externalFeature );
139         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
140     }
141 
142     public String getRemoveExternalFeature( HttpServletRequest request )
143     {
144 
145         String strExternalFeatureId = request.getParameter( PARAMETER_ID_EXTERNAL_FEATURE );
146 
147         _externalFeature = RightHome.findByPrimaryKey( strExternalFeatureId );
148         _externalFeature.setLocale( getUser( ).getLocale( ) );
149 
150         Object [ ] messageArgs = {
151                 _externalFeature.getName( )
152         };
153 
154         Map<String, Object> parameters = new HashMap<>( );
155         parameters.put( PARAMETER_ID_EXTERNAL_FEATURE, strExternalFeatureId );
156         parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DELETE_EXTERNAL_FEATURE ) );
157 
158         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE, messageArgs, null, JSP_DELETE_EXTERNAL_FEATURE, "",
159                 AdminMessage.TYPE_CONFIRMATION, parameters );
160     }
161 
162     public String doRemoveExternalFeature( HttpServletRequest request ) throws AccessDeniedException
163     {
164         if ( !SecurityTokenService.getInstance( ).validate( request, JSP_DELETE_EXTERNAL_FEATURE ) )
165         {
166             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
167         }
168         RightHome.remove( _externalFeature.getId( ) );
169 
170         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
171     }
172 
173     public String getModifyExternalFeature( HttpServletRequest request )
174     {
175 
176         setPageTitleProperty( PROPERTY_MODIFY_EXTERNAL_FEATURE_PAGETITLE );
177         Map<String, Object> model = new HashMap<>( );
178 
179         String strExternalFeatureId = request.getParameter( PARAMETER_ID_EXTERNAL_FEATURE );
180         _externalFeature = RightHome.findByPrimaryKey( strExternalFeatureId );
181         model.put( MARK_EXTERNAL_FEATURE, _externalFeature );
182 
183         Collection<FeatureGroup> featureGroups = FeatureGroupHome.getFeatureGroupsList( );
184         ReferenceListList">ReferenceList featureGroupsReferenceList = new ReferenceList( );
185         for ( FeatureGroup featureGroup : featureGroups )
186         {
187             featureGroup.setLocale( getUser( ).getLocale( ) );
188             featureGroupsReferenceList.add( featureGroup.getReferenceItem( ) );
189         }
190         model.put( MARK_FEATURES_GROUPS_REFERENCE_LIST, featureGroupsReferenceList );
191 
192         Collection<Level> rightLevels = LevelHome.getLevelsList( );
193         ReferenceListceList">ReferenceList rightLevelsReferenceList = new ReferenceList( );
194         for ( Level rightLevel : rightLevels )
195         {
196             rightLevelsReferenceList.add( rightLevel.getReferenceItem( ) );
197         }
198         model.put( MARK_RIGHT_LEVELS_REFERENCE_LIST, rightLevelsReferenceList );
199         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_EXTERNAL_FEATURE ) );
200 
201         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_EXTERNAL_FEATURE, getLocale( ), model );
202 
203         return getAdminPage( template.getHtml( ) );
204     }
205 
206     public String doModifyExternalFeature( HttpServletRequest request ) throws AccessDeniedException
207     {
208         if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_EXTERNAL_FEATURE ) )
209         {
210             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
211         }
212         int nIdOrder = _externalFeature.getOrder( );
213         String strIdExternalFeature = _externalFeature.getId( );
214 
215         _externalFeature = new Right( );
216         populate( _externalFeature, request );
217 
218         _externalFeature.setId( strIdExternalFeature );
219         _externalFeature.setFeatureGroup( FeatureGroupHome.findByPrimaryKey( request.getParameter( PARAMETER_ID_FEATURE_GROUP ) ).getId( ) );
220         _externalFeature.setExternalFeature( true );
221         _externalFeature.setOrder( nIdOrder );
222         _externalFeature.setLevel( Integer.parseInt( request.getParameter( PARAMETER_ID_LEVEL ) ) );
223 
224         RightHome.update( _externalFeature );
225 
226         // update this right for user if he or she already have it
227         AdminUser user = AdminUserService.getAdminUser( request );
228         if ( user.checkRight( _externalFeature.getId( ) ) )
229         {
230             user.updateRight( _externalFeature );
231         }
232 
233         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
234     }
235 }