Skip to content

Commit fe21c70

Browse files
authored
feat: Retrieve Memberships from User ACL instead of OrganizationService - MEED-10149 - Meeds-io/MIPs#240 (#471)
This change will centralize the users memberships retrieval to be made from UserACL Service rather than OrganizationService. This centralization will allow to optimize the Code enhancements and evolutivity.
1 parent 4714a12 commit fe21c70

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

processes-services/src/main/java/org/exoplatform/processes/listener/RequestCommentNotificationListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.exoplatform.commons.api.notification.model.PluginKey;
2121
import org.exoplatform.commons.notification.impl.NotificationContextImpl;
2222
import org.exoplatform.commons.utils.CommonsUtils;
23+
import org.exoplatform.portal.config.UserACL;
2324
import org.exoplatform.portal.config.UserPortalConfigService;
2425
import org.exoplatform.processes.model.WorkFlow;
2526
import org.exoplatform.processes.notification.plugin.RequestCommentPlugin;
@@ -37,8 +38,8 @@
3738
public class RequestCommentNotificationListener extends TaskCommentNotificationListener {
3839
private ProcessesService processesService;
3940

40-
public RequestCommentNotificationListener(OrganizationService organizationService, ProcessesService processesService) {
41-
super(organizationService);
41+
public RequestCommentNotificationListener(UserACL userACL, ProcessesService processesService) {
42+
super(userACL);
4243
this.processesService = processesService;
4344
}
4445

processes-services/src/main/java/org/exoplatform/processes/storage/ProcessesStorageImpl.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
import java.util.*;
88
import java.util.stream.Collectors;
99

10+
import org.apache.commons.collections.CollectionUtils;
1011
import org.apache.commons.lang3.StringUtils;
1112

1213
import org.exoplatform.commons.exception.ObjectNotFoundException;
1314
import org.exoplatform.commons.file.model.FileInfo;
1415
import org.exoplatform.commons.file.model.FileItem;
1516
import org.exoplatform.commons.file.services.FileService;
1617
import org.exoplatform.commons.file.services.FileStorageException;
18+
import org.exoplatform.portal.config.UserACL;
1719
import org.exoplatform.processes.Utils.EntityMapper;
1820
import org.exoplatform.processes.Utils.ProcessesUtils;
1921
import org.exoplatform.processes.dao.WorkDraftDAO;
@@ -64,7 +66,7 @@ public class ProcessesStorageImpl implements ProcessesStorage {
6466
private final ListenerService listenerService;
6567
private final ProcessesAttachmentService processesAttachmentService;
6668
private final FileService fileService;
67-
private final OrganizationService organizationService;
69+
private final UserACL userACL;
6870
private final String DATE_FORMAT = "yyyy/MM/dd";
6971
private final SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
7072

@@ -78,7 +80,7 @@ public ProcessesStorageImpl(WorkFlowDAO workFlowDAO,
7880
ListenerService listenerService,
7981
ProcessesAttachmentService processesAttachmentService,
8082
FileService fileService,
81-
OrganizationService organizationService) {
83+
UserACL userACL) {
8284
this.workFlowDAO = workFlowDAO;
8385
this.workDraftDAO = workDraftDAO;
8486
this.identityManager = identityManager;
@@ -89,7 +91,7 @@ public ProcessesStorageImpl(WorkFlowDAO workFlowDAO,
8991
this.listenerService = listenerService;
9092
this.processesAttachmentService = processesAttachmentService;
9193
this.fileService = fileService;
92-
this.organizationService = organizationService;
94+
this.userACL = userACL;
9395
}
9496

9597
@Override
@@ -134,7 +136,7 @@ public WorkFlow saveWorkFlow(WorkFlow workFlow, long userId) throws IllegalArgum
134136
if (workFlow == null) {
135137
throw new IllegalArgumentException("workflow argument is null");
136138
}
137-
Identity identity = identityManager.getIdentity(String.valueOf(userId));
139+
Identity identity = identityManager.getIdentity(userId);
138140
if (identity == null) {
139141
throw new IllegalArgumentException("identity is not exist");
140142
}
@@ -299,7 +301,7 @@ public List<Work> getWorks(long userIdentityId, WorkFilter workFilter, int offse
299301

300302
@Override
301303
public Work getWorkById(long userIdentityId, long workId) {
302-
Identity identity = identityManager.getIdentity(String.valueOf(userIdentityId));
304+
Identity identity = identityManager.getIdentity(userIdentityId);
303305
if (identity == null) {
304306
throw new IllegalArgumentException("identity is not exist");
305307
}
@@ -389,7 +391,7 @@ public Work saveWork(Work work, long userId) throws IllegalArgumentException {
389391
if (work == null) {
390392
throw new IllegalArgumentException("work argument is null");
391393
}
392-
Identity identity = identityManager.getIdentity(String.valueOf(userId));
394+
Identity identity = identityManager.getIdentity(userId);
393395
if (identity == null) {
394396
throw new IllegalArgumentException("identity is not exist");
395397
}
@@ -511,7 +513,7 @@ public List<Work> findAllWorkDraftsByUser(WorkFilter workFilter, int offset, int
511513
*/
512514
@Override
513515
public Work saveWorkDraft(Work work, long userId) {
514-
Identity identity = identityManager.getIdentity(String.valueOf(userId));
516+
Identity identity = identityManager.getIdentity(userId);
515517
if (identity == null) {
516518
throw new IllegalArgumentException("identity is not exist");
517519
}
@@ -577,20 +579,18 @@ public List<WorkFlow> findWorkFlows(ProcessesFilter processesFilter, long userId
577579
List<String> memberships = new ArrayList<>();
578580
boolean isMemberProcessesGroup = false;
579581
if (userIdentityId > 0) {
580-
Identity identity = identityManager.getIdentity(String.valueOf(userIdentityId));
582+
Identity identity = identityManager.getIdentity(userIdentityId);
581583
if (identity != null) {
582584
userName = identity.getRemoteId();
583585
memberships.add(userName);
584586
try {
585-
Collection<Membership> ms = organizationService.getMembershipHandler().findMembershipsByUser(userName);
586-
if (ms != null) {
587-
for (Membership membership : ms) {
588-
if (membership.getGroupId().equals(PROCESSES_GROUP)) {
589-
isMemberProcessesGroup = true;
590-
}
591-
String membership_ = membership.getMembershipType() + ":" + membership.getGroupId();
592-
memberships.add(membership_);
593-
}
587+
org.exoplatform.services.security.Identity aclIdentity = userACL.getUserIdentity(userName);
588+
isMemberProcessesGroup = aclIdentity.isMemberOf(PROCESSES_GROUP);
589+
if (CollectionUtils.isNotEmpty(aclIdentity.getMemberships())) {
590+
memberships.addAll(aclIdentity.getMemberships()
591+
.stream()
592+
.map(m -> m.getMembershipType() + ":" + m.getGroup())
593+
.toList());
594594
}
595595
} catch (Exception e) {
596596
LOG.error("Error while getting the user memberships", e);

processes-services/src/test/java/org/exoplatform/processes/storage/ProcessesStorageImplTest.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import javax.ws.rs.ext.RuntimeDelegate;
2727

2828
import org.apache.commons.lang3.StringUtils;
29+
import org.exoplatform.portal.config.UserACL;
30+
import org.exoplatform.services.security.MembershipEntry;
2931
import org.junit.AfterClass;
3032
import org.junit.Assert;
3133
import org.junit.Before;
@@ -130,7 +132,7 @@ public class ProcessesStorageImplTest {
130132
private FileService fileService;
131133

132134
@Mock
133-
private OrganizationService organizationService;
135+
private UserACL userACL;
134136

135137
private ProcessesStorage processesStorage;
136138

@@ -158,7 +160,7 @@ public void setUp() throws Exception {
158160
listenerService,
159161
processesAttachmentService,
160162
fileService,
161-
organizationService);
163+
userACL);
162164

163165
}
164166

@@ -213,7 +215,7 @@ public void saveWorkflow() throws EntityNotFoundException {
213215
Throwable exception1 = assertThrows(IllegalArgumentException.class, () -> this.processesStorage.saveWorkFlow(null, 1l));
214216
assertEquals("workflow argument is null", exception1.getMessage());
215217

216-
when(identityManager.getIdentity("1")).thenReturn(null);
218+
when(identityManager.getIdentity(1)).thenReturn(null);
217219
Throwable exception2 = assertThrows(IllegalArgumentException.class, () -> this.processesStorage.saveWorkFlow(workFlow, 1l));
218220
assertEquals("identity is not exist", exception2.getMessage());
219221

@@ -222,7 +224,7 @@ public void saveWorkflow() throws EntityNotFoundException {
222224
when(workFlow.getProjectId()).thenReturn(0L);
223225
when(workFlow.getSpaceId()).thenReturn("1");
224226
when(projectService.getProject(workFlow.getProjectId())).thenReturn(projectDto);
225-
when(identityManager.getIdentity("1")).thenReturn(identity);
227+
when(identityManager.getIdentity(1)).thenReturn(identity);
226228
when(space.getGroupId()).thenReturn("/spaces/processes_space");
227229
when(space.getId()).thenReturn("2");
228230
when(spaceService.getSpaceByGroupId("/spaces/processes_space")).thenReturn(space);
@@ -319,12 +321,12 @@ public void saveWork() throws EntityNotFoundException, IllegalAccessException, O
319321
when(statusDto.getProject()).thenReturn(projectDto);
320322
Throwable exception1 = assertThrows(IllegalArgumentException.class, () -> this.processesStorage.saveWork(null, 1l));
321323
assertEquals("work argument is null", exception1.getMessage());
322-
when(identityManager.getIdentity("1")).thenReturn(null);
324+
when(identityManager.getIdentity(1)).thenReturn(null);
323325
Throwable exception2 = assertThrows(IllegalArgumentException.class, () -> this.processesStorage.saveWork(work, 1l));
324326
assertEquals("identity is not exist", exception2.getMessage());
325327
work.setId(0L);
326328
work.setProjectId(1L);
327-
when(identityManager.getIdentity("1")).thenReturn(identity);
329+
when(identityManager.getIdentity(1)).thenReturn(identity);
328330
when(projectService.getProject(work.getProjectId())).thenReturn(projectDto);
329331
ENTITY_MAPPER.when(() -> EntityMapper.workToTask(work)).thenReturn(taskDto);
330332
ENTITY_MAPPER.when(() -> EntityMapper.taskToWork(taskDto)).thenReturn(work);
@@ -385,11 +387,11 @@ public void saveWorkDraft() {
385387
workEntity.equals(workEntity);
386388
workEntity.toString();
387389
Identity identity = mock(Identity.class);
388-
when(identityManager.getIdentity("1")).thenReturn(null);
390+
when(identityManager.getIdentity(1)).thenReturn(null);
389391
Throwable exception1 = assertThrows(IllegalArgumentException.class, () -> this.processesStorage.saveWorkDraft(work, 1l));
390392
assertEquals("identity is not exist", exception1.getMessage());
391393
ENTITY_MAPPER.when(() -> EntityMapper.toEntity(work)).thenReturn(workEntity);
392-
when(identityManager.getIdentity("1")).thenReturn(identity);
394+
when(identityManager.getIdentity(1)).thenReturn(identity);
393395
work.setWorkFlow(workFlow);
394396
when(workDraftDAO.create(workEntity)).thenReturn(workEntity);
395397
processesStorage.saveWorkDraft(work, 1L);
@@ -420,10 +422,10 @@ public void getWorkById() throws Exception {
420422
List<TaskDto> list = new ArrayList<>();
421423
list.add(taskDto);
422424
when(identity.getRemoteId()).thenReturn("root");
423-
when(identityManager.getIdentity("1")).thenReturn(null);
425+
when(identityManager.getIdentity(1)).thenReturn(null);
424426
Throwable exception1 = assertThrows(IllegalArgumentException.class, () -> this.processesStorage.getWorkById(1L, 1L));
425427
assertEquals("identity is not exist", exception1.getMessage());
426-
when(identityManager.getIdentity("1")).thenReturn(identity);
428+
when(identityManager.getIdentity(1)).thenReturn(identity);
427429
when(taskService.findTasks(any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())).thenReturn(list);
428430
processesStorage.getWorkById(1L, 1L);
429431
EntityMapper.taskToWork(taskDto);
@@ -435,7 +437,6 @@ public void getWorkById() throws Exception {
435437

436438
@Test
437439
public void updateWorkCompleted() throws EntityNotFoundException {
438-
COMMONS_UTILS.when(() -> CommonsUtils.getService(OrganizationService.class)).thenReturn(organizationService);
439440
COMMONS_UTILS.when(() -> CommonsUtils.getService(SpaceService.class)).thenReturn(spaceService);
440441
TaskDto taskDto = new TaskDto();
441442
StatusDto statusDto = new StatusDto();
@@ -533,7 +534,7 @@ public void getWorks() throws Exception {
533534
currentProfile.setProperty(Profile.FULL_NAME, username);
534535
currentIdentity.setProfile(currentProfile);
535536

536-
when(identityManager.getIdentity((String.valueOf(currentOwnerId)))).thenReturn(currentIdentity);
537+
when(identityManager.getIdentity(currentOwnerId)).thenReturn(currentIdentity);
537538

538539
PROCESSES_UTILS.when(() -> ProcessesUtils.getUserNameByIdentityId(any(), anyLong())).thenCallRealMethod();
539540
String user = ProcessesUtils.getUserNameByIdentityId(identityManager, 1l);
@@ -598,7 +599,7 @@ public void getIllustrationImageById() throws Exception {
598599
}
599600

600601
@Test
601-
public void findWorkflow() throws Exception {
602+
public void testFindWorkflow() throws Exception {
602603
IllustrativeAttachment illustrativeAttachment = new IllustrativeAttachment(null,
603604
"image.png",
604605
"image/png",
@@ -623,9 +624,11 @@ public void findWorkflow() throws Exception {
623624
when(workFlow.getProjectId()).thenReturn(0L);
624625
when(workFlow.getSpaceId()).thenReturn("1");
625626

626-
when(identityManager.getIdentity("1")).thenReturn(identity);
627+
when(identityManager.getIdentity(1)).thenReturn(identity);
627628
when(identity.getRemoteId()).thenReturn("user");
628629
when(identity.getId()).thenReturn("1");
630+
org.exoplatform.services.security.Identity aclIdentity = mock(org.exoplatform.services.security.Identity.class);
631+
when(userACL.getUserIdentity("user")).thenReturn(aclIdentity);
629632
when(space.getGroupId()).thenReturn("/spaces/processes_space");
630633
when(spaceService.getSpaceByGroupId("/spaces/processes_space")).thenReturn(space);
631634
when(spaceService.getSpaceById("1")).thenReturn(space);
@@ -664,7 +667,6 @@ public void findWorkflow() throws Exception {
664667
when(workFlowDAO.findWorkFlows(filter, memberships, 0, 0)).thenReturn(workFlowEntities);
665668
when(workFlow.getIllustrativeAttachment()).thenReturn(illustrativeAttachment);
666669
this.processesStorage.saveWorkFlow(workFlow, 1L);
667-
when(organizationService.getMembershipHandler()).thenReturn(membershipHandler);
668670
assertEquals(null, this.processesStorage.getWorkFlowById(1));
669671

670672
Collection<Membership> memberships_ = new ArrayList();
@@ -688,8 +690,7 @@ public void findWorkflow() throws Exception {
688690

689691
PROCESSES_UTILS.when(() -> ProcessesUtils.getProjectParentSpace(workFlow.getProjectId())).thenReturn(space);
690692
ENTITY_MAPPER.when(() -> EntityMapper.fromEntity(newWorkFlowEntity1, null)).thenReturn(workFlow);
691-
692-
when(organizationService.getMembershipHandler().findMembershipsByUser(identity.getRemoteId())).thenReturn(memberships_);
693+
when(aclIdentity.getMemberships()).thenReturn(memberships_.stream().map(m -> new MembershipEntry(m.getGroupId(), m.getMembershipType())).toList());
693694
assertEquals(1, this.processesStorage.findWorkFlows(filter, Long.parseLong(identity.getId()), 0, 0).size());
694695

695696
MembershipImpl adminProcesses = new MembershipImpl();
@@ -701,7 +702,7 @@ public void findWorkflow() throws Exception {
701702
PROCESSES_UTILS.when(() -> ProcessesUtils.getProjectParentSpace(workFlow.getProjectId())).thenReturn(space);
702703
ENTITY_MAPPER.when(() -> EntityMapper.fromEntity(newWorkFlowEntity1, null)).thenReturn(workFlow);
703704

704-
when(organizationService.getMembershipHandler().findMembershipsByUser(identity.getRemoteId())).thenReturn(memberships_);
705+
when(aclIdentity.getMemberships()).thenReturn(memberships_.stream().map(m -> new MembershipEntry(m.getGroupId(), m.getMembershipType())).toList());
705706
assertEquals(0, this.processesStorage.findWorkFlows(filter, Long.parseLong(identity.getId()), 0, 0).size());
706707
}
707708

@@ -731,7 +732,7 @@ public void countWorkflow() {
731732
when(workFlow.getProjectId()).thenReturn(0L);
732733
when(workFlow.getSpaceId()).thenReturn("1");
733734

734-
when(identityManager.getIdentity("1")).thenReturn(identity);
735+
when(identityManager.getIdentity(1)).thenReturn(identity);
735736
when(space.getGroupId()).thenReturn("/spaces/processes_space");
736737
when(spaceService.getSpaceByGroupId("/spaces/processes_space")).thenReturn(space);
737738
when(spaceService.getSpaceById("1")).thenReturn(space);

0 commit comments

Comments
 (0)