Skip to content

Commit 055cfd4

Browse files
committed
fix: Review permissions - EXO-79156
This change will review the process permissions depending on users roles.
1 parent 7fb5c2b commit 055cfd4

File tree

14 files changed

+875
-393
lines changed

14 files changed

+875
-393
lines changed

processes-api/src/main/java/org/exoplatform/processes/service/ProcessesService.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ List<WorkFlow> getWorkFlows(ProcessesFilter filter,
5050
int countWorkFlows(ProcessesFilter filter,
5151
long userIdentityId) throws IllegalAccessException;
5252

53+
WorkFlow getWorkFlow(long id,
54+
long userIdentityId) throws IllegalAccessException;
55+
5356
WorkFlow getWorkFlow(long id) throws IllegalAccessException;
5457

5558
WorkFlow createWorkFlow(WorkFlow workFlow, long userId) throws IllegalAccessException;
@@ -69,6 +72,8 @@ WorkFlow updateWorkFlow(WorkFlow workFlow,
6972
*/
7073
List<Work> getWorks(long userIdentityId, WorkFilter workFilter, int offset, int limit) throws Exception;
7174

75+
WorkFlow getWorkFlowByProjectId(long projectId, long userId) throws IllegalAccessException;
76+
7277
WorkFlow getWorkFlowByProjectId(long projectId);
7378

7479
/**
@@ -88,32 +93,38 @@ Work updateWork(Work work, long userId) throws IllegalArgumentException,
8893
* Delete a workflow by its given Id.
8994
*
9095
* @param workflowId : workflow id
96+
* @param userId user id
9197
*/
92-
void deleteWorkflowById(Long workflowId);
98+
void deleteWorkflowById(Long workflowId, long userId) throws IllegalAccessException, ObjectNotFoundException;
9399

94100
/**
95101
* @param projectId: Tasks project id
96102
* @param isCompleted: filter by completed and uncompleted tasks
103+
* @param userId user id
97104
* @return Filtered tasks count
98105
* @throws Exception
99106
*/
100-
int countWorksByWorkflow(Long projectId, Boolean isCompleted) throws Exception;
107+
int countWorksByWorkflow(Long projectId, long userId, Boolean isCompleted) throws Exception;
101108

102109
/**
103110
* Delete a work by its given id.
104111
*
105112
* @param workId: Work id
113+
* @param userId user identity ID
106114
*/
107-
void deleteWorkById(Long workId);
115+
void deleteWorkById(Long workId, long userId) throws ObjectNotFoundException, IllegalAccessException;
108116

109117
/**
110118
* update the completed property of the task of a work to completed or uncompleted
111119
*
112120
* @param workId work id
121+
* @param userId user identity ID
113122
* @param completed work completed property, can be true or false
123+
* @throws ObjectNotFoundException
124+
* @throws IllegalAccessException
114125
* @return {@link Work}
115126
*/
116-
Work updateWorkCompleted(Long workId, boolean completed);
127+
Work updateWorkCompleted(Long workId, long userId, boolean completed) throws ObjectNotFoundException, IllegalAccessException;
117128

118129
/**
119130
* Creates a work draft
@@ -122,8 +133,9 @@ Work updateWork(Work work, long userId) throws IllegalArgumentException,
122133
* @param userId user identity
123134
* @return {@link Work}
124135
* @throws IllegalArgumentException
136+
125137
*/
126-
Work createWorkDraft(Work work, long userId) throws IllegalArgumentException;
138+
Work createWorkDraft(Work work, long userId) throws IllegalArgumentException, IllegalAccessException;
127139

128140
/**
129141
* Updates a work draft
@@ -134,7 +146,7 @@ Work updateWork(Work work, long userId) throws IllegalArgumentException,
134146
* @throws IllegalArgumentException
135147
* @throws ObjectNotFoundException
136148
*/
137-
Work updateWorkDraft(Work work, long userId) throws IllegalArgumentException, ObjectNotFoundException;
149+
Work updateWorkDraft(Work work, long userId) throws IllegalArgumentException, ObjectNotFoundException, IllegalAccessException;
138150

139151
/**
140152
* Retrieves a list of accessible WorkDraft, for a selected user
@@ -150,9 +162,10 @@ Work updateWork(Work work, long userId) throws IllegalArgumentException,
150162
/**
151163
* Deletes a work draft by its given id
152164
*
153-
* @param id Work draft id
165+
* @param userId user identity Id
166+
* @param draftId Work draft id
154167
*/
155-
void deleteWorkDraftById(Long id);
168+
void deleteWorkDraftById(Long draftId, long userId) throws IllegalAccessException, ObjectNotFoundException;
156169

157170
/**
158171
* Retrieves the list of available statuses in all workflows
@@ -164,21 +177,37 @@ Work updateWork(Work work, long userId) throws IllegalArgumentException,
164177
/**
165178
* Retrieves a Work by its given id
166179
*
167-
* @param userIdentityId user identity id
180+
* @param userId user identity id
168181
* @param workId Work id
169182
* @return {@link Work}
170183
*/
171-
Work getWorkById(long userIdentityId, Long workId);
184+
Work getWorkById(long userId, Long workId) throws IllegalAccessException;
172185

173186
/**
174187
* Retrieves an illustration image by its given id
175188
*
176189
* @param illustrationId illustration file id
190+
* @param userId user id
177191
* @return {@link IllustrativeAttachment}
178192
* @throws FileStorageException
179193
* @throws ObjectNotFoundException
180194
*/
181-
IllustrativeAttachment getIllustrationImageById(Long illustrationId) throws FileStorageException,
195+
IllustrativeAttachment getIllustrationImageById(Long illustrationId,
196+
long userId) throws FileStorageException,
182197
ObjectNotFoundException,
183198
IOException;
199+
200+
boolean canAccess(WorkFlow workFlow, org.exoplatform.services.security.Identity identity);
201+
202+
boolean canAdd(org.exoplatform.services.security.Identity identity);
203+
204+
boolean canEdit(WorkFlow workFlow, org.exoplatform.services.security.Identity identity);
205+
206+
boolean canDelete(WorkFlow workFlow, org.exoplatform.services.security.Identity identity);
207+
208+
boolean canAddRequest(WorkFlow workFlow, org.exoplatform.services.security.Identity identity);
209+
210+
boolean canEditRequest(WorkFlow workFlow, org.exoplatform.services.security.Identity identity);
211+
212+
boolean canDeleteRequest(WorkFlow workFlow, org.exoplatform.services.security.Identity identity);
184213
}

processes-api/src/main/java/org/exoplatform/processes/storage/ProcessesStorage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public interface ProcessesStorage {
6464

6565
WorkFlow getWorkFlowByProjectId(long projectId);
6666

67-
WorkFlow saveWorkFlow(WorkFlow workFlow, long userId) throws IllegalArgumentException;
67+
WorkFlow saveWorkFlow(WorkFlow workFlow, Identity identity) throws IllegalArgumentException;
6868

6969
/**
7070
* Retrieves list of filtered works
@@ -84,11 +84,11 @@ public interface ProcessesStorage {
8484
* Saving a work and deletes its related draft if it was created from draft
8585
*
8686
* @param work Work Object
87-
* @param userId user Id
87+
* @param identity user social identity
8888
* @return {@link Work}
8989
* @throws IllegalArgumentException
9090
*/
91-
Work saveWork(Work work, long userId) throws IllegalArgumentException;
91+
Work saveWork(Work work, Identity identity) throws IllegalArgumentException;
9292

9393
/**
9494
* Delete a workflow by its given Id.

processes-services/src/main/java/org/exoplatform/processes/Utils/EntityMapper.java

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
import org.exoplatform.processes.entity.WorkEntity;
1414
import org.exoplatform.processes.entity.WorkFlowEntity;
1515
import org.exoplatform.processes.model.*;
16+
import org.exoplatform.processes.service.ProcessesService;
1617
import org.exoplatform.services.log.ExoLogger;
1718
import org.exoplatform.services.log.Log;
1819
import org.exoplatform.services.organization.Group;
1920
import org.exoplatform.services.organization.GroupHandler;
2021
import org.exoplatform.services.organization.OrganizationService;
22+
import org.exoplatform.services.security.Identity;
2123
import org.exoplatform.social.core.space.model.Space;
2224
import org.exoplatform.social.core.space.spi.SpaceService;
2325
import org.exoplatform.task.dto.StatusDto;
@@ -26,16 +28,14 @@
2628
public class EntityMapper {
2729
private static final Log LOG = ExoLogger.getLogger(EntityMapper.class);
2830

29-
private static final String PROCESSES_GROUP = "/platform/processes";
30-
3131
private EntityMapper() {
3232
}
3333

34-
public static WorkFlow fromEntity(WorkFlowEntity workFlowEntity, List<String> memberships) {
34+
public static WorkFlow fromEntity(WorkFlowEntity workFlowEntity, Identity identity) {
3535
if (workFlowEntity == null) {
3636
return null;
3737
}
38-
return new WorkFlow(workFlowEntity.getId(),
38+
WorkFlow workFlow = new WorkFlow(workFlowEntity.getId(),
3939
workFlowEntity.getTitle(),
4040
workFlowEntity.getDescription(),
4141
workFlowEntity.getSummary(),
@@ -48,13 +48,15 @@ public static WorkFlow fromEntity(WorkFlowEntity workFlowEntity, List<String> me
4848
workFlowEntity.getModifiedDate(),
4949
workFlowEntity.getProjectId(),
5050
"",
51-
getACL(workFlowEntity, memberships),
51+
null,
5252
null,
5353
new IllustrativeAttachment(workFlowEntity.getIllustrationImageId()),
5454
workFlowEntity.getManager(),
5555
workFlowEntity.getParticipator(),
5656
false,
5757
fromGroupToIdentity(workFlowEntity.getManager()));
58+
workFlow.setAcl(getACL(workFlow, identity));
59+
return workFlow;
5860
}
5961

6062
static List<CreatorIdentityEntity> fromGroupToIdentity(Set<String> managers) {
@@ -90,43 +92,22 @@ static List<CreatorIdentityEntity> fromGroupToIdentity(Set<String> managers) {
9092
return identityEntities;
9193
}
9294

93-
public static ProcessPermission getACL(WorkFlowEntity workFlowEntity, List<String> memberships) {
95+
public static ProcessPermission getACL(WorkFlow workFlow, Identity identity) {
9496

95-
if (memberships == null)
97+
if (identity == null) {
9698
return new ProcessPermission(true, true, true, true);
97-
ProcessPermission permission = new ProcessPermission(false, false, false, false);
98-
for (String member : memberships) {
99-
for (String manager : workFlowEntity.getManager()) {
100-
if (member.contains(manager)) {
101-
permission.setCanAddRequest(true);
102-
break;
103-
}
104-
}
105-
for (String participator : workFlowEntity.getParticipator()) {
106-
if (member.equals(participator)) {
107-
permission.setCanAccess(true);
108-
permission.setCanEdit(true);
109-
break;
110-
}
111-
}
112-
if (member.contains(PROCESSES_GROUP)) {
113-
permission.setCanDelete(true);
114-
permission.setCanEdit(true);
115-
}
116-
if (permission.isCanAddRequest() && permission.isCanAccess() && permission.isCanDelete() && permission.isCanEdit()) {
117-
break;
118-
}
11999
}
120-
return permission;
100+
ProcessesService processesService = CommonsUtils.getService(ProcessesService.class);
101+
return new ProcessPermission(processesService.canAccess(workFlow, identity), processesService.canEdit(workFlow, identity), processesService.canDelete(workFlow, identity), processesService.canAddRequest(workFlow, identity));
121102
}
122103

123104
public static WorkFlow fromEntity(WorkFlowEntity workFlowEntity,
124105
IllustrativeAttachment illustrativeAttachment,
125-
List<String> memberships) {
106+
Identity identity) {
126107
if (workFlowEntity == null) {
127108
return null;
128109
}
129-
WorkFlow workFlow = fromEntity(workFlowEntity, memberships);
110+
WorkFlow workFlow = fromEntity(workFlowEntity, identity);
130111
if (illustrativeAttachment != null) {
131112
workFlow.setIllustrativeAttachment(illustrativeAttachment);
132113
}

processes-services/src/main/java/org/exoplatform/processes/Utils/ProcessesUtils.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package org.exoplatform.processes.Utils;
22

3+
import java.util.*;
4+
35
import org.exoplatform.commons.utils.CommonsUtils;
6+
import org.exoplatform.portal.config.UserACL;
7+
import org.exoplatform.processes.model.CreatorIdentityEntity;
8+
import org.exoplatform.processes.model.WorkFlow;
49
import org.exoplatform.services.listener.ListenerService;
510
import org.exoplatform.services.log.ExoLogger;
611
import org.exoplatform.services.log.Log;
@@ -16,8 +21,10 @@ public class ProcessesUtils {
1621

1722
private static final Log LOG = ExoLogger.getLogger(ProcessesUtils.class);
1823

24+
public static final String PROCESSES_GROUP = "/platform/processes";
25+
1926
public static String getUserNameByIdentityId(IdentityManager identityManager, long identityId) {
20-
Identity identity = identityManager.getIdentity(String.valueOf(identityId));
27+
Identity identity = identityManager.getIdentity(identityId);
2128
return identity != null ? identity.getRemoteId() : "";
2229
}
2330

@@ -38,6 +45,42 @@ public static Space getProjectParentSpace(Long projectId) {
3845
return null;
3946
}
4047

48+
public static boolean isPlatformAdmin(org.exoplatform.services.security.Identity identity) {
49+
UserACL userAcl = CommonsUtils.getService(UserACL.class);
50+
return userAcl.isAdministrator(identity);
51+
}
52+
53+
public static boolean isProcessAdmin(org.exoplatform.services.security.Identity identity) {
54+
UserACL userAcl = CommonsUtils.getService(UserACL.class);
55+
return userAcl.isMemberOf(identity, PROCESSES_GROUP);
56+
}
57+
58+
public static boolean isProcessManager(org.exoplatform.services.security.Identity identity, WorkFlow workFlow) {
59+
UserACL userAcl = CommonsUtils.getService(UserACL.class);
60+
return userAcl.isMemberOf(identity, Objects.requireNonNull(getProjectParentSpace(workFlow.getProjectId())).getGroupId());
61+
}
62+
63+
public static boolean isProcessParticipant(org.exoplatform.services.security.Identity identity, WorkFlow workFlow) {
64+
UserACL userAcl = CommonsUtils.getService(UserACL.class);
65+
return getGroupsFromRequestCreators(workFlow.getRequestsCreators()).stream().anyMatch(m -> userAcl.isMemberOf(identity, m));
66+
}
67+
68+
public static Set<String> getGroupsFromRequestCreators(List<CreatorIdentityEntity> requestsCreators) {
69+
SpaceService spaceService = CommonsUtils.getService(SpaceService.class);
70+
List<String> groups = new ArrayList<>();
71+
for (CreatorIdentityEntity id : requestsCreators) {
72+
if (id.getIdentity().getProviderId().equals("space")) {
73+
Space space = spaceService.getSpaceByPrettyName(id.getIdentity().getRemoteId());
74+
if (space != null) {
75+
groups.add(space.getGroupId());
76+
}
77+
} else {
78+
groups.add(id.getIdentity().getRemoteId());
79+
}
80+
}
81+
return new HashSet<>(groups);
82+
}
83+
4184
public static <S, D> void broadcast(ListenerService listenerService, String eventName, S source, D data) {
4285
try {
4386
listenerService.broadcast(eventName, source, data);

0 commit comments

Comments
 (0)