Skip to content

Commit

Permalink
Merge pull request ezsystems#1090 from ezsystems/search_load_permissi…
Browse files Browse the repository at this point in the history
…on_fix

EZP-23643: Search without permissions throws UnauthorizedException
  • Loading branch information
andrerom committed Nov 18, 2014
2 parents 4e5b7e7 + 7eeb0c8 commit bee53ad
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 126 deletions.
114 changes: 0 additions & 114 deletions eZ/Publish/API/Repository/Tests/ContentServiceAuthorizationTest.php
Expand Up @@ -1367,120 +1367,6 @@ public function testDeleteRelationThrowsUnauthorizedException()
/* END: Use Case */
}

/**
* Test for the findContent() method.
*
* @return void
* @see \eZ\Publish\API\Repository\ContentService::findContent($query, $fieldFilters, $filterOnUserPermissions)
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testFindContent
*/
public function testFindContentWithUserPermissionFilter()
{
self::markTestIncomplete( "Search have been moved to SearchService" );
$repository = $this->getRepository();

/* BEGIN: Use Case */
$user = $this->createMediaUserVersion1();

// Set new media editor as current user
$repository->setCurrentUser( $user );

$contentService = $repository->getContentService();

// Create a search query for content objects about "eZ Publish"
$query = new Query();
$query->filter = new Criterion\LogicalAnd(
array(
new Criterion\Field( 'title', Criterion\Operator::LIKE, '*eZ Publish*' )
)
);

// Search for matching content
$searchResultWithoutPermissions = $contentService->findContent( $query, array(), false );

// Search for matching content
$searchResultWithPermissions = $contentService->findContent( $query, array() );
/* END: Use Case */

$this->assertGreaterThan(
$searchResultWithPermissions->count,
$searchResultWithoutPermissions->count
);
}

/**
* Test for the findSingle() method.
*
* @return void
* @see \eZ\Publish\API\Repository\ContentService::findSingle($query, $fieldFilters, $filterOnUserPermissions)
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testFindContent
*/
public function testFindSingleWithUserPermissionFilter()
{
self::markTestIncomplete( "Search have been moved to SearchService" );
$repository = $this->getRepository();

/* BEGIN: Use Case */
$user = $this->createMediaUserVersion1();

// Set new media editor as current user
$repository->setCurrentUser( $user );

$contentService = $repository->getContentService();

// Create a search query for content objects about "eZ Publish"
$query = new Query();
$query->filter = new Criterion\LogicalAnd(
array(
new Criterion\Field( 'name', Criterion\Operator::LIKE, 'Administrator users' )
)
);

// Search for matching content
$content = $contentService->findSingle( $query, array(), false );
/* END: Use Case */

$this->assertInstanceOf(
'\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
$content
);
}

/**
* Test for the findSingle() method.
*
* @return void
* @see \eZ\Publish\API\Repository\ContentService::findSingle($query, $fieldFilters, $filterOnUserPermissions)
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testFindContent
*/
public function testFindSingleThrowsNotFoundExceptionWithUserPermissionFilter()
{
self::markTestIncomplete( "Search have been moved to SearchService" );
$repository = $this->getRepository();

/* BEGIN: Use Case */
$user = $this->createMediaUserVersion1();

// Set new media editor as current user
$repository->setCurrentUser( $user );

$contentService = $repository->getContentService();

// Create a search query for content objects about "eZ Publish"
$query = new Query();
$query->filter = new Criterion\LogicalAnd(
array(
new Criterion\Field( 'name', Criterion\Operator::LIKE, 'Administrator users' )
)
);

// This call will fail with a "NotFoundException", because the current
// user has no access to the "Admin Users" user group
$contentService->findSingle( $query, array(), true );
/* END: Use Case */
}

