1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.social.webui.space;
18
19 import java.util.*;
20
21 import org.apache.commons.lang.ArrayUtils;
22 import org.apache.commons.lang.StringEscapeUtils;
23 import org.apache.commons.lang.StringUtils;
24
25 import org.exoplatform.commons.utils.LazyPageList;
26 import org.exoplatform.container.ExoContainerContext;
27 import org.exoplatform.container.PortalContainer;
28 import org.exoplatform.portal.application.PortalRequestContext;
29 import org.exoplatform.portal.webui.util.Util;
30 import org.exoplatform.services.log.ExoLogger;
31 import org.exoplatform.services.log.Log;
32 import org.exoplatform.services.organization.MembershipTypeHandler;
33 import org.exoplatform.social.core.identity.model.Identity;
34 import org.exoplatform.social.core.identity.model.Profile;
35 import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
36 import org.exoplatform.social.core.manager.IdentityManager;
37 import org.exoplatform.social.core.space.SpaceUtils;
38 import org.exoplatform.social.core.space.model.Space;
39 import org.exoplatform.social.core.space.spi.SpaceService;
40 import org.exoplatform.social.webui.StringListAccess;
41 import org.exoplatform.social.webui.UIUserInvitation;
42 import org.exoplatform.social.webui.Utils;
43 import org.exoplatform.web.application.ApplicationMessage;
44 import org.exoplatform.web.application.RequestContext;
45 import org.exoplatform.webui.application.WebuiRequestContext;
46 import org.exoplatform.webui.config.annotation.ComponentConfig;
47 import org.exoplatform.webui.config.annotation.ComponentConfigs;
48 import org.exoplatform.webui.config.annotation.EventConfig;
49 import org.exoplatform.webui.core.UIApplication;
50 import org.exoplatform.webui.core.UIContainer;
51 import org.exoplatform.webui.core.UIPageIterator;
52 import org.exoplatform.webui.core.UIPopupWindow;
53 import org.exoplatform.webui.event.Event;
54 import org.exoplatform.webui.event.EventListener;
55 import org.exoplatform.webui.organization.account.UIUserSelector;
56
57
58 @ComponentConfigs ({
59 @ComponentConfig(
60 template = "war:/groovy/social/webui/space/UISpaceMember.gtmpl",
61 events = {
62 @EventConfig(listeners = UISpaceMember.RevokeInvitedUserActionListener.class),
63 @EventConfig(listeners = UISpaceMember.DeclineUserActionListener.class),
64 @EventConfig(listeners = UISpaceMember.ValidateUserActionListener.class),
65 @EventConfig(listeners = UISpaceMember.RemoveUserActionListener.class),
66 @EventConfig(listeners = UISpaceMember.ToggleLeadershipActionListener.class)
67 }
68 )
69 })
70
71 public class UISpaceMember extends UIContainer {
72
73 private static final Log LOG = ExoLogger.getLogger(UISpaceMember.class);
74
75
76 private static final String MSG_ERROR_NOT_LEADER = "UISpaceMember.msg.error_not_leader";
77 private static final String MSG_ERROR_SELF_REMOVE_LEADER = "UISpaceMember.msg.error_self_remove_leader";
78 private static final String MSG_ERROR_SELF_REMOVE_LEADER_YOU = "UISpaceMember.msg.error_self_remove_leader_you";
79 private static final String MSG_ERROR_SELF_REMOVE_LEADER_ARE = "UISpaceMember.msg.error_self_remove_leader_are";
80 private static final String MSG_ERROR_SELF_REMOVE_LEADER_IS = "UISpaceMember.msg.error_self_remove_leader_is";
81 private static final String MSG_ERROR_SELF_REMOVE_LEADER_LEAVING_IT = "UISpaceMember.msg.error_self_remove_leader_leaving_it";
82 private static final String MSG_ERROR_SELF_REMOVE_LEADER_REMOVING_THE_RIGHTS = "UISpaceMember.msg.error_self_remove_leader_removing_the_rights";
83 private static final String MSG_ERROR_MEMBERS_LOADING = "UISpaceMember.label.MembersLoadingError";
84 private static final String MSG_ERROR_PENDING_USERS_LOADING = "UISpaceMember.label.PendingUsersLoadingError";
85 private static final String MSG_ERROR_INVITED_USERS_LOADING = "UISpaceMember.label.InvitedUsersLoadingError";
86
87
88
89 private static final int FIRST_PAGE = 1;
90
91 private String spaceId;
92 private SpaceService spaceService = null;
93 private UIPageIterator iteratorPendingUsers;
94 private UIPageIterator iteratorInvitedUsers;
95 private UIPageIterator iteratorExistingUsers;
96 private final String iteratorPendingID = "UIIteratorPendding";
97 private final String iteratorInvitedID = "UIIteratorInvited";
98 private final String iteratorExistingID = "UIIteratorExisting";
99 private final Integer PENDING_PER_PAGE = 10;
100 private final Integer INVITATION_PER_PAGE = 10;
101 private final Integer MEMBERS_PER_PAGE = 20;
102 private static final String USER_TO_INVITE = "user_to_invite";
103 String typeOfRelation = null;
104 String spaceURL = null;
105 private boolean hasErr = false;
106 private IdentityManager identityManager;
107
108
109
110
111 private boolean isNewSearch;
112
113
114
115
116
117
118
119 public UISpaceMember() throws Exception {
120 addChild(UIUserInvitation.class, null, null);
121 identityManager = ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(IdentityManager.class);
122 iteratorPendingUsers = createUIComponent(UIPageIterator.class, null, iteratorPendingID);
123 iteratorInvitedUsers = createUIComponent(UIPageIterator.class, null, iteratorInvitedID);
124 iteratorExistingUsers = createUIComponent(UIPageIterator.class, null, iteratorExistingID);
125 addChild(iteratorPendingUsers);
126 addChild(iteratorInvitedUsers);
127 addChild(iteratorExistingUsers);
128 setTypeOfRelation(USER_TO_INVITE);
129 }
130
131
132
133
134
135
136 public UIPageIterator getUIPageIteratorPendingUsers() {
137 return iteratorPendingUsers;
138 }
139
140
141
142
143
144
145 public UIPageIterator getUIPageIteratorInvitedUsers() {
146 return iteratorInvitedUsers;
147 }
148
149
150
151
152
153
154 public UIPageIterator getUIPageIteratorExistingUsers() {
155 return iteratorExistingUsers;
156 }
157
158
159
160
161
162
163
164 public void setValue(String spaceId) throws Exception {
165 this.spaceId = spaceId;
166 }
167
168
169
170
171
172 public String getTypeOfRelation() {
173 return typeOfRelation;
174 }
175
176
177
178
179
180
181 public void setTypeOfRelation(String typeOfRelation) {
182 this.typeOfRelation = typeOfRelation;
183 }
184
185
186
187
188 public String getSpaceURL() {
189 return spaceURL;
190 }
191
192
193
194
195
196
197 public void setSpaceURL(String spaceURL) {
198 this.spaceURL = spaceURL;
199 }
200
201
202
203
204 public boolean isHasErr() {
205 return hasErr;
206 }
207
208
209
210
211 public void setHasErr(boolean hasErr) {
212 this.hasErr = hasErr;
213 }
214
215
216
217
218
219
220 public String getCurrentUserName() {
221 RequestContext context = RequestContext.getCurrentInstance();
222 return context.getRemoteUser();
223 }
224
225
226
227
228
229
230 protected String getRestContextName() {
231 return PortalContainer.getCurrentRestContextName();
232 }
233
234
235
236
237
238
239 protected String getPortalName() {
240 return PortalContainer.getCurrentPortalContainerName();
241 }
242
243
244
245
246
247
248 @SuppressWarnings("unchecked")
249 public List<String> getPendingUsers() throws Exception {
250 SpaceService spaceService = getSpaceService();
251 Space space = spaceService.getSpaceById(spaceId);
252 try {
253 if (space == null) {
254 throw new Exception("Could not get the space with ID "+ spaceId);
255 }
256
257 String[] pendingUsers = space.getPendingUsers();
258 if (pendingUsers == null || pendingUsers.length == 0) {
259 return new ArrayList<String>();
260 }
261 pendingUsers = sortUsers(pendingUsers);
262
263 int currentPage = iteratorPendingUsers.getCurrentPage();
264 LazyPageList<String> pageList = new LazyPageList<String>(
265 new StringListAccess(Arrays.asList(pendingUsers)),
266 PENDING_PER_PAGE);
267 iteratorPendingUsers.setPageList(pageList);
268 int pageCount = iteratorPendingUsers.getAvailablePage();
269 if (pageCount >= currentPage) {
270 iteratorPendingUsers.setCurrentPage(currentPage);
271 } else if (pageCount < currentPage) {
272 iteratorPendingUsers.setCurrentPage(currentPage - 1);
273 }
274 return iteratorPendingUsers.getCurrentPageData();
275 } catch(Exception e) {
276 LOG.error("An error occurred while retrieving space pending users",e);
277 WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
278 UIApplication uiApplication = context.getUIApplication();
279 uiApplication.addMessage(new ApplicationMessage(MSG_ERROR_PENDING_USERS_LOADING,
280 null,
281 ApplicationMessage.ERROR));
282 }
283 return new ArrayList<>();
284 }
285
286
287
288
289
290
291 @SuppressWarnings("unchecked")
292 public List<String> getInvitedUsers() {
293 SpaceService spaceService = getSpaceService();
294 Space space = spaceService.getSpaceById(spaceId);
295 try {
296 if (space == null) {
297 throw new Exception("Could not get the space with ID "+ spaceId);
298 }
299
300 String[] invitedUsers = space.getInvitedUsers();
301 if (invitedUsers == null || invitedUsers.length == 0) {
302 return new ArrayList<String>();
303 }
304 invitedUsers = sortUsers(invitedUsers);
305
306 int currentPage = iteratorInvitedUsers.getCurrentPage();
307 LazyPageList<String> pageList = new LazyPageList<String>(
308 new StringListAccess(Arrays.asList(invitedUsers)),
309 INVITATION_PER_PAGE);
310 iteratorInvitedUsers.setPageList(pageList);
311 int pageCount = iteratorInvitedUsers.getAvailablePage();
312 if (pageCount >= currentPage) {
313 iteratorInvitedUsers.setCurrentPage(currentPage);
314 } else if (pageCount < currentPage) {
315 iteratorInvitedUsers.setCurrentPage(currentPage - 1);
316 }
317
318 return iteratorInvitedUsers.getCurrentPageData();
319 } catch(Exception e) {
320 LOG.error("An error occurred while retrieving space invited users",e);
321 WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
322 UIApplication uiApplication = context.getUIApplication();
323 uiApplication.addMessage(new ApplicationMessage(MSG_ERROR_INVITED_USERS_LOADING,
324 null,
325 ApplicationMessage.ERROR));
326 }
327 return new ArrayList<>();
328 }
329
330 private String[] sortUsers(String[] users) {
331 List<String> sortedIdentities = identityManager.sortIdentities(Arrays.asList(users), Profile.FULL_NAME);
332 users = sortedIdentities.toArray(new String[0]);
333 return users;
334 }
335
336
337
338
339
340
341 @SuppressWarnings("unchecked")
342 public List<String> getExistingUsers() {
343 SpaceService spaceService = getSpaceService();
344 Space space = spaceService.getSpaceById(spaceId);
345
346 try {
347 if (space == null) {
348 throw new Exception("Could not get the space with ID "+ spaceId);
349 }
350
351 String[] memberUsers = space.getMembers();
352 if (memberUsers == null || memberUsers.length == 0) {
353 return new ArrayList<String>();
354 }
355 memberUsers = sortUsers(memberUsers);
356
357 int currentPage = iteratorExistingUsers.getCurrentPage();
358 Set<String> users = new LinkedHashSet<String>(Arrays.asList(memberUsers));
359
360 LazyPageList<String> pageList = new LazyPageList<>(new StringListAccess(new ArrayList<>(users)), MEMBERS_PER_PAGE);
361 iteratorExistingUsers.setPageList(pageList);
362 if (this.isNewSearch()) {
363 iteratorExistingUsers.setCurrentPage(FIRST_PAGE);
364 } else {
365 iteratorExistingUsers.setCurrentPage(currentPage);
366 }
367 this.setNewSearch(false);
368 return iteratorExistingUsers.getCurrentPageData();
369 }catch (Exception e) {
370 LOG.error("An error occurred while retrieving space members",e);
371 WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
372 UIApplication uiApplication = context.getUIApplication();
373 uiApplication.addMessage(new ApplicationMessage(MSG_ERROR_MEMBERS_LOADING,
374 null,
375 ApplicationMessage.ERROR));
376 }
377 return new ArrayList<>();
378 }
379
380
381
382
383
384
385 public void setUsersName(String userName) {
386
387 }
388
389
390
391
392
393
394 public String getUsersName() {
395
396
397 return null;
398 }
399
400
401
402
403
404
405
406 public boolean isSuperManager() throws Exception {
407 return spaceService.isSuperManager(getRemoteUser());
408 }
409
410
411
412
413
414
415
416 public String getHomeSpaceUrl() throws Exception {
417 Space space = getSpaceService().getSpaceById(spaceId);
418 return Utils.getSpaceHomeURL(space);
419 }
420
421
422
423
424
425
426
427 public String getManageSpacesUrl() throws Exception {
428
429 return Utils.getURI("spaces");
430 }
431
432
433
434
435
436
437
438
439 public boolean isLeader(String userName) throws Exception {
440 SpaceService spaceService = getSpaceService();
441 Space space = spaceService.getSpaceById(spaceId);
442 return spaceService.isManager(space, userName);
443 }
444
445
446
447
448
449
450
451 protected boolean hasWildCardMembership(String userId) {
452 SpaceService spaceService = getSpaceService();
453 Space space = spaceService.getSpaceById(spaceId);
454 return SpaceUtils.isUserHasMembershipTypesInGroup(userId, space.getGroupId(), MembershipTypeHandler.ANY_MEMBERSHIP_TYPE);
455 }
456
457 public boolean isCurrentUser(String userName) throws Exception {
458 return (getRemoteUser().equals(userName));
459 }
460
461
462
463
464
465
466 static public class RevokeInvitedUserActionListener extends EventListener<UISpaceMember> {
467 public void execute(Event<UISpaceMember> event) throws Exception {
468 UISpaceMember uiSpaceMember = event.getSource();
469 WebuiRequestContext requestContext = event.getRequestContext();
470 String userName = event.getRequestContext().getRequestParameter(OBJECTID);
471
472 SpaceService spaceService = uiSpaceMember.getSpaceService();
473 spaceService.removeInvitedUser(spaceService.getSpaceById(uiSpaceMember.spaceId), userName);
474 requestContext.addUIComponentToUpdateByAjax(uiSpaceMember);
475 }
476 }
477
478
479
480
481
482
483 static public class DeclineUserActionListener extends EventListener<UISpaceMember> {
484 public void execute(Event<UISpaceMember> event) throws Exception {
485 UISpaceMember uiSpaceMember = event.getSource();
486 WebuiRequestContext requestContext = event.getRequestContext();
487 String userName = event.getRequestContext().getRequestParameter(OBJECTID);
488
489 SpaceService spaceService = uiSpaceMember.getSpaceService();
490 spaceService.removePendingUser(spaceService.getSpaceById(uiSpaceMember.spaceId), userName);
491 requestContext.addUIComponentToUpdateByAjax(uiSpaceMember);
492 }
493 }
494
495
496
497
498
499
500 static public class RemoveUserActionListener extends EventListener<UISpaceMember> {
501
502 public void execute(Event<UISpaceMember> event) throws Exception {
503 UISpaceMember uiSpaceMember = event.getSource();
504 WebuiRequestContext requestContext = event.getRequestContext();
505 PortalRequestContext prContext = Util.getPortalRequestContext();
506 boolean useAjax = prContext.useAjax();
507 UIApplication uiApp = requestContext.getUIApplication();
508 SpaceService spaceService = uiSpaceMember.getSpaceService();
509 Space space = spaceService.getSpaceById(uiSpaceMember.spaceId);
510 String currentUser = requestContext.getRemoteUser();
511 String userName = event.getRequestContext().getRequestParameter(OBJECTID);
512 if (!useAjax) {
513 userName = currentUser;
514 }
515
516 if (spaceService.isOnlyManager(space, userName)) {
517 uiApp.addMessage(new ApplicationMessage(MSG_ERROR_SELF_REMOVE_LEADER,
518 uiSpaceMember.makeParamSelfRemoveLeaderErrorMessage(userName, currentUser),
519 ApplicationMessage.WARNING));
520 return;
521 }
522
523 spaceService.removeMember(space, userName);
524 spaceService.setManager(spaceService.getSpaceById(space.getId()), userName, false);
525
526 if (!useAjax) {
527 prContext = Util.getPortalRequestContext();
528 prContext.setResponseComplete(true);
529 StringBuffer url = new StringBuffer();
530 if (uiSpaceMember.isSuperManager()) {
531 url.append(Utils.getSpaceHomeURL(space)).append("/SpaceSettingPortlet");
532 } else {
533 url.append(Utils.getURI("spaces"));
534 }
535 prContext.getResponse().sendRedirect(url.toString());
536 return;
537 } else {
538 requestContext.addUIComponentToUpdateByAjax(uiSpaceMember);
539 }
540 }
541 }
542
543
544
545
546
547
548 static public class ValidateUserActionListener extends EventListener<UISpaceMember> {
549 public void execute(Event<UISpaceMember> event) throws Exception {
550 UISpaceMember uiSpaceMember = event.getSource();
551 WebuiRequestContext requestContext = event.getRequestContext();
552 String userName = event.getRequestContext().getRequestParameter(OBJECTID);
553
554 SpaceService spaceService = uiSpaceMember.getSpaceService();
555 spaceService.addMember(spaceService.getSpaceById(uiSpaceMember.spaceId), userName);
556 requestContext.addUIComponentToUpdateByAjax(uiSpaceMember);
557 }
558 }
559
560 static public class ToggleLeadershipActionListener extends EventListener<UISpaceMember> {
561 @Override
562 public void execute(Event<UISpaceMember> event) throws Exception {
563 UISpaceMember uiSpaceMember = event.getSource();
564 SpaceService spaceService = uiSpaceMember.getSpaceService();
565 WebuiRequestContext rcontext = event.getRequestContext();
566 String authenticatedUser = rcontext.getRemoteUser();
567 Space space = spaceService.getSpaceById(uiSpaceMember.spaceId);
568
569
570 if(!spaceService.isSuperManager(authenticatedUser) && !spaceService.isManager(space, authenticatedUser)) {
571 UIApplication uiApp = rcontext.getUIApplication();
572 uiApp.addMessage(new ApplicationMessage(MSG_ERROR_NOT_LEADER, new String[]{}, ApplicationMessage.WARNING));
573 return;
574 }
575
576 String targetUser = rcontext.getRequestParameter(OBJECTID);
577 space.setEditor(Utils.getViewerRemoteId());
578
579 boolean success = false;
580 if (spaceService.isManager(space, targetUser)) {
581 if (spaceService.isOnlyManager(space, targetUser)) {
582 UIApplication uiApp = rcontext.getUIApplication();
583 uiApp.addMessage(new ApplicationMessage(MSG_ERROR_SELF_REMOVE_LEADER,
584 uiSpaceMember.makeParamSelfRemoveLeaderErrorMessage(targetUser, authenticatedUser),
585 ApplicationMessage.WARNING));
586 return;
587 }
588 spaceService.setManager(space, targetUser, false);
589 success = true;
590 } else if (spaceService.isMember(space, targetUser)) {
591 spaceService.setManager(space, targetUser, true);
592 success = true;
593 }
594
595 if (success) {
596 ((WebuiRequestContext) rcontext.getParentAppRequestContext()).setResponseComplete(true);
597 }
598 }
599 }
600
601
602
603
604
605
606 static public class CloseActionListener extends EventListener<UIUserSelector> {
607 public void execute(Event<UIUserSelector> event) throws Exception {
608 UIUserSelector uiForm = event.getSource();
609 UISpaceMember uiSpaceMember = uiForm.getAncestorOfType(UISpaceMember.class);
610 UIPopupWindow uiPopup = uiSpaceMember.getChild(UIPopupWindow.class);
611 uiPopup.setUIComponent(null);
612 uiPopup.setShow(false);
613 event.getRequestContext().addUIComponentToUpdateByAjax(uiSpaceMember);
614 }
615 }
616
617 protected boolean isMember(String userId) {
618 SpaceService spaceService = getSpaceService();
619 Space space = spaceService.getSpaceById(spaceId);
620 try {
621 if (ArrayUtils.contains(space.getMembers(), userId)) {
622 return true;
623 }
624 } catch (Exception e) {
625 return false;
626 }
627 return false;
628 }
629
630
631
632
633
634
635
636 private SpaceService getSpaceService() {
637 if (spaceService == null) {
638 spaceService = getApplicationComponent(SpaceService.class);
639 }
640 return spaceService;
641 }
642
643
644
645
646
647
648
649 private String getRemoteUser() throws Exception {
650 return Util.getPortalRequestContext().getRemoteUser();
651 }
652
653
654
655
656
657
658
659 public String getFullName(String userId) {
660 Identity identity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, userId, true);
661
662 if (identity == null) return StringUtils.EMPTY;
663
664 return StringEscapeUtils.escapeHtml(identity.getProfile().getFullName());
665 }
666
667 public boolean isNewSearch() {
668 return isNewSearch;
669 }
670
671 public void setNewSearch(boolean isNewSearch) {
672 this.isNewSearch = isNewSearch;
673 }
674
675 private String[] makeParamSelfRemoveLeaderErrorMessage(String userName, String currentUser) {
676 if (userName == null || currentUser == null) {
677 return null;
678 }
679 if (currentUser.equals(userName)) {
680 return new String[] {MSG_ERROR_SELF_REMOVE_LEADER_YOU, MSG_ERROR_SELF_REMOVE_LEADER_ARE, MSG_ERROR_SELF_REMOVE_LEADER_LEAVING_IT};
681 }
682 return new String[] {getFullName(userName), MSG_ERROR_SELF_REMOVE_LEADER_IS, MSG_ERROR_SELF_REMOVE_LEADER_REMOVING_THE_RIGHTS};
683 }
684 }