Skip to content

Commit

Permalink
IBX-6592: Allowed Location to be a part of permission check for Obj…
Browse files Browse the repository at this point in the history
…ect State assignment
  • Loading branch information
barw4 committed Nov 7, 2023
1 parent f721921 commit 06860b6
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 77 deletions.
13 changes: 10 additions & 3 deletions src/bundle/Controller/ObjectStateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ public function updateAction(Request $request, ObjectState $objectState): Respon
public function updateContentStateAction(
Request $request,
ContentInfo $contentInfo,
ObjectStateGroup $objectStateGroup
ObjectStateGroup $objectStateGroup,
Location $location
): Response {
if (!$this->permissionResolver->hasAccess('state', 'assign')) {
$exception = $this->createAccessDeniedException();
Expand All @@ -319,7 +320,7 @@ public function updateContentStateAction(

$form = $this->formFactory->create(
ContentObjectStateUpdateType::class,
new ContentObjectStateUpdateData($contentInfo, $objectStateGroup)
new ContentObjectStateUpdateData($contentInfo, $objectStateGroup, null, $location)
);
$form->handleRequest($request);

Expand All @@ -328,7 +329,13 @@ public function updateContentStateAction(
$contentInfo = $data->getContentInfo();
$objectStateGroup = $data->getObjectStateGroup();
$objectState = $data->getObjectState();
$this->objectStateService->setContentState($contentInfo, $objectStateGroup, $objectState);
$location = $data->getLocation();
$this->objectStateService->setContentState(
$contentInfo,
$objectStateGroup,
$objectState,
$location
);

$this->notificationHandler->success(
/** @Desc("Content item's Object state changed to '%name%'.") */
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Resources/config/routing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ ezplatform.object_state.state.bulk_delete:
_controller: 'EzSystems\EzPlatformAdminUiBundle\Controller\ObjectStateController::bulkDeleteAction'

ezplatform.object_state.contentstate.update:
path: /state/contentstate/update/{contentInfoId}/group/{objectStateGroupId}
path: /state/contentstate/update/{contentInfoId}/group/{objectStateGroupId}/{locationId}
defaults:
_controller: 'EzSystems\EzPlatformAdminUiBundle\Controller\ObjectStateController::updateContentStateAction'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
'method': 'POST',
'action': path('ezplatform.object_state.contentstate.update', {
'contentInfoId': content_info.id,
'objectStateGroupId': object_state.objectStateGroup.id
'objectStateGroupId': object_state.objectStateGroup.id,
'locationId': location.id,
}),
'attr': {'class': 'form-inline ez-form-inline ez-form-inline--align-left'}
}) }}
Expand Down
53 changes: 20 additions & 33 deletions src/lib/Form/Data/ObjectState/ContentObjectStateUpdateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,86 +9,73 @@
namespace EzSystems\EzPlatformAdminUi\Form\Data\ObjectState;

use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectState;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup;

class ContentObjectStateUpdateData
{
/**
* @var \eZ\Publish\API\Repository\Values\Content\ContentInfo
*/
/** @var \eZ\Publish\API\Repository\Values\Content\ContentInfo|null */
private $contentInfo;

/**
* @var \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
*/
/** @var \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup|null */
private $objectStateGroup;

/**
* @var \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
*/
/** @var \eZ\Publish\API\Repository\Values\ObjectState\ObjectState|null rm -r*/
private $objectState;

/**
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo|null $contentInfo
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup|null $objectStateGroup
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState|null $objectState
*/
/** @var \eZ\Publish\API\Repository\Values\Content\Location|null */
private $location;

public function __construct(
ContentInfo $contentInfo = null,
ObjectStateGroup $objectStateGroup = null,
ObjectState $objectState = null
ObjectState $objectState = null,
Location $location = null
) {
$this->contentInfo = $contentInfo;
$this->objectStateGroup = $objectStateGroup;
$this->objectState = $objectState;
$this->location = $location;
}

/**
* @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
*/
public function getContentInfo(): ContentInfo
{
return $this->contentInfo;
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
*/
public function setContentInfo(ContentInfo $contentInfo)
{
$this->contentInfo = $contentInfo;
}

/**
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
*/
public function getObjectStateGroup(): ObjectStateGroup
{
return $this->objectStateGroup;
}

/**
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
*/
public function setObjectStateGroup(ObjectStateGroup $objectStateGroup)
{
$this->objectStateGroup = $objectStateGroup;
}

/**
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState|null
*/
public function getObjectState(): ?ObjectState
{
return $this->objectState;
}

/**
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
*/
public function setObjectState(ObjectState $objectState)
{
$this->objectState = $objectState;
}

public function getLocation(): ?Location
{
return $this->location;
}

public function setLocation(Location $location)
{
$this->location = $location;
}
}
25 changes: 15 additions & 10 deletions src/lib/Form/Type/ObjectState/ContentObjectStateUpdateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,25 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$contentObjectStateUpdateData = $event->getData();
$objectStateGroup = $contentObjectStateUpdateData->getObjectStateGroup();
$contentInfo = $contentObjectStateUpdateData->getContentInfo();
$location = $contentObjectStateUpdateData->getLocation();
$form = $event->getForm();

$form->add('objectState', ObjectStateChoiceType::class, [
'label' => false,
'choice_loader' => new CallbackChoiceLoader(function () use ($objectStateGroup, $contentInfo) {
$contentState = $this->objectStateService->getContentState($contentInfo, $objectStateGroup);

return array_filter(
$this->objectStateService->loadObjectStates($objectStateGroup),
function (ObjectState $objectState) use ($contentInfo, $contentState) {
return $this->permissionResolver->canUser('state', 'assign', $contentInfo, [$objectState]);
}
);
}),
'choice_loader' => new CallbackChoiceLoader(
function () use ($objectStateGroup, $contentInfo, $location) {
return array_filter(
$this->objectStateService->loadObjectStates($objectStateGroup),
function (ObjectState $objectState) use ($contentInfo, $location) {
return $this->permissionResolver->canUser(
'state',
'assign',
$contentInfo,
[$location, $objectState],
);
}
);
}),
]);
});
}
Expand Down
20 changes: 11 additions & 9 deletions src/lib/Tab/LocationView/DetailsTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function getTemplateParameters(array $contextParameters = []): array
]);

