public function evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets = null)
{
if (!$value instanceof APIParentOwnerLimitation) {
throw new InvalidArgumentException('$value', 'Must be of type: APIParentOwnerLimitation');
}
if ($value->limitationValues[0] != 1 && $value->limitationValues[0] != 2) {
throw new BadStateException('Parent Owner limitation', 'expected limitation value to be 1 or 2 but got:' . $value->limitationValues[0]);
}
// Parent Limitations are usually used by content/create where target is specified, so we return false if not provided.
if (empty($targets)) {
return false;
}
foreach ($targets as $target) {
if ($target instanceof LocationCreateStruct) {
$target = $this->persistence->locationHandler()->load($target->parentLocationId);
}
if ($target instanceof Location) {
$targetContentInfo = $target->getContentInfo();
} elseif ($target instanceof SPILocation) {
$targetContentInfo = $this->persistence->contentHandler()->loadContentInfo($target->contentId);
} else {
throw new InvalidArgumentException('$targets', 'Must contain objects of type: Location or LocationCreateStruct');
}
$userId = $currentUser->getUserId();
$isOwner = $targetContentInfo->ownerId === $userId;
$isSelf = $targetContentInfo->id === $userId;
if (!($isOwner || $isSelf)) {
return false;
}
}
return true;
}