1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
62
63 public class ExternalFeaturesJspBean extends AdminFeaturesPageJspBean
64 {
65 private static final long serialVersionUID = -5179996626579180810L;
66
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
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
75 private static final String MESSAGE_CONFIRM_DELETE = "portal.features.delete_external_feature.confirmDeleteExternalFeature";
76
77
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
83 private static final String JSP_DELETE_EXTERNAL_FEATURE = "jsp/admin/features/DoRemoveExternalFeature.jsp";
84
85
86 public static final String RIGHT_EXTERNAL_FEATURES_MANAGEMENT = "CORE_FEATURES_MANAGEMENT";
87
88
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
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 }