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 5607b328494..0cba655ae3c 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 @@ -30,6 +30,11 @@ public ContentResolverResult resolve( final PortalRequest request ) { final ContentPath contentPath = request.getContentPath(); + if ( contentPath.isRoot() ) + { + return new ContentResolverResult( null, false, null, "/", contentPath.toString() ); + } + if ( request.getMode() == RenderMode.EDIT ) { return resolveInEditMode( contentPath ); @@ -44,7 +49,9 @@ private ContentResolverResult resolveInNonEditMode( final ContentPath contentPat { final Content content = callAsContentAdmin( () -> getContentByPath( contentPath ) ); - final Site site = callAsContentAdmin( () -> this.contentService.findNearestSiteByPath( contentPath ) ); + final Site site = content != null && content.isSite() + ? (Site) content + : callAsContentAdmin( () -> this.contentService.findNearestSiteByPath( contentPath ) ); final String siteRelativePath = siteRelativePath( site, contentPath ); return new ContentResolverResult( visibleContent( content ), content != null, site, siteRelativePath, contentPath.toString() ); @@ -60,31 +67,21 @@ private ContentResolverResult resolveInEditMode( final ContentPath contentPath ) final Content content = contentById != null ? contentById : callAsContentAdmin( () -> this.getContentByPath( contentPath ) ); - final Site site = getNearestSite( content ); - - final String siteRelativePath = siteRelativePath( site, content != null ? content.getPath() : contentPath ); - return new ContentResolverResult( visibleContent( content ), content != null, site, siteRelativePath, contentPathString ); - } - - private Site getNearestSite( final Content content ) - { - if ( content != null ) + if ( content == null ) { - if ( ContentPath.ROOT.equals( content.getPath() ) ) - { - return null; - } - else - { - return content.isSite() - ? (Site) content - : callAsContentAdmin( () -> this.contentService.getNearestSite( content.getId() ) ); - } + return new ContentResolverResult( null, false, null, contentPathString, contentPathString ); } - else + + if ( content.getPath().isRoot() ) { - return null; + return new ContentResolverResult( null, false, null, "/", contentPathString ); } + + final Site site = + content.isSite() ? (Site) content : callAsContentAdmin( () -> this.contentService.getNearestSite( content.getId() ) ); + + final String siteRelativePath = siteRelativePath( site, content.getPath() ); + return new ContentResolverResult( visibleContent( content ), true, site, siteRelativePath, contentPathString ); } private static ContentId tryConvertToContentId( final String contentPathString ) @@ -101,7 +98,7 @@ private static ContentId tryConvertToContentId( final String contentPathString ) private Content visibleContent( final Content content ) { - return content == null || ContentPath.ROOT.equals( content.getPath() ) || + return content == null || content.getPath().isRoot() || !content.getPermissions().isAllowedFor( ContextAccessor.current().getAuthInfo().getPrincipals(), Permission.READ ) ? null : content; 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 12c7f89e76f..f8682610706 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 @@ -139,6 +139,24 @@ void resolve_root_edit_mode() assertNull( result.getContent() ); assertNull( result.getNearestSite() ); assertEquals( "/", result.getSiteRelativePath() ); + final WebException exception = assertThrows( WebException.class, result::getContentOrElseThrow ); + assertEquals( HttpStatus.NOT_FOUND, exception.getStatus() ); + } + + @Test + void resolve_root_path_in_edit_mode() + { + final PortalRequest request = new PortalRequest(); + request.setMode( RenderMode.EDIT ); + request.setContentPath( ContentPath.ROOT ); + + final ContentResolverResult result = new ContentResolver( contentService ).resolve( request ); + + assertNull( result.getContent() ); + assertNull( result.getNearestSite() ); + assertEquals( "/", result.getSiteRelativePath() ); + final WebException exception = assertThrows( WebException.class, result::getContentOrElseThrow ); + assertEquals( HttpStatus.NOT_FOUND, exception.getStatus() ); } @Test @@ -211,7 +229,6 @@ void resolve_self_in_live_mode() request.setContentPath( ContentPath.from( "/mysite" ) ); when( this.contentService.getByPath( ContentPath.from( "/mysite" ) ) ).thenReturn( site ); - when( this.contentService.findNearestSiteByPath( ContentPath.from( "/mysite" ) ) ).thenReturn( site ); final ContentResolverResult result = new ContentResolver( contentService ).resolve( request ); @@ -281,6 +298,21 @@ void resolve_no_site_in_live_mode() assertNotNull( result.getSiteRelativePath() ); } + @Test + void resolve_root_in_live_mode() + { + final PortalRequest request = new PortalRequest(); + request.setContentPath( ContentPath.ROOT ); + + final ContentResolverResult result = new ContentResolver( contentService ).resolve( request ); + + assertNull( result.getContent() ); + assertNull( result.getNearestSite() ); + assertEquals( "/", result.getSiteRelativePath() ); + final WebException exception = assertThrows( WebException.class, result::getContentOrElseThrow ); + assertEquals( HttpStatus.NOT_FOUND, exception.getStatus() ); + } + private Content newContent() { final Content.Builder builder = Content.create();