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.admin;
35
36 import fr.paris.lutece.portal.business.right.Right;
37 import fr.paris.lutece.portal.business.right.RightHome;
38 import fr.paris.lutece.portal.business.user.AdminUser;
39 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
40 import fr.paris.lutece.portal.service.admin.AdminUserService;
41 import fr.paris.lutece.portal.service.admin.PasswordResetException;
42 import fr.paris.lutece.portal.service.i18n.I18nService;
43 import fr.paris.lutece.portal.service.template.AppTemplateService;
44 import fr.paris.lutece.portal.service.util.AppPathService;
45 import fr.paris.lutece.portal.service.util.AppPropertiesService;
46 import fr.paris.lutece.util.bean.BeanUtil;
47 import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
48 import fr.paris.lutece.util.beanvalidation.ValidationError;
49 import fr.paris.lutece.util.beanvalidation.ValidationErrorConfig;
50 import fr.paris.lutece.util.html.HtmlTemplate;
51
52 import java.io.Serializable;
53
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Locale;
57 import java.util.Map;
58 import java.util.Set;
59
60 import javax.servlet.http.HttpServletRequest;
61
62 import javax.validation.ConstraintViolation;
63
64
65
66
67 public abstract class AdminFeaturesPageJspBean implements Serializable
68 {
69 protected static final String JSP_TECHNICAL_ADMINISTRATION = "jsp/admin/AdminTechnicalMenu.jsp";
70 protected static final String ERROR_INVALID_TOKEN = "Invalid security token";
71
72
73
74
75 private static final long serialVersionUID = -7952383741759547934L;
76
77
78 private static final String TEMPLATE_MAIN = "/admin/feature_frameset.html";
79
80
81 private static final String MARK_FEATURE_URL = "feature_url";
82 private static final String MARK_FEATURE_TITLE = "feature_title";
83 private static final String MARK_FEATURE_ICON = "feature_icon";
84 private static final String MARK_FEATURE_DOCUMENTATION = "feature_documentation";
85 private static final String MARK_FEATURE_GROUP = "feature_group";
86 private static final String MARK_PAGE_TITLE = "page_title";
87 private static final String MARK_PAGE_CONTENT = "page_content";
88
89
90 private static final String PROPERTY_DEFAULT_FEATURE_ICON = "lutece.admin.feature.default.icon";
91 private static final String PROPERTY_RESET_EXCEPTION_MESSAGE = "User must reset his password.";
92
93
94 private String _strFeatureLabel;
95 private String _strFeatureUrl;
96 private String _strFeatureIcon;
97 private String _strFeatureDocumentation;
98 private String _strFeatureGroup;
99 private String _strPageTitleKey;
100 private Locale _locale;
101 private AdminUser _user;
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public void init( HttpServletRequest request, String strRight ) throws AccessDeniedException
116 {
117 _user = AdminUserService.getAdminUser( request );
118 Right right = RightHome.findByPrimaryKey( strRight );
119
120 if ( right == null )
121 {
122 throw new AccessDeniedException( strRight + " right does not exist for user "+_user.getAccessCode( )+"." );
123 }
124
125 if ( !_user.checkRight( strRight ) )
126 {
127 throw new AccessDeniedException( "User " + _user.getAccessCode( ) + " does not have " + strRight + " right." );
128 }
129
130 if ( _user.isPasswordReset( ) )
131 {
132 throw new PasswordResetException( PROPERTY_RESET_EXCEPTION_MESSAGE );
133 }
134
135
136 _locale = _user.getLocale( );
137
138 right.setLocale( _locale );
139 _strFeatureLabel = right.getName( );
140 _strFeatureUrl = right.getUrl( );
141 _strFeatureIcon = right.getIconUrl( );
142 _strFeatureDocumentation = right.getDocumentationUrl( );
143 _strFeatureGroup = right.getFeatureGroup( );
144 }
145
146
147
148
149
150
151
152 public void setPageTitleProperty( String strPageTitleKey )
153 {
154 _strPageTitleKey = strPageTitleKey;
155 }
156
157
158
159
160
161
162 public String getPageTitle( )
163 {
164 return ( _strPageTitleKey != null ) ? I18nService.getLocalizedString( _strPageTitleKey, getLocale( ) ) : "";
165 }
166
167
168
169
170
171
172 public Locale getLocale( )
173 {
174 return _locale;
175 }
176
177
178
179
180
181
182 public AdminUser getUser( )
183 {
184 return _user;
185 }
186
187
188
189
190
191
192
193
194 public String getHomeUrl( HttpServletRequest request )
195 {
196 return AppPathService.getBaseUrl( request ) + _strFeatureUrl;
197 }
198
199
200
201
202
203
204 public String getFeatureIcon( )
205 {
206 return _strFeatureIcon;
207 }
208
209
210
211
212
213
214
215 public void setFeatureIcon( String strFeatureIcon )
216 {
217 _strFeatureIcon = strFeatureIcon;
218 }
219
220
221
222
223
224
225
226 public void setFeatureGroup( String strFeatureGroup )
227 {
228 _strFeatureGroup = strFeatureGroup;
229 }
230
231
232
233
234
235
236
237
238 public String getAdminPage( String strContent )
239 {
240 Map<String, String> rootModel = new HashMap<>( );
241
242 rootModel.put( MARK_FEATURE_URL, _strFeatureUrl );
243 rootModel.put( MARK_FEATURE_TITLE, _strFeatureLabel );
244
245 String strIconUrl = ( _strFeatureIcon != null ) ? _strFeatureIcon : AppPropertiesService.getProperty( PROPERTY_DEFAULT_FEATURE_ICON );
246 rootModel.put( MARK_FEATURE_ICON, strIconUrl );
247
248 String strDocumentationUrl = null;
249
250 if ( _strFeatureDocumentation != null )
251 {
252 strDocumentationUrl = _strFeatureDocumentation;
253 }
254
255 rootModel.put( MARK_FEATURE_DOCUMENTATION, strDocumentationUrl );
256 rootModel.put( MARK_FEATURE_GROUP, _strFeatureGroup );
257
258 rootModel.put( MARK_PAGE_TITLE, getPageTitle( ) );
259 rootModel.put( MARK_PAGE_CONTENT, strContent );
260
261 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIN, getLocale( ), rootModel );
262
263 return template.getHtml( );
264 }
265
266
267
268
269
270
271
272
273
274 protected void populate( Object bean, HttpServletRequest request )
275 {
276 populate( bean, request, null );
277 }
278
279
280
281
282
283
284
285
286
287
288
289 protected void populate( Object bean, HttpServletRequest request, Locale locale )
290 {
291 if ( locale == null )
292 {
293 BeanUtil.populate( bean, request, getLocale( ) );
294 }
295 else
296 {
297 BeanUtil.populate( bean, request, locale );
298 }
299 }
300
301
302
303
304
305
306
307
308
309
310 public <T> Set<ConstraintViolation<T>> validate( T bean )
311 {
312 return BeanValidationUtil.validate( bean );
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326 public <T> List<ValidationError> validate( T bean, String strFieldsKeyPrefix )
327 {
328 return BeanValidationUtil.validate( bean, getLocale( ), strFieldsKeyPrefix );
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342 public <T> List<ValidationError> validate( T bean, ValidationErrorConfig config )
343 {
344 return BeanValidationUtil.validate( bean, getLocale( ), config );
345 }
346
347
348
349
350
351
352
353
354
355
356 protected String getAdminDashboardsUrl( HttpServletRequest request, String strAnchor )
357 {
358 return AppPathService.getBaseUrl( request ) + JSP_TECHNICAL_ADMINISTRATION + "?#" + strAnchor;
359 }
360
361
362
363
364
365
366
367 protected boolean isUserHigherThanConnectedUser( AdminUser user )
368 {
369 return user.getUserLevel( ) > getUser( ).getUserLevel( ) || getUser( ).isAdmin( );
370 }
371 }