Skip to content

Commit

Permalink
Merge pull request #591 from ezsystems/fix-EZP-21874-ParentContentTyp…
Browse files Browse the repository at this point in the history
…eLimitationType-draft-support

EZP-21874: ParentContentType Limitation does not work correctly with draft Content
  • Loading branch information
pspanja committed Nov 1, 2013
2 parents b1a363a + cf177cf commit ddc29a7
Show file tree
Hide file tree
Showing 3 changed files with 782 additions and 2 deletions.
Expand Up @@ -41,6 +41,7 @@ public function testParentContentTypeLimitationAllow()
$user = $this->createUserVersion1();

$roleService = $repository->getRoleService();
$contentService = $repository->getContentService();

$role = $roleService->loadRoleByIdentifier( 'Editor' );

Expand All @@ -63,11 +64,12 @@ public function testParentContentTypeLimitationAllow()
$repository->setCurrentUser( $user );

$draft = $this->createWikiPageDraft();
$content = $contentService->publishVersion( $draft->versionInfo );
/* END: Use Case */

$this->assertEquals(
'An awesome wiki page',
$draft->getFieldValue( 'title' )->text
$content->getFieldValue( 'title' )->text
);
}

Expand Down
83 changes: 82 additions & 1 deletion eZ/Publish/Core/Limitation/ParentContentTypeLimitationType.php
Expand Up @@ -12,6 +12,10 @@
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
use eZ\Publish\API\Repository\Values\ValueObject;
use eZ\Publish\API\Repository\Values\User\User as APIUser;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
Expand Down Expand Up @@ -122,7 +126,42 @@ public function evaluate( APILimitationValue $value, APIUser $currentUser, Value
if ( !$value instanceof APIParentContentTypeLimitation )
throw new InvalidArgumentException( '$value', 'Must be of type: APIParentContentTypeLimitation' );

// Parent Limitations are usually used by content/create where target is specified, so we return false if not provided.
if ( $object instanceof ContentCreateStruct )
{
return $this->evaluateForContentCreateStruct( $value, $targets );
}
else if ( $object instanceof Content )
{
$object = $object->getVersionInfo()->getContentInfo();
}
else if ( $object instanceof VersionInfo )
{
$object = $object->getContentInfo();
}
else if ( !$object instanceof ContentInfo )
{
throw new InvalidArgumentException(
"\$object",
"Must be of type: ContentCreateStruct, Content, VersionInfo or ContentInfo"
);
}

// Try to load locations if no targets were provided
if ( empty( $targets ) )
{
if ( $object->published )
{
$targets = $this->persistence->locationHandler()->loadLocationsByContent( $object->id );
}
else
{
// @todo Need support for draft locations to to work correctly
$targets = $this->persistence->locationHandler()->loadParentLocationsForDraftContent( $object->id );
}
}

// If targets is empty/null return false as user does not have access
// to content w/o location with this limitation
if ( empty( $targets ) )
{
return false;
Expand Down Expand Up @@ -161,6 +200,48 @@ public function evaluate( APILimitationValue $value, APIUser $currentUser, Value
return true;
}

/**
* Evaluate permissions for ContentCreateStruct against LocationCreateStruct placements.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If $targets does not contain
* objects of type LocationCreateStruct
*
* @param \eZ\Publish\API\Repository\Values\User\Limitation $value
* @param array $targets
*
* @return bool
*/
protected function evaluateForContentCreateStruct( APILimitationValue $value, array $targets )
{
// If targets is empty/null return false as user does not have access
// to content w/o location with this limitation
if ( empty( $targets ) )
{
return false;
}

foreach ( $targets as $target )
{
if ( !$target instanceof LocationCreateStruct )
{
throw new InvalidArgumentException(
'$targets',
'If $object is ContentCreateStruct must contain objects of type: LocationCreateStruct'
);
}

$location = $this->persistence->locationHandler()->load( $target->parentLocationId );
$contentTypeId = $this->persistence->contentHandler()->loadContentInfo( $location->contentId )->contentTypeId;

if ( !in_array( $contentTypeId, $value->limitationValues ) )
{
return false;
}
}

return true;
}

/**
* Returns Criterion for use in find() query
*
Expand Down

0 comments on commit ddc29a7

Please sign in to comment.