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
119 if ( !_user.checkRight( strRight ) )
120 {
121 throw new AccessDeniedException( "User " + _user.getAccessCode( ) + " does not have " + strRight + " right." );
122 }
123
124 if ( _user.isPasswordReset( ) )
125 {
126 throw new PasswordResetException( PROPERTY_RESET_EXCEPTION_MESSAGE );
127 }
128
129
130 _locale = _user.getLocale( );
131
132 Right right = RightHome.findByPrimaryKey( strRight );
133 right.setLocale( _locale );
134 _strFeatureLabel = right.getName( );
135 _strFeatureUrl = right.getUrl( );
136 _strFeatureIcon = right.getIconUrl( );
137 _strFeatureDocumentation = right.getDocumentationUrl( );
138 _strFeatureGroup = right.getFeatureGroup( );
139 }
140
141
142
143
144
145
146
147 public void setPageTitleProperty( String strPageTitleKey )
148 {
149 _strPageTitleKey = strPageTitleKey;
150 }
151
152
153
154
155
156
157 public String getPageTitle( )
158 {
159 return ( _strPageTitleKey != null ) ? I18nService.getLocalizedString( _strPageTitleKey, getLocale( ) ) : "";
160 }
161
162
163
164
165
166
167 public Locale getLocale( )
168 {
169 return _locale;
170 }
171
172
173
174
175
176
177 public AdminUser getUser( )
178 {
179 return _user;
180 }
181
182
183
184
185
186
187
188
189 public String getHomeUrl( HttpServletRequest request )
190 {
191 return AppPathService.getBaseUrl( request ) + _strFeatureUrl;
192 }
193
194
195
196
197
198
199 public String getFeatureIcon( )
200 {
201 return _strFeatureIcon;
202 }
203
204
205
206
207
208
209
210 public void setFeatureIcon( String strFeatureIcon )
211 {
212 _strFeatureIcon = strFeatureIcon;
213 }
214
215
216
217
218
219
220
221 public void setFeatureGroup( String strFeatureGroup )
222 {
223 _strFeatureGroup = strFeatureGroup;
224 }
225
226
227
228
229
230
231
232
233 public String getAdminPage( String strContent )
234 {
235 Map<String, String> rootModel = new HashMap<>( );
236
237 rootModel.put( MARK_FEATURE_URL, _strFeatureUrl );
238 rootModel.put( MARK_FEATURE_TITLE, _strFeatureLabel );
239
240 String strIconUrl = ( _strFeatureIcon != null ) ? _strFeatureIcon : AppPropertiesService.getProperty( PROPERTY_DEFAULT_FEATURE_ICON );
241 rootModel.put( MARK_FEATURE_ICON, strIconUrl );
242
243 String strDocumentationUrl = null;
244
245 if ( _strFeatureDocumentation != null )
246 {
247 strDocumentationUrl = _strFeatureDocumentation;
248 }
249
250 rootModel.put( MARK_FEATURE_DOCUMENTATION, strDocumentationUrl );
251 rootModel.put( MARK_FEATURE_GROUP, _strFeatureGroup );
252
253 rootModel.put( MARK_PAGE_TITLE, getPageTitle( ) );
254 rootModel.put( MARK_PAGE_CONTENT, strContent );
255
256 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MAIN, getLocale( ), rootModel );
257
258 return template.getHtml( );
259 }
260
261
262
263
264
265
266
267
268
269 protected void populate( Object bean, HttpServletRequest request )
270 {
271 populate( bean, request, null );
272 }
273
274
275
276
277
278
279
280
281
282
283
284 protected void populate( Object bean, HttpServletRequest request, Locale locale )
285 {
286 if ( locale == null )
287 {
288 BeanUtil.populate( bean, request, getLocale( ) );
289 }
290 else
291 {
292 BeanUtil.populate( bean, request, locale );
293 }
294 }
295
296
297
298
299
300
301
302
303
304
305 public <T> Set<ConstraintViolation<T>> validate( T bean )
306 {
307 return BeanValidationUtil.validate( bean );
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321 public <T> List<ValidationError> validate( T bean, String strFieldsKeyPrefix )
322 {
323 return BeanValidationUtil.validate( bean, getLocale( ), strFieldsKeyPrefix );
324 }
325
326
327
328
329
330
331
332
333
334
335
336
337 public <T> List<ValidationError> validate( T bean, ValidationErrorConfig config )
338 {
339 return BeanValidationUtil.validate( bean, getLocale( ), config );
340 }
341
342
343
344
345
346
347
348
349
350
351 protected String getAdminDashboardsUrl( HttpServletRequest request, String strAnchor )
352 {
353 return AppPathService.getBaseUrl( request ) + JSP_TECHNICAL_ADMINISTRATION + "?#" + strAnchor;
354 }
355
356
357
358
359
360
361
362 protected boolean isUserHigherThanConnectedUser( AdminUser user )
363 {
364 return user.getUserLevel( ) > getUser( ).getUserLevel( ) || getUser( ).isAdmin( );
365 }
366 }