Skip to content

Commit

Permalink
feat: Allow to add, edit and delete a Portlet instance - MEED-6946 - M…
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker authored and exo-swf committed May 31, 2024
1 parent eea33fd commit 48a744c
Show file tree
Hide file tree
Showing 27 changed files with 729 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class PortletInstanceCategoryRest {
description = "This retrieves portlet instance categorys")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Request fulfilled"), })
public List<PortletInstanceCategory> getPortletInstanceCategorys(HttpServletRequest request) {
return portletInstanceService.getPortletInstanceCategories(request.getLocale(), true);
return portletInstanceService.getPortletInstanceCategories(request.getRemoteUser(), request.getLocale(), true);
}

@GetMapping("{id}")
Expand All @@ -72,7 +72,13 @@ public PortletInstanceCategory getPortletInstanceCategory(
@Parameter(description = "Portlet instance category identifier")
@PathVariable("id")
long id) {
return portletInstanceService.getPortletInstanceCategory(id, request.getLocale(), true);
try {
return portletInstanceService.getPortletInstanceCategory(id, request.getRemoteUser(), request.getLocale(), true);
} catch (ObjectNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} catch (IllegalAccessException e) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
}
}

@PostMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public List<PortletInstance> getPortletInstances(
@Parameter(description = "Portlet instance category identifier")
@RequestParam(name = "categoryId", required = false, defaultValue = "0")
long categoryId) {
return portletInstanceService.getPortletInstances(categoryId, request.getLocale(), true);
return portletInstanceService.getPortletInstances(categoryId, request.getRemoteUser(), request.getLocale(), true);
}

@GetMapping("{id}")
Expand All @@ -76,7 +76,13 @@ public PortletInstance getPortletInstance(
@Parameter(description = "Portlet instance identifier")
@PathVariable("id")
long id) {
return portletInstanceService.getPortletInstance(id, request.getLocale(), true);
try {
return portletInstanceService.getPortletInstance(id, request.getRemoteUser(), request.getLocale(), true);
} catch (ObjectNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} catch (IllegalAccessException e) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
}
}

@PostMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
*/
package io.meeds.layout.service;