/**
* Creates a pseudo editor with a limitation to objects in the "Media/Images"
* subtree.
Expand Down
Expand Up @@ -65,7 +65,7 @@ public function tearDown()
*
* @see \eZ\Publish\API\Repository\LanguageService::createLanguage()
*
* @depends \eZ\Publish\API\Repository\Tests\LanguageServiceTest::testNewLanguageCreateStruct
* @depends eZ\Publish\API\Repository\Tests\LanguageServiceTest::testNewLanguageCreateStruct
*
* @expectedException \RuntimeException
* @expectedExceptionMessage Maximum number of languages reached!
Expand Down
2 changes: 1 addition & 1 deletion eZ/Publish/API/Repository/Tests/RoleServiceTest.php
Expand Up @@ -1026,7 +1026,7 @@ public function testRemovePolicy()
*
* @return void
* @see \eZ\Publish\API\Repository\RoleService::deletePolicy()
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::loadRole
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAddPolicy
*/
public function testDeletePolicy()
Expand Down
101 changes: 99 additions & 2 deletions eZ/Publish/API/Repository/Tests/SearchServiceAuthorizationTest.php
Expand Up @@ -27,7 +27,7 @@ class SearchServiceAuthorizationTest extends BaseTest
*
* @return void
* @see \eZ\Publish\API\Repository\SearchService::findContent()
* @depends eZ\Publish\API\Repository\Tests\SearchServiceTest::testFindContent
* @depends eZ\Publish\API\Repository\Tests\SearchServiceTest::testFindContentFiltered
*/
public function testFindContent()
{
Expand Down Expand Up @@ -57,7 +57,7 @@ public function testFindContent()
*
* @return void
* @see \eZ\Publish\API\Repository\SearchService::findContent()
* @depends eZ\Publish\API\Repository\Tests\SearchServiceTest::testFindContent
* @depends eZ\Publish\API\Repository\Tests\SearchServiceTest::testFindContentFiltered
*/
public function testFindContentEmptyResult()
{
Expand Down Expand Up @@ -115,4 +115,101 @@ public function testFindSingleThrowsNotFoundException()
);
/* END: Use Case */
}

/**
* Test for the findContent() method, verifying disabling permissions
*
* @return void
* @see \eZ\Publish\API\Repository\ContentService::findContent($query, $fieldFilters, $filterOnUserPermissions)
* @depends eZ\Publish\API\Repository\Tests\SearchServiceAuthorizationTest::testFindContent
*/
public function testFindContentWithUserPermissionFilter()
{
$repository = $this->getRepository();

/* BEGIN: Use Case */
$user = $this->createMediaUserVersion1();

// Set new media editor as current user
$repository->setCurrentUser( $user );

$searchService = $repository->getSearchService();

// Search for "Admin Users" user group which user normally does not have access to
$query = new Query();
$query->filter = new Criterion\LogicalAnd(
array(
new Criterion\ContentId( 12 ),
)
);

// Search for matching content
$searchResultWithoutPermissions = $searchService->findContent( $query, array(), false );

// Search for matching content
$searchResultWithPermissions = $searchService->findContent( $query, array() );
/* END: Use Case */

$this->assertEquals( 1, $searchResultWithoutPermissions->totalCount );
$this->assertEquals( 0, $searchResultWithPermissions->totalCount );
}

/**
* Test for the findSingle() method disabling permission filtering
*
* @return void
* @see \eZ\Publish\API\Repository\ContentService::findSingle($query, $fieldFilters, $filterOnUserPermissions)
* @depends eZ\Publish\API\Repository\Tests\SearchServiceAuthorizationTest::testFindContent
*/
public function testFindSingleWithUserPermissionFilter()
{
$repository = $this->getRepository();

/* BEGIN: Use Case */
$user = $this->createMediaUserVersion1();

// Set new media editor as current user
$repository->setCurrentUser( $user );

// Search for "Admin Users" user group which user normally does not have access to
$content = $repository->getSearchService()->findSingle(
new Criterion\ContentId( 12 ),
array(),
false
);
/* END: Use Case */

$this->assertInstanceOf(
'\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
$content
);
}

/**
* Test for the findSingle() method.
*
* @return void
* @see \eZ\Publish\API\Repository\ContentService::findSingle($query, $fieldFilters, $filterOnUserPermissions)
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @depends eZ\Publish\API\Repository\Tests\SearchServiceAuthorizationTest::testFindContent
*/
public function testFindSingleThrowsNotFoundExceptionWithUserPermissionFilter()
{
$repository = $this->getRepository();

/* BEGIN: Use Case */
$user = $this->createMediaUserVersion1();

// Set new media editor as current user
$repository->setCurrentUser( $user );

$searchService = $repository->getSearchService();

// This call will fail with a "NotFoundException", because the current
// user has no access to the "Admin Users" user group
$searchService->findSingle(
new Criterion\ContentId( 12 )
);
/* END: Use Case */
}
}
11 changes: 10 additions & 1 deletion eZ/Publish/API/Repository/Tests/SearchServiceLocationTest.php
Expand Up @@ -10,6 +10,7 @@
namespace eZ\Publish\API\Repository\Tests;

use eZ\Publish\API\Repository\Tests\SetupFactory\LegacyElasticsearch;
use eZ\Publish\API\Repository\Tests\SetupFactory\LegacySolr;
use eZ\Publish\Core\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
Expand All @@ -18,7 +19,6 @@
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
use eZ\Publish\API\Repository\Tests\SetupFactory\LegacySolr;

