Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.exoplatform.commons.api.notification.model.PluginKey;
import org.exoplatform.commons.notification.impl.NotificationContextImpl;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.services.security.ConversationState;
import org.exoplatform.task.dto.CommentDto;
import org.exoplatform.task.dto.ProjectDto;
Expand All @@ -43,10 +43,10 @@

public class TaskCommentNotificationListener extends Listener<TaskDto, CommentDto> {

private final OrganizationService organizationService;
private final UserACL userAcl;

public TaskCommentNotificationListener(OrganizationService organizationService) {
this.organizationService = organizationService;
public TaskCommentNotificationListener(UserACL userAcl) {
this.userAcl = userAcl;
}

@Override
Expand All @@ -67,7 +67,7 @@ private NotificationContext buildContext(TaskDto task, CommentDto comment) {
ctx.append(NotificationUtils.CREATOR, creator);

//. Receiver
Set<String> receiver = new HashSet<String>();
Set<String> receiver = new HashSet<>();

// Task creator
receiver.add(task.getCreatedBy());
Expand Down Expand Up @@ -111,7 +111,7 @@ private NotificationContext buildContext(TaskDto task, CommentDto comment) {
mentioned.remove(creator);
if (task.getStatus() != null && task.getStatus().getProject() != null) {
ProjectDto project = task.getStatus().getProject();
receiver.removeIf(user -> !ProjectUtil.isProjectParticipant(organizationService, user, project));
receiver.removeIf(user -> !ProjectUtil.isProjectParticipant(userAcl, user, project));
}
ctx.append(NotificationUtils.RECEIVERS, receiver);
ctx.append(NotificationUtils.MENTIONED, mentioned);
Expand All @@ -120,7 +120,7 @@ private NotificationContext buildContext(TaskDto task, CommentDto comment) {
}

private void dispatch(NotificationContext ctx, String... pluginId) {
List<NotificationCommand> commands = new ArrayList<NotificationCommand>(pluginId.length);
List<NotificationCommand> commands = new ArrayList<>(pluginId.length);
for (String p : pluginId) {
commands.add(ctx.makeCommand(PluginKey.key(p)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.exoplatform.container.RootContainer;
import org.exoplatform.container.component.RequestLifeCycle;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.portal.config.UserPortalConfigService;
import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.spi.SpaceService;
import org.exoplatform.task.dto.ProjectDto;
Expand Down Expand Up @@ -107,17 +107,16 @@ public NotificationInfo makeNotification(NotificationContext ctx) {

private ExoContainer getContainer() {
String containerName = PortalContainer.getCurrentPortalContainerName();
ExoContainer container = RootContainer.getInstance().getPortalContainer(containerName);
return container;
return RootContainer.getInstance().getPortalContainer(containerName);
}

private String buildProjectUrl(ProjectDto project, ExoContainer container, WebAppController controller) {
return CommonsUtils.getCurrentDomain() + ProjectUtil.buildProjectURL(project, CommonsUtils.getCurrentSite(), container, controller.getRouter());
}

protected Set<String> getReceiver(TaskDto task, NotificationContext ctx) {
OrganizationService organizationService = CommonsUtils.getOrganizationService();
Set<String> receivers = new HashSet<String>();
UserACL userAcl = CommonsUtils.getService(UserACL.class);
Set<String> receivers = new HashSet<>();
if (task.getAssignee() != null && !task.getAssignee().isEmpty()) {
receivers.add(task.getAssignee());
}
Expand All @@ -135,7 +134,7 @@ protected Set<String> getReceiver(TaskDto task, NotificationContext ctx) {
}
if (task.getStatus() != null && task.getStatus().getProject() != null) {
ProjectDto project = task.getStatus().getProject();
receivers.removeIf(user -> !ProjectUtil.isProjectParticipant(organizationService, user, project));
receivers.removeIf(user -> !ProjectUtil.isProjectParticipant(userAcl, user, project));
}
return receivers;
}
Expand Down
26 changes: 8 additions & 18 deletions services/src/main/java/org/exoplatform/task/util/ProjectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
Expand All @@ -35,17 +34,15 @@
import java.util.ResourceBundle;
import java.util.Set;

import org.exoplatform.commons.utils.CommonsUtils;
import org.gatein.common.text.EntityEncoder;

import org.exoplatform.commons.utils.HTMLEntityEncoder;
import org.exoplatform.commons.utils.ListAccess;
import org.exoplatform.container.ExoContainer;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.organization.Membership;
import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.services.organization.User;
import org.exoplatform.services.security.ConversationState;
import org.exoplatform.services.security.Identity;
import org.exoplatform.services.security.MembershipEntry;
Expand All @@ -58,7 +55,6 @@
import org.exoplatform.task.exception.ParameterEntityException;
import org.exoplatform.task.service.ProjectService;
import org.exoplatform.web.controller.router.Router;
import org.gatein.common.text.EntityEncoder;

public final class ProjectUtil {
private static final Log LOG = ExoLogger.getExoLogger(ProjectUtil.class);
Expand Down Expand Up @@ -675,27 +671,21 @@ private static ProjectService getProjectService() {
return container.getComponentInstanceOfType(ProjectService.class);
}

public static boolean isProjectParticipant(OrganizationService organizationService,
String userName,
ProjectDto project) {
Collection<Membership> memberships;
try {
memberships = organizationService.getMembershipHandler().findMembershipsByUser(userName);
} catch (Exception e) {
LOG.error("Error while getting user memberships", e);
return false;
}
public static boolean isProjectParticipant(UserACL userAcl,
String userName,
ProjectDto project) {
if (project.getParticipator() == null) {
return false;
}
if (project.getParticipator().contains(userName)) {
return true;
} else {
Collection<MembershipEntry> memberships = userAcl.getUserIdentity(userName).getMemberships();
for (String per : project.getParticipator()) {
MembershipEntry entry = MembershipEntry.parse(per);
if (entry != null) {
boolean isParticipant = memberships.stream()
.map(Membership::getGroupId)
.map(MembershipEntry::getGroup)
.anyMatch(groupId -> groupId.equals(entry.getGroup()));
if (isParticipant) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.exoplatform.commons.notification.impl.NotificationContextImpl;
import org.exoplatform.commons.utils.CommonsUtils;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.services.organization.OrganizationService;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.task.dto.ProjectDto;
import org.exoplatform.task.dto.StatusDto;
import org.exoplatform.task.dto.TaskDto;
Expand All @@ -47,7 +47,7 @@
public class AbstractNotificationPluginTest {

@Mock
private OrganizationService organizationService;
private UserACL userACL;

public class DummyNotificationPlugin extends AbstractNotificationPlugin {

Expand All @@ -74,8 +74,7 @@ public boolean isValid(NotificationContext notificationContext) {
public void setUp() throws Exception {
commonsUtils = mockStatic(CommonsUtils.class);
projectUtil = mockStatic(ProjectUtil.class);

commonsUtils.when(() -> CommonsUtils.getOrganizationService()).thenReturn(organizationService);
commonsUtils.when(() -> CommonsUtils.getService(UserACL.class)).thenReturn(userACL);
}

@After
Expand All @@ -96,7 +95,7 @@ public void shouldReturnAssigneeInReceivers() throws Exception {
AbstractNotificationPlugin notificationPlugin = new DummyNotificationPlugin(new InitParams());

// When
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService,"user1", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL,"user1", projectDto)).thenReturn(true);
Set<String> receivers = notificationPlugin.getReceiver(task, null);

// Then
Expand All @@ -118,8 +117,8 @@ public void shouldReturnCoworkersInReceivers() throws Exception {
AbstractNotificationPlugin notificationPlugin = new DummyNotificationPlugin(new InitParams());

// When
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService,"user1", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService,"user2", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL,"user1", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL,"user2", projectDto)).thenReturn(true);

Set<String> receivers = notificationPlugin.getReceiver(task, null);

Expand All @@ -143,8 +142,8 @@ public void shouldReturnWatchersInReceivers() throws Exception {
AbstractNotificationPlugin notificationPlugin = new DummyNotificationPlugin(new InitParams());

// When
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService,"user1", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService,"user2", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL,"user1", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL,"user2", projectDto)).thenReturn(true);
Set<String> receivers = notificationPlugin.getReceiver(task, null);

// Then
Expand All @@ -171,9 +170,9 @@ public void shouldReturnCreatorInReceivers() throws Exception {
AbstractNotificationPlugin notificationPlugin = new DummyNotificationPlugin(new InitParams());

// When
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService,"user1", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService,"user2", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService,"user3", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL,"user1", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL,"user2", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL,"user3", projectDto)).thenReturn(true);

Set<String> receivers = notificationPlugin.getReceiver(task, ctx);

Expand Down Expand Up @@ -201,8 +200,8 @@ public void shouldNotReturnUserInReceiversIfNotProjectParticipant() throws Excep
AbstractNotificationPlugin notificationPlugin = new DummyNotificationPlugin(new InitParams());

// When
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService, "user1", projectDto)).thenReturn(false);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(organizationService, "user2", projectDto)).thenReturn(true);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL, "user1", projectDto)).thenReturn(false);
projectUtil.when(() -> ProjectUtil.isProjectParticipant(userACL, "user2", projectDto)).thenReturn(true);

Set<String> receivers = notificationPlugin.getReceiver(task, ctx);

Expand Down