$this->supplySectionParameters($viewParameters, $contentInfo, $location);
$this->supplyObjectStateParameters($viewParameters, $contentInfo);
$this->supplyObjectStateParameters($viewParameters, $contentInfo, $location);
$this->supplyTranslations($viewParameters, $versionInfo);
$this->supplyFormLocationUpdate($viewParameters, $location);
$this->supplyCreator($viewParameters, $contentInfo);
Expand Down Expand Up @@ -185,14 +185,13 @@ private function supplyLastContributor(ArrayObject $parameters, VersionInfo $ver
}
}

/**
* @param \ArrayObject $parameters
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
*/
private function supplyObjectStateParameters(ArrayObject &$parameters, ContentInfo $contentInfo): void
{
private function supplyObjectStateParameters(
ArrayObject &$parameters,
ContentInfo $contentInfo,
Location $location
): void {
$objectStatesDataset = $this->datasetFactory->objectStates();
$objectStatesDataset->load($contentInfo);
$objectStatesDataset->load($contentInfo, $location);

$canAssignObjectState = $this->canUserAssignObjectState();

Expand All @@ -206,7 +205,10 @@ private function supplyObjectStateParameters(ArrayObject &$parameters, ContentIn
$objectStateUpdateForm = $this->formFactory->create(
ContentObjectStateUpdateType::class,
new ContentObjectStateUpdateData(
$contentInfo, $objectStateGroup, $objectState
$contentInfo,
$objectStateGroup,
$objectState,
$location,
)
)->createView();

Expand Down
17 changes: 4 additions & 13 deletions src/lib/UI/Dataset/ObjectStatesDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\ObjectStateService;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup;
use EzSystems\EzPlatformAdminUi\UI\Value as UIValue;
use EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory;
Expand All @@ -25,31 +26,21 @@ class ObjectStatesDataset
/** @var UIValue\ObjectState\ObjectState[] */
protected $data;

/**
* @param \eZ\Publish\API\Repository\ObjectStateService $objectStateService
* @param \EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory $valueFactory
*/
public function __construct(ObjectStateService $objectStateService, ValueFactory $valueFactory)
{
$this->objectStateService = $objectStateService;
$this->valueFactory = $valueFactory;
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
*
* @return ObjectStatesDataset
*/
public function load(ContentInfo $contentInfo): self
public function load(ContentInfo $contentInfo, Location $location): self
{
$data = array_map(
function (ObjectStateGroup $objectStateGroup) use ($contentInfo) {
function (ObjectStateGroup $objectStateGroup) use ($contentInfo, $location) {
$hasObjectStates = !empty($this->objectStateService->loadObjectStates($objectStateGroup));
if (!$hasObjectStates) {
return [];
}

return $this->valueFactory->createObjectState($contentInfo, $objectStateGroup);
return $this->valueFactory->createObjectState($contentInfo, $objectStateGroup, $location);
},
$this->objectStateService->loadObjectStateGroups()
);
Expand Down
15 changes: 8 additions & 7 deletions src/lib/UI/Value/ValueFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,22 +253,23 @@ public function createLocation(Location $location): UIValue\Content\Location
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
*
* @return UIValue\ObjectState\ObjectState
*
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function createObjectState(
ContentInfo $contentInfo,
ObjectStateGroup $objectStateGroup
ObjectStateGroup $objectStateGroup,
Location $location
): UIValue\ObjectState\ObjectState {
$objectState = $this->objectStateService->getContentState($contentInfo, $objectStateGroup);

return new UIValue\ObjectState\ObjectState($objectState, [
'userCanAssign' => $this->permissionResolver->canUser('state', 'assign', $contentInfo, [$objectState]),
'userCanAssign' => $this->permissionResolver->canUser(
'state',
'assign',
$contentInfo,
[$location, $objectState],
),
]);
}

Expand Down

0 comments on commit 06860b6

Please sign in to comment.