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.FeatureGroup;
37 import fr.paris.lutece.portal.business.right.FeatureGroupHome;
38 import fr.paris.lutece.portal.business.right.Right;
39 import fr.paris.lutece.portal.business.user.AdminUser;
40 import fr.paris.lutece.portal.business.user.AdminUserHome;
41 import fr.paris.lutece.portal.business.user.authentication.LuteceDefaultAdminUser;
42 import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
43 import fr.paris.lutece.portal.service.admin.AdminUserService;
44 import fr.paris.lutece.portal.service.dashboard.DashboardService;
45 import fr.paris.lutece.portal.service.dashboard.IDashboardComponent;
46 import fr.paris.lutece.portal.service.datastore.DatastoreService;
47 import fr.paris.lutece.portal.service.i18n.I18nService;
48 import fr.paris.lutece.portal.service.init.AppInfo;
49 import fr.paris.lutece.portal.service.message.AdminMessage;
50 import fr.paris.lutece.portal.service.message.AdminMessageService;
51 import fr.paris.lutece.portal.service.plugin.Plugin;
52 import fr.paris.lutece.portal.service.plugin.PluginService;
53 import fr.paris.lutece.portal.service.portal.PortalService;
54 import fr.paris.lutece.portal.service.spring.SpringContextService;
55 import fr.paris.lutece.portal.service.template.AppTemplateService;
56 import fr.paris.lutece.portal.service.util.AppPathService;
57 import fr.paris.lutece.portal.service.util.AppPropertiesService;
58 import fr.paris.lutece.portal.web.constants.Markers;
59 import fr.paris.lutece.portal.web.constants.Messages;
60 import fr.paris.lutece.portal.web.constants.Parameters;
61 import fr.paris.lutece.portal.web.l10n.LocaleService;
62 import fr.paris.lutece.util.html.HtmlTemplate;
63 import fr.paris.lutece.util.password.IPassword;
64 import fr.paris.lutece.util.password.IPasswordFactory;
65
66 import org.apache.commons.lang.StringUtils;
67
68 import java.io.Serializable;
69
70 import java.util.ArrayList;
71 import java.util.Collections;
72 import java.util.HashMap;
73 import java.util.List;
74 import java.util.Locale;
75 import java.util.Map;
76
77 import javax.servlet.http.HttpServletRequest;
78
79
80
81
82
83 public class AdminMenuJspBean implements Serializable
84 {
85
86
87 public static final String PROPERTY_LOGOUT_URL = "lutece.admin.logout.url";
88 public static final String PROPERTY_MENU_DEFAULT_POS = "top";
89 public static final String PROPERTY_MENU_DATASTORE_POS = "portal.site.site_property.menu.position";
90 private static final long serialVersionUID = -8939026727319948581L;
91
92
93 private static final String MARK_FEATURE_GROUP_LIST = "feature_group_list";
94 private static final String MARK_LANGUAGES_LIST = "languages_list";
95 private static final String MARK_CURRENT_LANGUAGE = "current_language";
96 private static final String MARK_USER = "user";
97 private static final String MARK_ADMIN_URL = "admin_url";
98 private static final String MARK_ADMIN_LOGOUT_URL = "admin_logout_url";
99 private static final String MARK_ADMIN_SUMMARY_DOCUMENTATION_URL = "admin_summary_documentation_url";
100 private static final String MARK_SITE_NAME = "site_name";
101 private static final String MARK_MENU_POS = "menu_pos";
102 private static final String MARK_MODIFY_PASSWORD_URL = "url_modify_password";
103 private static final String MARK_DASHBOARD_ZONE = "dashboard_zone_";
104 private static final String MARK_URL_CSS = "css_url";
105 private static final String MARK_JAVASCRIPT_FILE = "javascript_file";
106 private static final String MARK_JAVASCRIPT_FILES = "javascript_files";
107 private static final String MARK_PLUGIN_NAME = "plugin_name";
108 private static final String MARK_ADMIN_AVATAR = "adminAvatar";
109
110
111 private static final String TEMPLATE_ADMIN_HOME = "admin/user/admin_home.html";
112 private static final String TEMPLATE_ADMIN_MENU_HEADER = "admin/user/admin_header.html";
113 private static final String TEMPLATE_ADMIN_MENU_FOOTER = "admin/user/admin_footer.html";
114 private static final String TEMPLATE_MODIFY_PASSWORD_DEFAULT_MODULE = "admin/user/modify_password_default_module.html";
115 private static final String TEMPLATE_STYLESHEET_LINK = "admin/stylesheet_link.html";
116 private static final String TEMPLATE_JAVASCRIPT_FILE = "admin/javascript_file.html";
117
118
119 private static final String PARAMETER_LANGUAGE = "language";
120
121
122 private static final String PROPERTY_DEFAULT_FEATURE_ICON = "lutece.admin.feature.default.icon";
123 private static final String PROPERTY_DOCUMENTATION_SUMMARY_URL = "lutece.documentation.summary.url";
124 private static final String PROPERTY_DASHBOARD_ZONES = "lutece.dashboard.zones.count";
125 private static final int PROPERTY_DASHBOARD_ZONES_DEFAULT = 4;
126 private static final String REFERER = "referer";
127
128
129 private static final String PROPERTY_JSP_URL_ADMIN_LOGOUT = "lutece.admin.logout.url";
130 private static final String MESSAGE_CONTROL_PASSWORD_NO_CORRESPONDING = "portal.users.message.password.confirm.error";
131 private static final String PASSWORD_ERROR = "portal.users.message.password.wrong.current";
132 private static final String PASSWORD_CURRENT_ERROR = "portal.users.message.password.new.equals.current";
133 private static final String MESSAGE_PASSWORD_REDIRECT = "portal.users.message.password.ok.redirect";
134
135 private static String _strStylesheets;
136 private static String _strJavascripts;
137 private boolean _bAdminAvatar = PluginService.isPluginEnable( "adminavatar" );
138
139
140
141
142
143
144
145 public String getAdminMenuHeader( HttpServletRequest request )
146 {
147 Map<String, Object> model = new HashMap<String, Object>( );
148 String strVersion = AppInfo.getVersion( );
149 String strSiteName = PortalService.getSiteName( );
150 AdminUser user = AdminUserService.getAdminUser( request );
151 Locale locale = user.getLocale( );
152 List<FeatureGroup> aFeaturesGroupList = getFeatureGroupsList( user );
153
154
155 model.put( Markers.VERSION, strVersion );
156 model.put( MARK_SITE_NAME, strSiteName );
157 model.put( MARK_MENU_POS,
158 DatastoreService.getInstanceDataValue( PROPERTY_MENU_DATASTORE_POS, PROPERTY_MENU_DEFAULT_POS ) );
159 model.put( MARK_FEATURE_GROUP_LIST, aFeaturesGroupList );
160 model.put( MARK_ADMIN_URL, AppPathService.getBaseUrl( request ) + AppPathService.getAdminMenuUrl( ) );
161 model.put( MARK_USER, user );
162 model.put( MARK_LANGUAGES_LIST, I18nService.getAdminLocales( locale ) );
163 model.put( MARK_CURRENT_LANGUAGE, locale.getLanguage( ) );
164
165 String strLogoutUrl = AppPropertiesService.getProperty( PROPERTY_LOGOUT_URL );
166 model.put( MARK_ADMIN_LOGOUT_URL, ( strLogoutUrl == null ) ? "" : strLogoutUrl );
167
168 String strDocumentationUrl = AppPropertiesService.getProperty( PROPERTY_DOCUMENTATION_SUMMARY_URL );
169 model.put( MARK_ADMIN_SUMMARY_DOCUMENTATION_URL, ( strDocumentationUrl == null ) ? null : strDocumentationUrl );
170
171 int nZoneMax = AppPropertiesService.getPropertyInt( PROPERTY_DASHBOARD_ZONES, PROPERTY_DASHBOARD_ZONES_DEFAULT );
172 setDashboardData( model, user, request, nZoneMax );
173
174 model.put( MARK_ADMIN_AVATAR , _bAdminAvatar );
175
176 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADMIN_MENU_HEADER, user.getLocale( ), model );
177
178 return template.getHtml( );
179 }
180
181
182
183
184
185
186
187 public String getAdminMenuFooter( HttpServletRequest request )
188 {
189 Map<String, Object> model = new HashMap<String, Object>( );
190 String strFooterVersion = AppInfo.getVersion( );
191 String strFooterSiteName = PortalService.getSiteName( );
192 AdminUser user = AdminUserService.getAdminUser( request );
193 Locale locale = ( user != null ) ? user.getLocale( ) : LocaleService.getDefault( );
194 model.put( Markers.VERSION, strFooterVersion );
195 model.put( MARK_SITE_NAME, strFooterSiteName );
196 model.put( MARK_JAVASCRIPT_FILES, getAdminJavascripts( ) );
197
198 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADMIN_MENU_FOOTER, locale, model );
199
200 return template.getHtml( );
201 }
202
203
204
205
206
207
208 public String getAdminMenuUser( HttpServletRequest request )
209 {
210 AdminUser user = AdminUserService.getAdminUser( request );
211
212 Locale locale = user.getLocale( );
213
214
215 List<FeatureGroup> listFeatureGroups = getFeatureGroupsList( user );
216
217 Map<String, Object> model = new HashMap<String, Object>( );
218
219 model.put( MARK_FEATURE_GROUP_LIST, listFeatureGroups );
220 model.put( MARK_USER, user );
221 model.put( MARK_LANGUAGES_LIST, I18nService.getAdminLocales( locale ) );
222 model.put( MARK_CURRENT_LANGUAGE, locale.getLanguage( ) );
223 model.put( MARK_MODIFY_PASSWORD_URL, AdminAuthenticationService.getInstance( ).getChangePasswordPageUrl( ) );
224
225 setDashboardData( model, user, request );
226
227 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADMIN_HOME, locale, model );
228
229 return template.getHtml( );
230 }
231
232
233
234
235
236
237
238 private void setDashboardData( Map<String, Object> model, AdminUser user, HttpServletRequest request )
239 {
240 List<IDashboardComponent> listDashboards = DashboardService.getInstance( ).getDashboards( user, request );
241 int nZoneMax = AppPropertiesService.getPropertyInt( PROPERTY_DASHBOARD_ZONES, PROPERTY_DASHBOARD_ZONES_DEFAULT );
242
243 if ( ( listDashboards != null ) && ( listDashboards.size( ) > 0 ) )
244 {
245 int nColumnCount = DashboardService.getInstance( ).getColumnCount( );
246
247
248 for ( int i = 1; i <= nColumnCount; i++ )
249 {
250 model.put( MARK_DASHBOARD_ZONE + i,
251 DashboardService.getInstance( ).getDashboardData( listDashboards, user, i, request ) );
252 }
253
254
255 for ( int i = nColumnCount + 1; i < nZoneMax; i++ )
256 {
257 model.put( MARK_DASHBOARD_ZONE + i,
258 DashboardService.getInstance( ).getDashboardData( user, i, request ) );
259 }
260 }
261 else
262 {
263 for ( int i = 1; i < nZoneMax; i++ )
264 {
265 model.put( MARK_DASHBOARD_ZONE + i,
266 DashboardService.getInstance( ).getDashboardData( user, i, request ) );
267 }
268 }
269 }
270
271
272
273
274
275
276
277
278 private void setDashboardData( Map<String, Object> model, AdminUser user, HttpServletRequest request,
279 int nDashboardZone )
280 {
281 model.put( MARK_DASHBOARD_ZONE + nDashboardZone,
282 DashboardService.getInstance( ).getDashboardData( user, nDashboardZone, request ) );
283 }
284
285
286
287
288
289
290
291 private List<FeatureGroup> getFeatureGroupsList( AdminUser user )
292 {
293
294 ArrayList<FeatureGroup> aOutFeatureGroupList = new ArrayList<FeatureGroup>( );
295
296
297 Map<String, Right> featuresMap = user.getRights( );
298 List<Right> features = new ArrayList<Right>( featuresMap.values( ) );
299
300 List<Right> rightsToDelete = new ArrayList<Right>( );
301
302
303 for ( Right right : features )
304 {
305 if ( right.getUrl( ) == null )
306 {
307 rightsToDelete.add( right );
308 }
309 }
310
311 features.removeAll( rightsToDelete );
312
313 Collections.sort( features );
314
315
316 for ( FeatureGroup featureGroup : FeatureGroupHome.getFeatureGroupsList( ) )
317 {
318 ArrayList<Right> aLeftFeatures = new ArrayList<Right>( );
319
320 for ( Right right : features )
321 {
322 right.setLocale( user.getLocale( ) );
323 right.setIconUrl( getFeatureIcon( right ) );
324
325 String strFeatureGroup = right.getFeatureGroup( );
326
327 if ( featureGroup.getId( ).equalsIgnoreCase( strFeatureGroup ) )
328 {
329 featureGroup.addFeature( right );
330 }
331 else
332 {
333 aLeftFeatures.add( right );
334 }
335 }
336
337 if ( !featureGroup.isEmpty( ) )
338 {
339 featureGroup.setLocale( user.getLocale( ) );
340 aOutFeatureGroupList.add( featureGroup );
341 }
342
343 features = aLeftFeatures;
344 }
345
346
347 if ( aOutFeatureGroupList.size( ) > 0 )
348 {
349 FeatureGroup lastFeatureGroup = aOutFeatureGroupList.get( aOutFeatureGroupList.size( ) - 1 );
350
351 for ( Right right : features )
352 {
353 lastFeatureGroup.addFeature( right );
354
355
356 }
357 }
358
359 return aOutFeatureGroupList;
360 }
361
362
363
364
365
366
367
368 public String doChangeLanguage( HttpServletRequest request )
369 {
370 String strLanguage = request.getParameter( PARAMETER_LANGUAGE );
371 AdminUser user = AdminUserService.getAdminUser( request );
372 Locale locale = new Locale( strLanguage );
373 user.setLocale( locale );
374 AppPathService.getBaseUrl( request );
375
376 return AppPathService.getBaseUrl( request ) + AppPathService.getAdminMenuUrl( );
377 }
378
379
380
381
382
383
384 private String getFeatureIcon( Right right )
385 {
386 String strIconUrl = AppPropertiesService.getProperty( PROPERTY_DEFAULT_FEATURE_ICON );
387
388 if ( ( right.getIconUrl( ) != null ) && ( !right.getIconUrl( ).equals( "" ) ) )
389 {
390 strIconUrl = right.getIconUrl( );
391 }
392 else
393 {
394 String strPluginName = right.getPluginName( );
395 Plugin plugin = PluginService.getPlugin( strPluginName );
396
397 if ( plugin != null )
398 {
399 strIconUrl = plugin.getIconUrl( );
400 }
401 }
402
403 return strIconUrl;
404 }
405
406
407
408
409
410
411
412
413 public String getModifyDefaultAdminUserPassword( HttpServletRequest request )
414 {
415 AdminUser user = AdminUserService.getAdminUser( request );
416 Locale locale = user.getLocale( );
417 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_PASSWORD_DEFAULT_MODULE, locale );
418
419 return template.getHtml( );
420 }
421
422
423
424
425
426
427
428
429 public String doModifyDefaultAdminUserPassword( HttpServletRequest request )
430 {
431 AdminUser user = AdminUserService.getAdminUser( request );
432
433 String strCurrentPassword = request.getParameter( Parameters.PASSWORD_CURRENT );
434 String strNewPassword = request.getParameter( Parameters.NEW_PASSWORD );
435 String strConfirmNewPassword = request.getParameter( Parameters.CONFIRM_NEW_PASSWORD );
436
437 LuteceDefaultAdminUser userStored = AdminUserHome.findLuteceDefaultAdminUserByPrimaryKey( user.getUserId( ) );
438
439 IPassword password = userStored.getPassword( );
440
441
442 if ( StringUtils.isEmpty( strCurrentPassword ) || StringUtils.isEmpty( strNewPassword ) || StringUtils.isEmpty( strConfirmNewPassword ) )
443 {
444 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
445 }
446
447
448 if ( !strNewPassword.equals( strConfirmNewPassword ) )
449 {
450 return AdminMessageService.getMessageUrl( request, MESSAGE_CONTROL_PASSWORD_NO_CORRESPONDING,
451 AdminMessage.TYPE_STOP );
452 }
453
454 String strUrl = AdminUserService.checkPassword( request, strNewPassword, user.getUserId( ) );
455
456 if ( StringUtils.isNotEmpty( strUrl ) )
457 {
458 return strUrl;
459 }
460
461
462 if ( !password.check( strCurrentPassword ) )
463 {
464 return AdminMessageService.getMessageUrl( request, PASSWORD_ERROR, AdminMessage.TYPE_STOP );
465 }
466
467
468 if ( strCurrentPassword.equals( strNewPassword ) )
469 {
470 return AdminMessageService.getMessageUrl( request, PASSWORD_CURRENT_ERROR, AdminMessage.TYPE_STOP );
471 }
472
473
474 IPasswordFactory passwordFactory = SpringContextService.getBean( IPasswordFactory.BEAN_NAME );
475 userStored.setPassword( passwordFactory.getPasswordFromCleartext( strNewPassword ) );
476 userStored.setPasswordReset( Boolean.FALSE );
477 userStored.setPasswordMaxValidDate( AdminUserService.getPasswordMaxValidDate( ) );
478 AdminUserHome.update( userStored );
479 AdminUserHome.insertNewPasswordInHistory( userStored.getPassword( ), userStored.getUserId( ) );
480
481 return AdminMessageService.getMessageUrl( request, MESSAGE_PASSWORD_REDIRECT,
482 AppPropertiesService.getProperty( PROPERTY_JSP_URL_ADMIN_LOGOUT ), AdminMessage.TYPE_INFO );
483 }
484
485
486
487
488
489
490 public String doModifyAccessibilityMode( HttpServletRequest request )
491 {
492 AdminUser user = AdminUserService.getAdminUser( request );
493
494 if ( user != null )
495 {
496 boolean bIsAccessible = !user.getAccessibilityMode( );
497 user.setAccessibilityMode( bIsAccessible );
498 AdminUserHome.update( user );
499 }
500
501 String strReferer = request.getHeader( REFERER );
502
503 if ( StringUtils.isNotBlank( strReferer ) )
504 {
505 return strReferer;
506 }
507
508 return AppPathService.getBaseUrl( request ) + AppPathService.getAdminMenuUrl( );
509 }
510
511
512
513
514
515
516 public String getAdminStyleSheets( )
517 {
518 if ( _strStylesheets == null )
519 {
520 StringBuilder sbCssLinks = new StringBuilder( );
521 List<Plugin> listPlugins = new ArrayList<Plugin>( );
522 listPlugins.add( PluginService.getCore( ) );
523 listPlugins.addAll( PluginService.getPluginList( ) );
524
525 for ( Plugin plugin : listPlugins )
526 {
527 if ( plugin.getAdminCssStyleSheets( ) != null )
528 {
529 for ( String strStyleSheet : plugin.getAdminCssStyleSheets( ) )
530 {
531 Map<String, Object> model = new HashMap<String, Object>( );
532 model.put( MARK_URL_CSS, strStyleSheet );
533 model.put( MARK_PLUGIN_NAME, plugin.getName( ) );
534 sbCssLinks.append( AppTemplateService.getTemplate( TEMPLATE_STYLESHEET_LINK,
535 LocaleService.getDefault( ), model ).getHtml( ) );
536 }
537 }
538 }
539
540 _strStylesheets = sbCssLinks.toString( );
541 }
542
543 return _strStylesheets;
544 }
545
546
547
548
549
550
551 private String getAdminJavascripts( )
552 {
553 if ( _strJavascripts == null )
554 {
555 StringBuilder sbJavascripts = new StringBuilder( );
556 List<Plugin> listPlugins = new ArrayList<Plugin>( );
557 listPlugins.add( PluginService.getCore( ) );
558 listPlugins.addAll( PluginService.getPluginList( ) );
559
560 for ( Plugin plugin : listPlugins )
561 {
562 if ( plugin.getAdminJavascriptFiles( ) != null )
563 {
564 for ( String strJavascript : plugin.getAdminJavascriptFiles( ) )
565 {
566 Map<String, Object> model = new HashMap<String, Object>( );
567 model.put( MARK_JAVASCRIPT_FILE, strJavascript );
568 model.put( MARK_PLUGIN_NAME, plugin.getName( ) );
569 sbJavascripts.append( AppTemplateService.getTemplate( TEMPLATE_JAVASCRIPT_FILE,
570 LocaleService.getDefault( ), model ).getHtml( ) );
571 }
572 }
573 }
574
575 _strJavascripts = sbJavascripts.toString( );
576 }
577
578 return _strJavascripts;
579 }
580 }