Skip to content

Commit

Permalink
403 for /site/repo/branch for root content #10368
Browse files Browse the repository at this point in the history
(cherry picked from commit a17b33f)
  • Loading branch information
rymsha committed Jan 4, 2024
1 parent 171cdc4 commit ca5697d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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() );
Expand All @@ -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 )
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ca5697d

Please sign in to comment.