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.mailinglist;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42
43 import javax.servlet.http.HttpServletRequest;
44
45 import org.apache.commons.lang3.StringUtils;
46
47 import fr.paris.lutece.portal.business.mailinglist.MailingList;
48 import fr.paris.lutece.portal.business.mailinglist.MailingListFilter;
49 import fr.paris.lutece.portal.business.mailinglist.MailingListHome;
50 import fr.paris.lutece.portal.business.mailinglist.MailingListUsersFilter;
51 import fr.paris.lutece.portal.business.mailinglist.Recipient;
52 import fr.paris.lutece.portal.business.rbac.RBACRoleHome;
53 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
54 import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
55 import fr.paris.lutece.portal.service.mailinglist.MailingListRemovalListenerService;
56 import fr.paris.lutece.portal.service.message.AdminMessage;
57 import fr.paris.lutece.portal.service.message.AdminMessageService;
58 import fr.paris.lutece.portal.service.security.SecurityTokenService;
59 import fr.paris.lutece.portal.service.template.AppTemplateService;
60 import fr.paris.lutece.portal.service.util.AppLogService;
61 import fr.paris.lutece.portal.service.util.AppPathService;
62 import fr.paris.lutece.portal.service.util.AppPropertiesService;
63 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
64 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
65 import fr.paris.lutece.portal.web.constants.Messages;
66 import fr.paris.lutece.portal.web.constants.Parameters;
67 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
68 import fr.paris.lutece.util.ReferenceList;
69 import fr.paris.lutece.util.html.AbstractPaginator;
70 import fr.paris.lutece.util.html.HtmlTemplate;
71 import fr.paris.lutece.util.http.SecurityUtil;
72 import fr.paris.lutece.util.sort.AttributeComparator;
73 import fr.paris.lutece.util.url.UrlItem;
74
75
76
77
78 public class MailingListJspBean extends AdminFeaturesPageJspBean
79 {
80 private static final long serialVersionUID = -1589153949244582338L;
81
82
83 public static final String RIGHT_MANAGE_MAILINGLISTS = "CORE_MAILINGLISTS_MANAGEMENT";
84
85
86 private static final String TEMPLATE_MANAGE_MAILINGLISTS = "admin/mailinglist/manage_mailinglists.html";
87 private static final String TEMPLATE_CREATE_MAILINGLIST = "admin/mailinglist/create_mailinglist.html";
88 private static final String TEMPLATE_MODIFY_MAILINGLIST = "admin/mailinglist/modify_mailinglist.html";
89 private static final String TEMPLATE_ADD_USERS = "admin/mailinglist/add_users.html";
90 private static final String TEMPLATE_VIEW_USERS = "admin/mailinglist/view_users.html";
91
92
93 private static final String MARK_MAILINGLISTS_LIST = "mailinglists_list";
94 private static final String MARK_WORKGROUPS_LIST = "workgroups_list";
95 private static final String MARK_WORKGROUP_SELECTED = "selected_workgroup";
96 private static final String MARK_ROLES_LIST = "roles_list";
97 private static final String MARK_RECIPIENTS_LIST = "recipients_list";
98 private static final String MARK_MAILINGLIST = "mailinglist";
99 private static final String MARK_MAILINGLIST_FILTER = "mailinglistFilter";
100 private static final String MARK_PAGINATOR = "paginator";
101 private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
102
103
104 private static final String PROPERTY_CREATE_MAILINGLIST_PAGETITLE = "portal.mailinglist.create_mailinglist.pageTitle";
105 private static final String PROPERTY_MODIFY_MAILINGLIST_PAGETITLE = "portal.mailinglist.modify_mailinglist.pageTitle";
106 private static final String PROPERTY_VIEW_USERS_PAGETITLE = "portal.mailinglist.view_users.pageTitle";
107 private static final String PROPERTY_ADD_USERS_PAGETITLE = "portal.mailinglist.add_users.pageTitle";
108 private static final String PROPERTY_MAILINGLIST_PER_PAGE = "paginator.mailinglist.itemsPerPage";
109 private static final String MESSAGE_CONFIRM_REMOVE = "portal.mailinglist.message.confirmRemoveMailingList";
110 private static final String MESSAGE_CANNOT_REMOVE = "portal.mailinglist.message.cannotRemoveMailingList";
111 private static final String MESSAGE_FILTER_ALREADY_EXISTS = "portal.mailinglist.message.filterAlreadyExists";
112
113
114 private static final String PARAMETER_WORKGROUP = "workgroup";
115 private static final String PARAMETER_ROLE = "role";
116 private static final String PARAMETER_MAILINGLIST_ID = "id_mailinglist";
117 private static final String PARAMETER_NAME = "name";
118 private static final String PARAMETER_DESCRIPTION = "description";
119 private static final String PARAMETER_SESSION = "session";
120
121
122 private static final String JSP_MODIFY_MAILINGLIST = "ModifyMailingList.jsp";
123 private static final String JSP_URL_REMOVE_MAILINGLIST = "jsp/admin/mailinglist/DoRemoveMailingList.jsp";
124 private static final String JSP_URL_MANAGE_MAILINGLISTS = "jsp/admin/mailinglist/ManageMailingLists.jsp";
125 private MailingListFilter _mailingListFilter;
126 private int _nItemsPerPage;
127 private String _strCurrentPageIndex;
128
129
130
131
132
133
134
135
136 public String getManageMailinglists( HttpServletRequest request )
137 {
138 Map<String, Object> model = new HashMap<>( );
139
140
141 if ( StringUtils.isBlank( request.getParameter( PARAMETER_SESSION ) ) )
142 {
143 _mailingListFilter = new MailingListFilter( );
144 populate( _mailingListFilter, request );
145 }
146
147 List<MailingList> listMailinglists = AdminMailingListService.getUserMailingListsByFilter( getUser( ), _mailingListFilter );
148
149
150 String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
151 String strAscSort = request.getParameter( Parameters.SORTED_ASC );
152 boolean bIsAscSort = true;
153
154 if ( StringUtils.isBlank( strSortedAttributeName ) )
155 {
156 strSortedAttributeName = PARAMETER_NAME;
157 }
158
159 if ( StringUtils.isNotBlank( strAscSort ) )
160 {
161 bIsAscSort = Boolean.parseBoolean( strAscSort );
162 }
163
164 Collections.sort( listMailinglists, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
165
166
167 _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
168 int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_MAILINGLIST_PER_PAGE, 50 );
169 _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );
170
171 UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MANAGE_MAILINGLISTS );
172 url.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, strSortedAttributeName );
173 url.addParameter( Parameters.SORTED_ASC, Boolean.toString( bIsAscSort ) );
174 url.addParameter( PARAMETER_SESSION, PARAMETER_SESSION );
175
176 LocalizedPaginator<MailingList> paginator = new LocalizedPaginator<>( listMailinglists, _nItemsPerPage, url.getUrl( ),
177 AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, request.getLocale( ) );
178
179 model.put( MARK_MAILINGLISTS_LIST, paginator.getPageItems( ) );
180 model.put( MARK_PAGINATOR, paginator );
181 model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( paginator.getItemsPerPage( ) ) );
182 model.put( MARK_MAILINGLIST_FILTER, _mailingListFilter );
183
184 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_MAILINGLISTS, getLocale( ), model );
185
186 return getAdminPage( template.getHtml( ) );
187 }
188
189
190
191
192
193
194
195
196 public String getCreateMailinglist( HttpServletRequest request )
197 {
198 setPageTitleProperty( PROPERTY_CREATE_MAILINGLIST_PAGETITLE );
199
200 ReferenceList listWorkgroups = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
201
202 HashMap<String, Object> model = new HashMap<>( );
203 model.put( MARK_WORKGROUPS_LIST, listWorkgroups );
204
205
206 if ( !listWorkgroups.isEmpty( ) )
207 {
208 model.put( MARK_WORKGROUP_SELECTED, listWorkgroups.get( 0 ).getCode( ) );
209 }
210
211 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_MAILINGLIST ) );
212 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_MAILINGLIST, getLocale( ), model );
213
214 return getAdminPage( template.getHtml( ) );
215 }
216
217
218
219
220
221
222
223
224
225
226 public String doCreateMailingList( HttpServletRequest request ) throws AccessDeniedException
227 {
228 MailingListmailinglist/MailingList.html#MailingList">MailingList mailinglist = new MailingList( );
229 String strErrors = processFormData( request, mailinglist );
230
231 if ( strErrors != null )
232 {
233 return AdminMessageService.getMessageUrl( request, strErrors, AdminMessage.TYPE_STOP );
234 }
235 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_MAILINGLIST ) )
236 {
237 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
238 }
239
240 MailingListHome.create( mailinglist );
241
242
243 UrlItemrlItem.html#UrlItem">UrlItem urlModify = new UrlItem( JSP_MODIFY_MAILINGLIST );
244 urlModify.addParameter( PARAMETER_MAILINGLIST_ID, mailinglist.getId( ) );
245
246 return urlModify.getUrl( );
247 }
248
249
250
251
252
253
254
255
256 public String getModifyMailinglist( HttpServletRequest request )
257 {
258 setPageTitleProperty( PROPERTY_MODIFY_MAILINGLIST_PAGETITLE );
259
260 ReferenceList listWorkgroups = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
261
262 String strMailingListId = request.getParameter( PARAMETER_MAILINGLIST_ID );
263
264 if ( !StringUtils.isNumeric( strMailingListId ) )
265 {
266 AppLogService.error( " {} is not a valid mailing list id.", ( ) -> SecurityUtil.logForgingProtect( strMailingListId ) );
267
268 return getManageMailinglists( request );
269 }
270
271 int nMailingListId = Integer.parseInt( strMailingListId );
272 MailingList mailinglist = MailingListHome.findByPrimaryKey( nMailingListId );
273
274 if ( mailinglist == null )
275 {
276 AppLogService.error( "{} is not a valid mailing list id.", ( ) -> SecurityUtil.logForgingProtect( strMailingListId ) );
277
278 return getManageMailinglists( request );
279 }
280
281 Map<String, Object> model = new HashMap<>( );
282 model.put( MARK_WORKGROUPS_LIST, listWorkgroups );
283 model.put( MARK_MAILINGLIST, mailinglist );
284 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_MAILINGLIST ) );
285
286 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_MAILINGLIST, getLocale( ), model );
287
288 return getAdminPage( template.getHtml( ) );
289 }
290
291
292
293
294
295
296
297
298
299
300 public String doModifyMailingList( HttpServletRequest request ) throws AccessDeniedException
301 {
302 String strId = request.getParameter( PARAMETER_MAILINGLIST_ID );
303 int nId = Integer.parseInt( strId );
304 MailingList mailinglist = MailingListHome.findByPrimaryKey( nId );
305
306 String strErrors = processFormData( request, mailinglist );
307
308 if ( strErrors != null )
309 {
310 return AdminMessageService.getMessageUrl( request, strErrors, AdminMessage.TYPE_STOP );
311 }
312 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_MAILINGLIST ) )
313 {
314 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
315 }
316
317 MailingListHome.update( mailinglist );
318
319 return getHomeUrl( request );
320 }
321
322
323
324
325
326
327
328
329 public String getConfirmRemoveMailingList( HttpServletRequest request )
330 {
331 String strId = request.getParameter( PARAMETER_MAILINGLIST_ID );
332
333 ArrayList<String> listErrors = new ArrayList<>( );
334
335 if ( !MailingListRemovalListenerService.getService( ).checkForRemoval( strId, listErrors, getLocale( ) ) )
336 {
337 String strCause = AdminMessageService.getFormattedList( listErrors, getLocale( ) );
338 Object [ ] args = {
339 strCause
340 };
341 return AdminMessageService.getMessageUrl( request, MESSAGE_CANNOT_REMOVE, args, AdminMessage.TYPE_STOP );
342 }
343 String strUrlRemove = JSP_URL_REMOVE_MAILINGLIST;
344 Map<String, String> parameters = new HashMap<>( );
345 parameters.put( PARAMETER_MAILINGLIST_ID, strId );
346 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_URL_REMOVE_MAILINGLIST ) );
347 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE, strUrlRemove, AdminMessage.TYPE_CONFIRMATION, parameters );
348
349 }
350
351
352
353
354
355
356
357
358
359
360 public String doRemoveMailingList( HttpServletRequest request ) throws AccessDeniedException
361 {
362 if ( !SecurityTokenService.getInstance( ).validate( request, JSP_URL_REMOVE_MAILINGLIST ) )
363 {
364 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
365 }
366 String strId = request.getParameter( PARAMETER_MAILINGLIST_ID );
367 int nId = Integer.parseInt( strId );
368
369 MailingListHome.remove( nId );
370
371 return getHomeUrl( request );
372 }
373
374
375
376
377
378
379
380
381 public String getViewUsers( HttpServletRequest request )
382 {
383 setPageTitleProperty( PROPERTY_VIEW_USERS_PAGETITLE );
384
385 HashMap<String, Object> model = new HashMap<>( );
386 Collection<Recipient> listRecipients;
387 String strId = request.getParameter( PARAMETER_MAILINGLIST_ID );
388
389 if ( strId != null )
390 {
391 int nIdMailingList = Integer.parseInt( strId );
392 listRecipients = AdminMailingListService.getRecipients( nIdMailingList );
393 }
394 else
395 {
396 String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP );
397 String strRole = request.getParameter( PARAMETER_ROLE );
398 listRecipients = AdminMailingListService.getRecipients( strWorkgroup, strRole );
399 }
400
401 model.put( MARK_RECIPIENTS_LIST, listRecipients );
402
403 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_USERS, getLocale( ), model );
404
405 return getAdminPage( template.getHtml( ) );
406 }
407
408
409
410
411
412
413
414
415 public String getAddUsers( HttpServletRequest request )
416 {
417 setPageTitleProperty( PROPERTY_ADD_USERS_PAGETITLE );
418
419 String strId = request.getParameter( PARAMETER_MAILINGLIST_ID );
420 int nId = Integer.parseInt( strId );
421 MailingList mailinglist = MailingListHome.findByPrimaryKey( nId );
422
423 if ( mailinglist == null )
424 {
425 return getManageMailinglists( request );
426 }
427
428 ReferenceList listWorkgroups = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
429 ReferenceList listRoles = RBACRoleHome.getRolesList( );
430 listRoles.addItem( AdminMailingListService.ALL_ROLES, AdminMailingListService.ALL_ROLES );
431
432 Map<String, Object> model = new HashMap<>( );
433 model.put( MARK_WORKGROUPS_LIST, listWorkgroups );
434 model.put( MARK_ROLES_LIST, listRoles );
435 model.put( MARK_MAILINGLIST, mailinglist );
436 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_ADD_USERS ) );
437
438 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADD_USERS, getLocale( ), model );
439
440 return getAdminPage( template.getHtml( ) );
441 }
442
443
444
445
446
447
448
449
450
451
452 public String doAddUsers( HttpServletRequest request ) throws AccessDeniedException
453 {
454 String strId = request.getParameter( PARAMETER_MAILINGLIST_ID );
455 String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP );
456 String strRole = request.getParameter( PARAMETER_ROLE );
457
458 int nId = Integer.parseInt( strId );
459 MailingListUsersFilterglist/MailingListUsersFilter.html#MailingListUsersFilter">MailingListUsersFilter filter = new MailingListUsersFilter( );
460 filter.setWorkgroup( strWorkgroup );
461 filter.setRole( strRole );
462
463 if ( !AdminMailingListService.checkFilter( filter, nId ) )
464 {
465 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_ADD_USERS ) )
466 {
467 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
468 }
469 MailingListHome.addFilterToMailingList( filter, nId );
470
471
472 UrlItemrlItem.html#UrlItem">UrlItem urlModify = new UrlItem( JSP_MODIFY_MAILINGLIST );
473 urlModify.addParameter( PARAMETER_MAILINGLIST_ID, nId );
474
475 return urlModify.getUrl( );
476 }
477
478 return AdminMessageService.getMessageUrl( request, MESSAGE_FILTER_ALREADY_EXISTS, AdminMessage.TYPE_STOP );
479 }
480
481
482
483
484
485
486
487
488
489
490 public String doDeleteFilter( HttpServletRequest request ) throws AccessDeniedException
491 {
492 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_MAILINGLIST ) )
493 {
494 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
495 }
496
497 String strId = request.getParameter( PARAMETER_MAILINGLIST_ID );
498 String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP );
499 String strRole = request.getParameter( PARAMETER_ROLE );
500
501 int nId = Integer.parseInt( strId );
502 MailingListUsersFilterglist/MailingListUsersFilter.html#MailingListUsersFilter">MailingListUsersFilter filter = new MailingListUsersFilter( );
503 filter.setWorkgroup( strWorkgroup );
504 filter.setRole( strRole );
505 MailingListHome.deleteFilterToMailingList( filter, nId );
506
507
508 UrlItemrlItem.html#UrlItem">UrlItem urlModify = new UrlItem( JSP_MODIFY_MAILINGLIST );
509 urlModify.addParameter( PARAMETER_MAILINGLIST_ID, nId );
510
511 return urlModify.getUrl( );
512 }
513
514
515
516
517
518
519
520
521
522
523 private String processFormData( HttpServletRequest request, MailingList mailinglist )
524 {
525 String strErrors = null;
526 String strName = request.getParameter( PARAMETER_NAME );
527 String strDescription = request.getParameter( PARAMETER_DESCRIPTION );
528 String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP );
529
530 if ( ( strName == null ) || ( strName.equals( "" ) ) || ( strDescription == null ) || ( strDescription.equals( "" ) ) )
531 {
532 return Messages.MANDATORY_FIELDS;
533 }
534
535 mailinglist.setName( strName );
536 mailinglist.setDescription( strDescription );
537 mailinglist.setWorkgroup( strWorkgroup );
538
539 return strErrors;
540 }
541 }