Skip to content

Commit

Permalink
feat: Allow Space Master to edit space public site - MEED-7809 - Meed…
Browse files Browse the repository at this point in the history
…s-io/MIPs#160 (#4189)

This change will update Layout `UserACL` management API to allow a Space
Master to edit and access public site.
  • Loading branch information
boubaker committed Nov 16, 2024
1 parent 973d908 commit 8cd840e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.mop.service.LayoutService;
import org.exoplatform.services.security.Identity;
import org.exoplatform.social.core.space.SpaceUtils;
import org.exoplatform.social.core.space.model.Space;
Expand All @@ -36,7 +37,10 @@
public class AuthorizationManager extends UserACL {

@Setter
private SpaceService spaceService;
private SpaceService spaceService;

@Setter
private LayoutService layoutService;

public AuthorizationManager(InitParams params) {
super(params);
Expand All @@ -52,25 +56,32 @@ public boolean hasEditPermission(Identity identity, String ownerType, String own
&& identity != null
&& getSpaceService().canManageSpaceLayout(space, identity.getUserId());
}
return isSpacesAdministrator(identity, ownerType, ownerId)
|| super.hasEditPermission(identity, ownerType, ownerId, expression);
return super.hasEditPermission(identity, ownerType, ownerId, expression)
|| isSpacesAdministrator(identity, ownerType, ownerId);
}

@Override
public boolean hasAccessPermission(Identity identity, String ownerType, String ownerId, Stream<String> expressionsStream) {
if (PortalConfig.GROUP_TEMPLATE.equalsIgnoreCase(ownerType)) {
return isAdministrator(identity);
} else {
return isSpacesAdministrator(identity, ownerType, ownerId)
|| super.hasAccessPermission(identity, ownerType, ownerId, expressionsStream);
return super.hasAccessPermission(identity, ownerType, ownerId, expressionsStream)
|| isSpacesAdministrator(identity, ownerType, ownerId);
}
}

private boolean isSpacesAdministrator(Identity identity, String ownerType, String groupId) {
if (isSpaceSite(ownerType, groupId)) {
return getSpaceService().isSuperManager(getSpaceService().getSpaceByGroupId(groupId), identity.getUserId());
private boolean isSpacesAdministrator(Identity identity, String ownerType, String ownerId) {
if (isAdministrator(identity)) {
return true;
} else if (isSpaceSite(ownerType, ownerId)) {
return getSpaceService().isSuperManager(getSpaceService().getSpaceByGroupId(ownerId), identity.getUserId());
} else if (isSpacePublicSite(ownerType, ownerId)) {
Space space = getSpaceService().getSpaceById(getSpaceIdFromPublicSite(ownerType, ownerId));
return space != null
&& identity != null
&& getSpaceService().canManageSpacePublicSite(space, identity.getUserId());
} else {
return isAdministrator(identity);
return false;
}
}

Expand All @@ -79,11 +90,28 @@ private boolean isSpaceSite(String ownerType, String ownerId) {
&& StringUtils.startsWith(ownerId, SpaceUtils.SPACE_GROUP_PREFIX);
}

private String getSpaceIdFromPublicSite(String ownerType, String ownerId) {
PortalConfig portalConfig = getLayoutService().getPortalConfig(ownerType, ownerId);
return portalConfig.getProperty(SpaceUtils.PUBLIC_SITE_SPACE_ID);
}

private boolean isSpacePublicSite(String ownerType, String ownerId) {
PortalConfig portalConfig = getLayoutService().getPortalConfig(ownerType, ownerId);
return portalConfig != null && portalConfig.getProperty(SpaceUtils.PUBLIC_SITE_SPACE_ID) != null;
}

private SpaceService getSpaceService() {
if (spaceService == null) {
spaceService = ExoContainerContext.getService(SpaceService.class);
}
return spaceService;
}

private LayoutService getLayoutService() {
if (layoutService == null) {
layoutService = ExoContainerContext.getService(LayoutService.class);
}
return layoutService;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
import org.exoplatform.portal.mop.page.PageContext;
import org.exoplatform.portal.mop.page.PageKey;
import org.exoplatform.portal.mop.page.PageState;
import org.exoplatform.portal.mop.service.LayoutService;
import org.exoplatform.services.security.Identity;
import org.exoplatform.services.security.MembershipEntry;
import org.exoplatform.social.core.space.SpaceUtils;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.spi.SpaceService;

Expand Down Expand Up @@ -81,6 +83,9 @@ public class AuthorizationManagerTest {
@Mock
SpaceService spaceService;

@Mock
LayoutService layoutService;

@Mock
UserACL userAcl;

Expand Down Expand Up @@ -122,6 +127,7 @@ public void setup() {

authorizationManager = new AuthorizationManager(params);
authorizationManager.setSpaceService(spaceService);
authorizationManager.setLayoutService(layoutService);
}

@Test
Expand Down Expand Up @@ -249,4 +255,22 @@ public void testHasAccessPermissionWhenSiteIsASpace() {
assertTrue(authorizationManager.hasAccessPermission(page, identity));
}

@Test
public void testCanEditWhenSiteIsSpacePublicSite() {
String spaceId = "2";

when(identity.getUserId()).thenReturn(TEST_USER);
when(portalConfig.getType()).thenReturn(PortalConfig.PORTAL_TYPE);
when(portalConfig.getName()).thenReturn("publicSite");
assertFalse(authorizationManager.hasEditPermission(portalConfig, identity));

when(layoutService.getPortalConfig(PortalConfig.PORTAL_TYPE, "publicSite")).thenReturn(portalConfig);
when(portalConfig.getProperty(SpaceUtils.PUBLIC_SITE_SPACE_ID)).thenReturn(spaceId);
when(spaceService.getSpaceById(spaceId)).thenReturn(space);
assertFalse(authorizationManager.hasEditPermission(portalConfig, identity));

when(spaceService.canManageSpacePublicSite(space, TEST_USER)).thenReturn(true);
assertTrue(authorizationManager.hasEditPermission(portalConfig, identity));
}

}

0 comments on commit 8cd840e

Please sign in to comment.