Skip to content

Commit

Permalink
Update actions from CRUDController in order to assert that referenc…
Browse files Browse the repository at this point in the history
…ed objects exist
  • Loading branch information
phansys committed Mar 5, 2021
1 parent b67a89c commit e661297
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 94 deletions.
58 changes: 35 additions & 23 deletions src/Controller/CRUDController.php
Expand Up @@ -135,6 +135,8 @@ public function listAction()
{
$request = $this->getRequest();

$this->assertObjectExists($request);

$this->admin->checkAccess('list');

$preResponse = $this->preList($request);
Expand Down Expand Up @@ -213,9 +215,7 @@ public function deleteAction($id) // NEXT_MAJOR: Remove the unused $id parameter
$id = $request->get($this->admin->getIdParameter());
$object = $this->admin->getObject($id);

if (!$object) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->assertObjectExists($request);

$this->checkParentChildAssociation($request, $object);

Expand Down Expand Up @@ -306,9 +306,7 @@ public function editAction($deprecatedId = null) // NEXT_MAJOR: Remove the unuse
$id = $request->get($this->admin->getIdParameter());
$existingObject = $this->admin->getObject($id);

if (!$existingObject) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->assertObjectExists($request);

$this->checkParentChildAssociation($request, $existingObject);

Expand Down Expand Up @@ -564,11 +562,14 @@ public function batchAction()
public function createAction()
{
$request = $this->getRequest();
// the key used to lookup the template
$templateKey = 'edit';

$this->assertObjectExists($request);

$this->admin->checkAccess('create');

// the key used to lookup the template
$templateKey = 'edit';

$class = new \ReflectionClass($this->admin->hasActiveSubClass() ? $this->admin->getActiveSubClass() : $this->admin->getClass());

if ($class->isAbstract()) {
Expand Down Expand Up @@ -694,9 +695,7 @@ public function showAction($deprecatedId = null) // NEXT_MAJOR: Remove the unuse
$id = $request->get($this->admin->getIdParameter());
$object = $this->admin->getObject($id);

if (!$object) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->assertObjectExists($request);

$this->checkParentChildAssociation($request, $object);

Expand Down Expand Up @@ -748,9 +747,7 @@ public function historyAction($deprecatedId = null) // NEXT_MAJOR: Remove the un
$id = $request->get($this->admin->getIdParameter());
$object = $this->admin->getObject($id);

if (!$object) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->assertObjectExists($request);

$this->admin->checkAccess('history', $object);

Expand Down Expand Up @@ -796,9 +793,7 @@ public function historyViewRevisionAction($id = null, $revision = null) // NEXT_
$id = $request->get($this->admin->getIdParameter());
$object = $this->admin->getObject($id);

if (!$object) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->assertObjectExists($request);

$this->admin->checkAccess('historyViewRevision', $object);

Expand Down Expand Up @@ -858,9 +853,7 @@ public function historyCompareRevisionsAction($id = null, $base_revision = null,
$id = $request->get($this->admin->getIdParameter());
$object = $this->admin->getObject($id);

if (!$object) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->assertObjectExists($request);

$manager = $this->get('sonata.admin.audit.manager');

Expand Down Expand Up @@ -1001,9 +994,7 @@ public function aclAction($deprecatedId = null) // NEXT_MAJOR: Remove the unused
$id = $request->get($this->admin->getIdParameter());
$object = $this->admin->getObject($id);

if (!$object) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->assertObjectExists($request);

$this->admin->checkAccess('acl', $object);

Expand Down Expand Up @@ -1643,6 +1634,27 @@ protected function handleXmlHttpRequestSuccessResponse(Request $request, object
], Response::HTTP_OK);
}

final protected function assertObjectExists(Request $request): void
{
$admin = $this->admin;

do {
$objectId = $request->get($admin->getIdParameter());

if (null !== $objectId) {
$adminObject = $admin->getObject($objectId);

if (null === $adminObject) {
throw $this->createNotFoundException(sprintf(
'Unable to find %s object with id: %s.',
$admin->getClassnameLabel(),
$objectId
));
}
}
} while ($admin->isChild() && $admin = $admin->getParent());
}

private function getSelectedTab(Request $request): array
{
return array_filter(['_tab' => $request->request->get('_tab')]);
Expand Down

0 comments on commit e661297

Please sign in to comment.