import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -45,7 +47,9 @@
@Service
public class PortletInstanceService {

private static final Log LOG = ExoLogger.getLogger(PortletInstanceService.class);
private static final List<String> EVERYONE_PERMISSIONS_LIST = Collections.singletonList("Everyone");

private static final Log LOG = ExoLogger.getLogger(PortletInstanceService.class);

@Autowired
private LayoutAclService layoutAclService;
Expand All @@ -66,44 +70,51 @@ public class PortletInstanceService {
private PortletInstanceStorage portletInstanceStorage;

public List<PortletInstance> getPortletInstances() {
return getPortletInstances(null, false);
}

public List<PortletInstance> getPortletInstances(boolean expand) {
return getPortletInstances(null, expand);
}

public List<PortletInstance> getPortletInstances(Locale locale,
boolean expand) {
return getPortletInstances(0, locale, expand);
return portletInstanceStorage.getPortletInstances();
}

public List<PortletInstance> getPortletInstances(long categoryId,
String username,
Locale locale,
boolean expand) {
List<PortletInstance> portletInstances = categoryId < 1 ? portletInstanceStorage.getPortletInstances() :
portletInstanceStorage.getPortletInstances(categoryId);
portletInstances = portletInstances.stream().filter(p -> this.hasPermission(p, username)).toList();
if (expand) {
portletInstances.forEach(portletInstance -> computePortletInstanceAttributes(locale, portletInstance));
portletInstances.stream()
.forEach(portletInstance -> computePortletInstanceAttributes(locale, portletInstance));
}
return portletInstances;
}

public List<PortletInstanceCategory> getPortletInstanceCategories() {
return getPortletInstanceCategories(null, false);
return portletInstanceCategoryStorage.getPortletInstanceCategories();
}

public List<PortletInstanceCategory> getPortletInstanceCategories(Locale locale, boolean expand) {
public List<PortletInstanceCategory> getPortletInstanceCategories(String username,
Locale locale,
boolean expand) {
List<PortletInstanceCategory> portletInstanceCategories = portletInstanceCategoryStorage.getPortletInstanceCategories();
portletInstanceCategories = portletInstanceCategories.stream().filter(c -> this.hasPermission(c, username)).toList();
if (expand && locale != null) {
portletInstanceCategories.forEach(portletInstance -> computePortletInstanceCategoryAttributes(locale, portletInstance));
portletInstanceCategories.stream()
.forEach(c -> computePortletInstanceCategoryAttributes(locale, c));
}
return portletInstanceCategories;
}

public PortletInstance getPortletInstance(long id, Locale locale, boolean expand) {
public PortletInstance getPortletInstance(long id,
String username,
Locale locale,
boolean expand) throws IllegalAccessException, ObjectNotFoundException {
PortletInstance portletInstance = portletInstanceStorage.getPortletInstance(id);
if (expand && portletInstance != null) {
if (portletInstance == null) {
throw new ObjectNotFoundException("Portlet instance not found");
}
if (!this.hasPermission(portletInstance, username)) {
throw new IllegalAccessException();
}
if (expand) {
computePortletInstanceAttributes(locale, portletInstance);
}
return portletInstance;
Expand All @@ -113,9 +124,19 @@ public PortletInstanceCategory getPortletInstanceCategory(long id) {
return portletInstanceCategoryStorage.getPortletInstanceCategory(id);
}

public PortletInstanceCategory getPortletInstanceCategory(long id, Locale locale, boolean expand) {
public PortletInstanceCategory getPortletInstanceCategory(long id,
String username,
Locale locale,
boolean expand) throws ObjectNotFoundException,
IllegalAccessException {
PortletInstanceCategory portletInstanceCategory = portletInstanceCategoryStorage.getPortletInstanceCategory(id);
if (expand && portletInstanceCategory != null) {
if (portletInstanceCategory == null) {
throw new ObjectNotFoundException("Portlet instance category not found");
}
if (!this.hasPermission(portletInstanceCategory, username)) {
throw new IllegalAccessException();
}
if (expand) {
computePortletInstanceCategoryAttributes(locale, portletInstanceCategory);
}
return portletInstanceCategory;
Expand Down Expand Up @@ -273,4 +294,18 @@ private String getLabel(String objectType, long objectId, String fieldName, Loca
}
}

private boolean hasPermission(PortletInstance portletInstance, String username) {
List<String> permissions = portletInstance.getPermissions();
return CollectionUtils.isEmpty(permissions)
|| permissions.equals(EVERYONE_PERMISSIONS_LIST)
|| (StringUtils.isNotBlank(username) && permissions.stream().anyMatch(p -> layoutAclService.isMemberOf(username, p)));
}

private boolean hasPermission(PortletInstanceCategory category, String username) {
List<String> permissions = category.getPermissions();
return CollectionUtils.isEmpty(permissions)
|| permissions.equals(EVERYONE_PERMISSIONS_LIST)
|| (StringUtils.isNotBlank(username) && permissions.stream().anyMatch(p -> layoutAclService.isMemberOf(username, p)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public static PortletInstanceCategory fromEntity(PortletInstanceCategoryEntity e
null,
entity.getIcon(),
entity.isSystem(),
entity.getPermissions());
entity.getPermissions()
== null ? Collections.emptyList() :
entity.getPermissions().stream().filter(StringUtils::isNotBlank).toList());
}

public static PortletInstance fromEntity(PortletInstanceEntity entity, PortletDescriptor portlet) {
Expand All @@ -57,7 +59,9 @@ public static PortletInstance fromEntity(PortletInstanceEntity entity, PortletDe
entity.getContentId(),
getPreferences(entity),
0l,
entity.getPermissions(),
entity.getPermissions()
== null ? Collections.emptyList() :
entity.getPermissions().stream().filter(StringUtils::isNotBlank).toList(),
portlet == null ? null : portlet.getSupportedModes(),
entity.isSystem(),
entity.isDisabled(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void getPortletInstanceCategorysAnonymously() throws Exception {
void getPortletInstanceCategorysWithUser() throws Exception {
ResultActions response = mockMvc.perform(get(REST_PATH).with(testSimpleUser()));
response.andExpect(status().isOk());
verify(portletInstanceService).getPortletInstanceCategories(any(), anyBoolean());
verify(portletInstanceService).getPortletInstanceCategories(any(), any(), anyBoolean());
}

@Test
Expand All @@ -135,7 +135,7 @@ void getPortletInstanceCategoryAnonymously() throws Exception {
void getPortletInstanceCategoryWithUser() throws Exception {
ResultActions response = mockMvc.perform(get(REST_PATH + "/1").with(testSimpleUser()));
response.andExpect(status().isOk());
verify(portletInstanceService).getPortletInstanceCategory(eq(1l), any(), eq(true));
verify(portletInstanceService).getPortletInstanceCategory(eq(1l), any(), any(), eq(true));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void getPortletInstancesAnonymously() throws Exception {
void getPortletInstancesWithUser() throws Exception {
ResultActions response = mockMvc.perform(get(REST_PATH).with(testSimpleUser()));
response.andExpect(status().isOk());
verify(portletInstanceService).getPortletInstances(anyLong(), any(), anyBoolean());
verify(portletInstanceService).getPortletInstances(anyLong(), any(), any(), anyBoolean());
}

@Test
Expand All @@ -136,7 +136,7 @@ void getPortletInstanceAnonymously() throws Exception {
void getPortletInstanceWithUser() throws Exception {
ResultActions response = mockMvc.perform(get(REST_PATH + "/1").with(testSimpleUser()));
response.andExpect(status().isOk());
verify(portletInstanceService).getPortletInstance(eq(1l), any(), eq(true));
verify(portletInstanceService).getPortletInstance(eq(1l), any(), any(), eq(true));
}

@Test
Expand Down
Loading

0 comments on commit 48a744c

Please sign in to comment.