From e3b83bd65bdca61717f3c48f1c59ddb823079c16 Mon Sep 17 00:00:00 2001 From: rymsha Date: Mon, 20 Mar 2023 09:39:18 +0100 Subject: [PATCH] Pattern mapping does not work on project #10085 (cherry picked from commit 267f963e47748fee97a930712d67d679489423c8) --- .../xp/portal/impl/ContentResolver.java | 92 +++++++++-------- .../mapping/ControllerMappingsResolver.java | 14 +-- .../handler/mapping/MappingHandlerHelper.java | 17 ++-- .../xp/portal/impl/ContentResolverTest.java | 98 ++++++++++++++----- .../handler/mapping/MappingHandlerTest.java | 6 ++ .../handler/render/ComponentHandlerTest.java | 9 +- .../impl/handler/render/PageHandlerTest.java | 44 ++++++++- .../handler/render/RenderBaseHandlerTest.java | 9 +- .../handler/service/ServiceHandlerTest.java | 21 ++-- 9 files changed, 215 insertions(+), 95 deletions(-) diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/ContentResolver.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/ContentResolver.java index 44eb5ec8df8..5607b328494 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/ContentResolver.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/ContentResolver.java @@ -1,6 +1,5 @@ package com.enonic.xp.portal.impl; -import java.util.Optional; import java.util.concurrent.Callable; import com.enonic.xp.content.Content; @@ -14,6 +13,7 @@ import com.enonic.xp.portal.PortalRequest; import com.enonic.xp.portal.RenderMode; import com.enonic.xp.security.RoleKeys; +import com.enonic.xp.security.acl.Permission; import com.enonic.xp.security.auth.AuthenticationInfo; import com.enonic.xp.site.Site; @@ -42,69 +42,76 @@ public ContentResolverResult resolve( final PortalRequest request ) private ContentResolverResult resolveInNonEditMode( final ContentPath contentPath ) { - Content content = getContentByPath( contentPath ); - - final boolean contentExists = content != null || contentExistsByPath( contentPath ); + final Content content = callAsContentAdmin( () -> getContentByPath( contentPath ) ); final Site site = callAsContentAdmin( () -> this.contentService.findNearestSiteByPath( contentPath ) ); - return new ContentResolverResult( content, contentExists, site, siteRelativePath( site, contentPath ), contentPath.toString() ); + final String siteRelativePath = siteRelativePath( site, contentPath ); + return new ContentResolverResult( visibleContent( content ), content != null, site, siteRelativePath, contentPath.toString() ); } private ContentResolverResult resolveInEditMode( final ContentPath contentPath ) { final String contentPathString = contentPath.toString(); - ContentId contentId; + final ContentId contentId = tryConvertToContentId( contentPathString ); - try - { - contentId = ContentId.from( contentPathString.substring( 1 ) ); - } - catch ( Exception e ) - { - contentId = null; - } - - final Content content = - Optional.ofNullable( contentId ).map( this::getContentById ).orElseGet( () -> getContentByPath( contentPath ) ); + final Content contentById = contentId != null ? callAsContentAdmin( () -> getContentById( contentId ) ) : null; - final boolean contentExists = content != null || contentExistsById( contentId ) || contentExistsByPath( contentPath ); + final Content content = contentById != null ? contentById : callAsContentAdmin( () -> this.getContentByPath( contentPath ) ); - final Site site = content != null ? callAsContentAdmin( () -> this.contentService.getNearestSite( content.getId() ) ) : null; + final Site site = getNearestSite( content ); - return new ContentResolverResult( content, contentExists, site, - siteRelativePath( site, content == null ? null : content.getPath() ), contentPathString ); + final String siteRelativePath = siteRelativePath( site, content != null ? content.getPath() : contentPath ); + return new ContentResolverResult( visibleContent( content ), content != null, site, siteRelativePath, contentPathString ); } - private Content getContentById( final ContentId contentId ) + + private Site getNearestSite( final Content content ) { - try + if ( content != null ) { - final Content content = this.contentService.getById( contentId ); if ( ContentPath.ROOT.equals( content.getPath() ) ) { return null; } else { - return content; + return content.isSite() + ? (Site) content + : callAsContentAdmin( () -> this.contentService.getNearestSite( content.getId() ) ); } } - catch ( final Exception e ) + else { return null; } } - private Content getContentByPath( final ContentPath contentPath ) + private static ContentId tryConvertToContentId( final String contentPathString ) { - if ( contentPath.equals( ContentPath.ROOT ) ) + try + { + return ContentId.from( contentPathString.substring( 1 ) ); + } + catch ( Exception e ) { return null; } + } + + private Content visibleContent( final Content content ) + { + return content == null || ContentPath.ROOT.equals( content.getPath() ) || + !content.getPermissions().isAllowedFor( ContextAccessor.current().getAuthInfo().getPrincipals(), Permission.READ ) + ? null + : content; + } + + private Content getContentById( final ContentId contentId ) + { try { - return this.contentService.getByPath( contentPath ); + return this.contentService.getById( contentId ); } catch ( final ContentNotFoundException e ) { @@ -112,14 +119,16 @@ private Content getContentByPath( final ContentPath contentPath ) } } - private boolean contentExistsById( final ContentId contentId ) - { - return this.contentService.contentExists( contentId ); - } - - private boolean contentExistsByPath( final ContentPath contentPath ) + private Content getContentByPath( final ContentPath contentPath ) { - return !ContentPath.ROOT.equals( contentPath ) && this.contentService.contentExists( contentPath ); + try + { + return this.contentService.getByPath( contentPath ); + } + catch ( final ContentNotFoundException e ) + { + return null; + } } private static T callAsContentAdmin( final Callable callable ) @@ -133,14 +142,17 @@ private static T callAsContentAdmin( final Callable callable ) private static String siteRelativePath( final Site site, final ContentPath contentPath ) { - if ( site == null || contentPath == null) + if ( site == null ) { - return null; + return contentPath.toString(); } - if ( site.getPath().equals( contentPath ) ) + else if ( site.getPath().equals( contentPath ) ) { return "/"; } - return contentPath.toString().substring( site.getPath().toString().length() ); + else + { + return contentPath.toString().substring( site.getPath().toString().length() ); + } } } diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/mapping/ControllerMappingsResolver.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/mapping/ControllerMappingsResolver.java index 38b8c826285..894c9b3a0a1 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/mapping/ControllerMappingsResolver.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/mapping/ControllerMappingsResolver.java @@ -43,10 +43,13 @@ private static String urlEscape( final String value ) return UrlEscapers.urlFormParameterEscaper().escape( value ); } - private boolean matchesUrlPattern( final ControllerMappingDescriptor descriptor, final String relativePath, final String relativeUrl ) + private boolean matchesUrlPattern( final ControllerMappingDescriptor descriptor, final String relativePath, + final Multimap params ) { - final boolean patternWithQueryParameters = descriptor.getPattern().toString().contains( "\\?" ); - final boolean patternMatches = descriptor.getPattern().matcher( patternWithQueryParameters ? relativeUrl : relativePath ).matches(); + final boolean patternHasQueryParameters = descriptor.getPattern().toString().contains( "\\?" ); + final boolean patternMatches = descriptor.getPattern() + .matcher( patternHasQueryParameters ? relativePath + normalizedQueryParams( params ) : relativePath ) + .matches(); return descriptor.invertPattern() != patternMatches; } @@ -105,10 +108,7 @@ private boolean filterDescriptor( final ControllerMappingDescriptor controllerMa { if ( matchesContent( controllerMappingDescriptor, content ) ) { - final String contentPath = siteRelativePath != null ? siteRelativePath : content.getPath().toString(); - final String contentUrl = contentPath + normalizedQueryParams( params ); - - return matchesUrlPattern( controllerMappingDescriptor, contentPath, contentUrl ); + return matchesUrlPattern( controllerMappingDescriptor, siteRelativePath, params ); } } } diff --git a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/mapping/MappingHandlerHelper.java b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/mapping/MappingHandlerHelper.java index 7bcb1892c9d..694a969d103 100644 --- a/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/mapping/MappingHandlerHelper.java +++ b/modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/handler/mapping/MappingHandlerHelper.java @@ -103,26 +103,25 @@ public WebResponse handle( final WebRequest webRequest, final WebResponse webRes final Content content = resolvedContent.getContent(); - final SiteConfigs.Builder siteConfigs = SiteConfigs.create(); + final SiteConfigs siteConfigs; if ( site != null ) { - site.getSiteConfigs().forEach( siteConfigs::add ); + siteConfigs = site.getSiteConfigs(); } - else if ( content != null ) + else { - Optional.ofNullable( ProjectName.from( ( request.getRepositoryId() ) ) ) - .map( projectName -> callAsAdmin( () -> projectService.get( projectName ) ) ) - .map( Project::getSiteConfigs ) - .ifPresent( configs -> configs.forEach( siteConfigs::add ) ); + final Project project = callAsAdmin( () -> projectService.get( ProjectName.from( request.getRepositoryId() ) ) ); + siteConfigs = Optional.ofNullable( project ).map( Project::getSiteConfigs ).orElse( SiteConfigs.empty() ); } - else + + if ( siteConfigs.isEmpty() ) { return webHandlerChain.handle( webRequest, webResponse ); } final Optional resolve = - controllerMappingsResolver.resolve( resolvedContent.getSiteRelativePath(), request.getParams(), content, siteConfigs.build(), + controllerMappingsResolver.resolve( resolvedContent.getSiteRelativePath(), request.getParams(), content, siteConfigs, getServiceType( request ) ); if ( resolve.isPresent() ) diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/ContentResolverTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/ContentResolverTest.java index d5cb6b82381..16f3d26445b 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/ContentResolverTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/ContentResolverTest.java @@ -18,16 +18,21 @@ import com.enonic.xp.portal.RenderMode; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.security.PrincipalKey; +import com.enonic.xp.security.RoleKeys; +import com.enonic.xp.security.acl.AccessControlEntry; +import com.enonic.xp.security.acl.AccessControlList; +import com.enonic.xp.security.acl.Permission; import com.enonic.xp.site.Site; import com.enonic.xp.site.SiteConfigs; import com.enonic.xp.web.HttpStatus; import com.enonic.xp.web.WebException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @@ -63,10 +68,9 @@ void resolve_self_in_edit_mode() final PortalRequest request = new PortalRequest(); request.setMode( RenderMode.EDIT ); - request.setContentPath( ContentPath.from( "/c8da0c10-0002-4b68-b407-87412f3e45c9" ) ); + request.setContentPath( ContentPath.from( "/site0c10-0002-4b68-b407-87412f3e45c9" ) ); - when( this.contentService.getById( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c9" ) ) ).thenReturn( site ); - when( this.contentService.getNearestSite( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c9" ) ) ).thenReturn( site ); + when( this.contentService.getById( site.getId() ) ).thenReturn( site ); final ContentResolverResult result = new ContentResolver( contentService ).resolve( request ); @@ -93,7 +97,7 @@ void resolve_not_found_edit_mode() assertNull( result.getContent() ); assertNull( result.getNearestSite() ); - assertNull( result.getSiteRelativePath() ); + assertEquals( "/c8da0c10-0002-4b68-b407-87412f3e45c8", result.getSiteRelativePath() ); } @Test @@ -104,11 +108,11 @@ void resolve_found_by_path_edit_mode() final PortalRequest request = new PortalRequest(); request.setMode( RenderMode.EDIT ); - request.setContentPath( ContentPath.from( "/c8da0c10-0002-4b68-b407-87412f3e45c8" ) ); + request.setContentPath( ContentPath.from( "/some-page" ) ); - when( this.contentService.getById( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c8" ) ) ).thenThrow( - new ContentNotFoundException( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c8" ), null ) ); - when( this.contentService.getByPath( ContentPath.from( "/c8da0c10-0002-4b68-b407-87412f3e45c8" ) ) ).thenReturn( content ); + when( this.contentService.getById( ContentId.from( "some-page" ) ) ).thenThrow( + new ContentNotFoundException( ContentId.from( "some-page" ), null ) ); + when( this.contentService.getByPath( ContentPath.from( "/some-page" ) ) ).thenReturn( content ); when( this.contentService.getNearestSite( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c8" ) ) ).thenReturn( site ); @@ -124,18 +128,17 @@ void resolve_root_edit_mode() { final PortalRequest request = new PortalRequest(); request.setMode( RenderMode.EDIT ); - request.setContentPath( ContentPath.from( "/c8da0c10-0002-4b68-b407-87412f3e45c8" ) ); + request.setContentPath( ContentPath.from( "/root0c10-0002-4b68-b407-87412f3e45c8" ) ); - final Content rootContent = mock( Content.class ); - when( rootContent.getPath() ).thenReturn( ContentPath.ROOT ); + final Content rootContent = newRootContent(); - when( this.contentService.getById( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c8" ) ) ).thenReturn( rootContent ); + when( this.contentService.getById( ContentId.from( "root0c10-0002-4b68-b407-87412f3e45c8" ) ) ).thenReturn( rootContent ); final ContentResolverResult result = new ContentResolver( contentService ).resolve( request ); assertNull( result.getContent() ); assertNull( result.getNearestSite() ); - assertNull( result.getSiteRelativePath() ); + assertEquals( "/", result.getSiteRelativePath() ); } @Test @@ -145,16 +148,16 @@ void resolve_no_site_edit_mode() final PortalRequest request = new PortalRequest(); request.setMode( RenderMode.EDIT ); - request.setContentPath( ContentPath.from( "/c8da0c10-0002-4b68-b407-87412f3e45c8" ) ); + request.setContentPath( ContentPath.from( "/site0c10-0002-4b68-b407-87412f3e45c9" ) ); - when( this.contentService.getById( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c8" ) ) ).thenReturn( content ); - when( this.contentService.getNearestSite( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c8" ) ) ).thenReturn( null ); + when( this.contentService.getById( ContentId.from( "site0c10-0002-4b68-b407-87412f3e45c9" ) ) ).thenReturn( content ); + when( this.contentService.getNearestSite( any() ) ).thenReturn( null ); final ContentResolverResult result = new ContentResolver( contentService ).resolve( request ); assertSame( content, result.getContent() ); assertNull( result.getNearestSite() ); - assertNull( result.getSiteRelativePath() ); + assertEquals( "/mysite/landing-page", result.getSiteRelativePath() ); } @Test @@ -227,7 +230,6 @@ void resolve_non_existing_in_live_mode() request.setContentPath( contentPath ); when( this.contentService.getByPath( contentPath ) ).thenThrow( new ContentNotFoundException( contentPath, null ) ); - when( this.contentService.contentExists( contentPath ) ).thenReturn( false ); when( this.contentService.findNearestSiteByPath( contentPath ) ).thenReturn( site ); final ContentResolverResult result = new ContentResolver( contentService ).resolve( request ); @@ -243,20 +245,20 @@ void resolve_non_existing_in_live_mode() void resolve_existing_but_needs_authentication_in_live_mode() { final Site site = newSite(); + final Content content = newPrivilegedContent(); final PortalRequest request = new PortalRequest(); - final ContentPath contentPath = ContentPath.from( "/mysite/landing-page/non-existing" ); + final ContentPath contentPath = ContentPath.from( "/mysite/privileged-page" ); request.setContentPath( contentPath ); - when( this.contentService.getByPath( contentPath ) ).thenThrow( new ContentNotFoundException( contentPath, null ) ); - when( this.contentService.contentExists( contentPath ) ).thenReturn( true ); + when( this.contentService.getByPath( contentPath ) ).thenReturn( content ); when( this.contentService.findNearestSiteByPath( contentPath ) ).thenReturn( site ); final ContentResolverResult result = new ContentResolver( contentService ).resolve( request ); assertNull( result.getContent() ); assertSame( site, result.getNearestSite() ); - assertEquals( "/landing-page/non-existing", result.getSiteRelativePath() ); + assertEquals( "/privileged-page", result.getSiteRelativePath() ); final WebException e = assertThrows( WebException.class, result::getContentOrElseThrow ); assertEquals( HttpStatus.UNAUTHORIZED, e.getStatus() ); } @@ -276,7 +278,7 @@ void resolve_no_site_in_live_mode() assertSame( content, result.getContent() ); assertNull( result.getNearestSite() ); - assertNull( result.getSiteRelativePath() ); + assertNotNull( result.getSiteRelativePath() ); } private Content newContent() @@ -292,6 +294,47 @@ private Content newContent() builder.creator( PrincipalKey.from( "user:system:admin" ) ); builder.createdTime( Instant.ofEpochSecond( 0 ) ); builder.data( new PropertyTree() ); + builder.permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) + .build() ); + return builder.build(); + } + + private Content newPrivilegedContent() + { + final Content.Builder builder = Content.create(); + builder.id( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c9" ) ); + builder.name( "privileged-page" ); + builder.displayName( "My Privileged Page" ); + builder.parentPath( ContentPath.from( "/mysite" ) ); + builder.type( ContentTypeName.from( ApplicationKey.from( "com.enonic.test.app" ), "landing-page" ) ); + builder.modifier( PrincipalKey.from( "user:system:admin" ) ); + builder.modifiedTime( Instant.ofEpochSecond( 0 ) ); + builder.creator( PrincipalKey.from( "user:system:admin" ) ); + builder.createdTime( Instant.ofEpochSecond( 0 ) ); + builder.data( new PropertyTree() ); + builder.permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.ADMIN ).build() ) + .build() ); + return builder.build(); + } + + + private Content newRootContent() + { + final Content.Builder builder = Content.create(); + builder.root(); + builder.id( ContentId.from( "root0c10-0002-4b68-b407-87412f3e45c8" ) ); + builder.displayName( "" ); + builder.type( ContentTypeName.folder() ); + builder.modifier( PrincipalKey.from( "user:system:admin" ) ); + builder.modifiedTime( Instant.ofEpochSecond( 0 ) ); + builder.creator( PrincipalKey.from( "user:system:admin" ) ); + builder.createdTime( Instant.ofEpochSecond( 0 ) ); + builder.data( new PropertyTree() ); + builder.permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) + .build() ); return builder.build(); } @@ -299,10 +342,13 @@ private Site newSite() { final Site.Builder site = Site.create(); - site.id( ContentId.from( "c8da0c10-0002-4b68-b407-87412f3e45c9" ) ); + site.id( ContentId.from( "site0c10-0002-4b68-b407-87412f3e45c9" ) ); site.siteConfigs( SiteConfigs.empty() ); site.name( "mysite" ); site.parentPath( ContentPath.ROOT ); + site.permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) + .build() ); return site.build(); } -} \ No newline at end of file +} diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/mapping/MappingHandlerTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/mapping/MappingHandlerTest.java index 2de60bcec2b..15eea012900 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/mapping/MappingHandlerTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/mapping/MappingHandlerTest.java @@ -35,6 +35,9 @@ import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.security.PrincipalKey; import com.enonic.xp.security.RoleKeys; +import com.enonic.xp.security.acl.AccessControlEntry; +import com.enonic.xp.security.acl.AccessControlList; +import com.enonic.xp.security.acl.Permission; import com.enonic.xp.site.Site; import com.enonic.xp.site.SiteConfig; import com.enonic.xp.site.SiteConfigs; @@ -363,6 +366,9 @@ private Content createPage( final String id, final String path, final String con .owner( PrincipalKey.from( "user:myStore:me" ) ) .displayName( "My Content" ) .modifier( PrincipalKey.from( "user:system:admin" ) ) + .permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) + .build() ) .type( ContentTypeName.from( contentTypeName ) ); if ( withPage ) diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerTest.java index 073f37844a1..4caa4e9b072 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/ComponentHandlerTest.java @@ -21,6 +21,10 @@ import com.enonic.xp.region.Region; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.security.PrincipalKey; +import com.enonic.xp.security.RoleKeys; +import com.enonic.xp.security.acl.AccessControlEntry; +import com.enonic.xp.security.acl.AccessControlList; +import com.enonic.xp.security.acl.Permission; import com.enonic.xp.site.Site; import com.enonic.xp.web.HttpMethod; import com.enonic.xp.web.HttpStatus; @@ -236,7 +240,10 @@ private Content createPageWithFragment( final String id, final String path, fina .owner( PrincipalKey.from( "user:myStore:me" ) ) .displayName( "My Content" ) .modifier( PrincipalKey.from( "user:system:admin" ) ) - .type( ContentTypeName.from( contentTypeName ) ); + .type( ContentTypeName.from( contentTypeName ) ) + .permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) + .build() ); if ( withPage ) { diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageHandlerTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageHandlerTest.java index bafaa1f72ed..b322fe37b59 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageHandlerTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/PageHandlerTest.java @@ -21,7 +21,11 @@ import com.enonic.xp.portal.url.PageUrlParams; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.security.PrincipalKey; +import com.enonic.xp.security.RoleKeys; import com.enonic.xp.security.User; +import com.enonic.xp.security.acl.AccessControlEntry; +import com.enonic.xp.security.acl.AccessControlList; +import com.enonic.xp.security.acl.Permission; import com.enonic.xp.security.auth.AuthenticationInfo; import com.enonic.xp.util.Reference; import com.enonic.xp.web.HttpMethod; @@ -132,8 +136,21 @@ public void getContentNotFound() public void getContentExistsButNeedsAuthentication() { final ContentPath path = ContentPath.from( "/site/somepath/content" ); - when( this.contentService.getByPath( path ) ).thenThrow( new ContentNotFoundException( path, Branch.from( "draft" ) ) ); - when( this.contentService.contentExists( path ) ).thenReturn( true ); + + final Content content = Content.create() + .id( ContentId.from( "id" ) ) + .path( path ) + .owner( PrincipalKey.from( "user:myStore:me" ) ) + .displayName( "My Content" ) + .modifier( PrincipalKey.from( "user:system:admin" ) ) + .type( ContentTypeName.shortcut() ) + .data( new PropertyTree() ) + .permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.ADMIN ).build() ) + .build() ) + .build(); + + when( this.contentService.getByPath( path ) ).thenReturn( content ); this.request.setContentPath( path ); final WebException e = @@ -149,8 +166,21 @@ public void getContentExistsButInsufficientRights() final Context authenticatedContext = ContextBuilder.from( ContextAccessor.current() ).authInfo( authenticationInfo ).build(); final ContentPath path = ContentPath.from( "/site/somepath/content" ); - when( this.contentService.getByPath( path ) ).thenThrow( new ContentNotFoundException( path, Branch.from( "draft" ) ) ); - when( this.contentService.contentExists( path ) ).thenReturn( true ); + + final Content content = Content.create() + .id( ContentId.from( "id" ) ) + .path( path ) + .owner( PrincipalKey.from( "user:myStore:me" ) ) + .displayName( "My Content" ) + .modifier( PrincipalKey.from( "user:system:admin" ) ) + .type( ContentTypeName.shortcut() ) + .data( new PropertyTree() ) + .permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.ADMIN ).build() ) + .build() ) + .build(); + + when( this.contentService.getByPath( path ) ).thenReturn( content ); this.request.setContentPath( path ); final WebException e = assertThrows( WebException.class, () -> authenticatedContext.callWith( @@ -230,6 +260,9 @@ public void getContentShortcut() .modifier( PrincipalKey.from( "user:system:admin" ) ) .type( ContentTypeName.shortcut() ) .data( rootDataSet ) + .permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) + .build() ) .build(); when( this.contentService.getByPath( content.getPath().asAbsolute() ) ).thenReturn( content ); @@ -267,6 +300,9 @@ public void getContentShortcutWithParams() .modifier( PrincipalKey.from( "user:system:admin" ) ) .type( ContentTypeName.shortcut() ) .data( rootDataSet ) + .permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) + .build() ) .build(); when( this.contentService.getByPath( content.getPath().asAbsolute() ) ).thenReturn( content ); diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/RenderBaseHandlerTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/RenderBaseHandlerTest.java index 151c5939d3b..7587b38e46b 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/RenderBaseHandlerTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/render/RenderBaseHandlerTest.java @@ -29,6 +29,10 @@ import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.schema.content.ContentTypeNames; import com.enonic.xp.security.PrincipalKey; +import com.enonic.xp.security.RoleKeys; +import com.enonic.xp.security.acl.AccessControlEntry; +import com.enonic.xp.security.acl.AccessControlList; +import com.enonic.xp.security.acl.Permission; import com.enonic.xp.site.Site; import com.enonic.xp.web.handler.BaseHandlerTest; import com.enonic.xp.xml.parser.XmlPageDescriptorParser; @@ -155,7 +159,10 @@ private Content createPage( final String id, final String path, final String con owner( PrincipalKey.from( "user:myStore:me" ) ). displayName( "My Content" ). modifier( PrincipalKey.from( "user:system:admin" ) ). - type( ContentTypeName.from( contentTypeName ) ); + type( ContentTypeName.from( contentTypeName ) ) + .permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) + .build() ); if ( withPage ) { diff --git a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/service/ServiceHandlerTest.java b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/service/ServiceHandlerTest.java index 572c9282abf..bc04723a749 100644 --- a/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/service/ServiceHandlerTest.java +++ b/modules/portal/portal-impl/src/test/java/com/enonic/xp/portal/impl/handler/service/ServiceHandlerTest.java @@ -28,6 +28,10 @@ import com.enonic.xp.resource.ResourceService; import com.enonic.xp.schema.content.ContentTypeName; import com.enonic.xp.security.PrincipalKey; +import com.enonic.xp.security.RoleKeys; +import com.enonic.xp.security.acl.AccessControlEntry; +import com.enonic.xp.security.acl.AccessControlList; +import com.enonic.xp.security.acl.Permission; import com.enonic.xp.service.ServiceDescriptor; import com.enonic.xp.service.ServiceDescriptorService; import com.enonic.xp.site.Site; @@ -279,13 +283,16 @@ private Content createPage( final String id, final String path, final String con PropertyTree rootDataSet = new PropertyTree(); rootDataSet.addString( "property1", "value1" ); - final Content.Builder content = Content.create(). - id( ContentId.from( id ) ). - path( ContentPath.from( path ) ). - owner( PrincipalKey.from( "user:myStore:me" ) ). - displayName( "My Content" ). - modifier( PrincipalKey.from( "user:system:admin" ) ). - type( ContentTypeName.from( contentTypeName ) ); + final Content.Builder content = Content.create() + .id( ContentId.from( id ) ) + .path( ContentPath.from( path ) ) + .owner( PrincipalKey.from( "user:myStore:me" ) ) + .displayName( "My Content" ) + .modifier( PrincipalKey.from( "user:system:admin" ) ) + .type( ContentTypeName.from( contentTypeName ) ) + .permissions( AccessControlList.create() + .add( AccessControlEntry.create().allow( Permission.READ ).principal( RoleKeys.EVERYONE ).build() ) + .build() ); if ( withPage ) {