/**
* Test case for Location operations in the SearchService.
Expand Down Expand Up @@ -830,6 +830,15 @@ public function testQueryCustomField()
*/
public function testQueryModifiedField()
{
// Check using get_class since the others extend SetupFactory\Legacy
if ( get_class( $this->getSetupFactory() ) === '\eZ\Publish\API\Repository\Tests\SetupFactory\Legacy' )
{
$this->markTestIncomplete(
"Custom fields not supported by LegacySE " .
"(@todo: Legacy should fallback to just querying normal field so this should be tested here)"
);
}

$query = new LocationQuery(
array(
'query' => new Criterion\Field(
Expand Down
11 changes: 10 additions & 1 deletion eZ/Publish/API/Repository/Tests/SearchServiceTest.php
Expand Up @@ -10,6 +10,7 @@
namespace eZ\Publish\API\Repository\Tests;

use eZ\Publish\API\Repository\Tests\SetupFactory\LegacyElasticsearch;
use eZ\Publish\API\Repository\Tests\SetupFactory\LegacySolr;
use eZ\Publish\Core\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\Core\Repository\Values\Content\Location;
Expand All @@ -20,7 +21,6 @@
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
use eZ\Publish\API\Repository\Tests\SetupFactory\LegacySolr;

/**
* Test case for operations in the SearchService using in memory storage.
Expand Down Expand Up @@ -2349,6 +2349,15 @@ public function testQueryCustomField()
*/
public function testQueryModifiedField()
{
// Check using get_class since the others extend SetupFactory\Legacy
if ( get_class( $this->getSetupFactory() ) === '\eZ\Publish\API\Repository\Tests\SetupFactory\Legacy' )
{
$this->markTestIncomplete(
"Custom fields not supported by LegacySE " .
"(@todo: Legacy should fallback to just querying normal field so this should be tested here)"
);
}

$query = new Query(
array(
'query' => new Criterion\Field(
Expand Down
6 changes: 4 additions & 2 deletions eZ/Publish/Core/Repository/SearchService.php
Expand Up @@ -137,10 +137,11 @@ public function findContent( Query $query, array $fieldFilters = array(), $filte
$contentService = $this->repository->getContentService();
foreach ( $result->searchHits as $hit )
{
$hit->valueObject = $contentService->loadContent(
$hit->valueObject = $contentService->internalLoadContent(
$hit->valueObject->id,
( !empty( $fieldFilters['languages'] ) ? $fieldFilters['languages'] : null ),
null,
false,
( isset( $fieldFilters['useAlwaysAvailable'] ) ? $fieldFilters['useAlwaysAvailable'] : true )
);
}
Expand Down Expand Up @@ -265,10 +266,11 @@ public function findSingle( Criterion $filter, array $fieldFilters = array(), $f
}

$contentInfo = $this->searchHandler->findSingle( $filter, $fieldFilters );
return $this->repository->getContentService()->loadContent(
return $this->repository->getContentService()->internalLoadContent(
$contentInfo->id,
( !empty( $fieldFilters['languages'] ) ? $fieldFilters['languages'] : null ),
null,
false,
( isset( $fieldFilters['useAlwaysAvailable'] ) ? $fieldFilters['useAlwaysAvailable'] : true )
);
}
Expand Down
8 changes: 4 additions & 4 deletions eZ/Publish/Core/Repository/Tests/Service/Mock/SearchTest.php
Expand Up @@ -312,7 +312,7 @@ public function testFindContentNoPermissionsFilter()

$contentServiceMock
->expects( $this->once() )
->method( "loadContent" )
->method( "internalLoadContent" )
->will( $this->returnValue( $contentMock ) );

$result = $service->findContent( $serviceQuery, $fieldFilters, false );
Expand Down Expand Up @@ -394,7 +394,7 @@ public function testFindContentWithPermission()

$contentServiceMock
->expects( $this->once() )
->method( "loadContent" )
->method( "internalLoadContent" )
->will( $this->returnValue( $contentMock ) );

$permissionsCriterionHandlerMock->expects( $this->once() )
Expand Down Expand Up @@ -615,7 +615,7 @@ public function testFindContentWithDefaultQueryValues()

$contentServiceMock
->expects( $this->once() )
->method( "loadContent" )
->method( "internalLoadContent" )
->will( $this->returnValue( $contentMock ) );

/** @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
Expand Down Expand Up @@ -791,7 +791,7 @@ public function testFindSingle()

$contentServiceMock
->expects( $this->once() )
->method( "loadContent" )
->method( "internalLoadContent" )
->will( $this->returnValue( $contentMock ) );

$result = $service->findSingle( $criterionMock, $fieldFilters, true );
Expand Down

0 comments on commit bee53ad

Please sign in to comment.