Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also check for id in assertObjectExists #7240

Merged
merged 2 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@ public function batchActionDelete(ProxyQueryInterface $query)
public function deleteAction($id) // NEXT_MAJOR: Remove the unused $id parameter
{
$request = $this->getRequest();
$this->assertObjectExists($request, true);

$id = $request->get($this->admin->getIdParameter());
\assert(null !== $id);
$object = $this->admin->getObject($id);

$this->assertObjectExists($request);
\assert(null !== $object);

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

Expand Down Expand Up @@ -303,10 +305,12 @@ public function editAction($deprecatedId = null) // NEXT_MAJOR: Remove the unuse
$templateKey = 'edit';

$request = $this->getRequest();
$this->assertObjectExists($request, true);

$id = $request->get($this->admin->getIdParameter());
\assert(null !== $id);
$existingObject = $this->admin->getObject($id);

$this->assertObjectExists($request);
\assert(null !== $existingObject);

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

Expand Down Expand Up @@ -692,10 +696,12 @@ public function showAction($deprecatedId = null) // NEXT_MAJOR: Remove the unuse
}

$request = $this->getRequest();
$this->assertObjectExists($request, true);

$id = $request->get($this->admin->getIdParameter());
\assert(null !== $id);
$object = $this->admin->getObject($id);

$this->assertObjectExists($request);
\assert(null !== $object);

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

Expand Down Expand Up @@ -744,10 +750,12 @@ public function historyAction($deprecatedId = null) // NEXT_MAJOR: Remove the un
}

$request = $this->getRequest();
$this->assertObjectExists($request, true);

$id = $request->get($this->admin->getIdParameter());
\assert(null !== $id);
$object = $this->admin->getObject($id);

$this->assertObjectExists($request);
\assert(null !== $object);

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

Expand Down Expand Up @@ -790,10 +798,12 @@ public function historyAction($deprecatedId = null) // NEXT_MAJOR: Remove the un
public function historyViewRevisionAction($id = null, $revision = null) // NEXT_MAJOR: Remove the unused $id parameter
{
$request = $this->getRequest();
$this->assertObjectExists($request, true);

$id = $request->get($this->admin->getIdParameter());
\assert(null !== $id);
$object = $this->admin->getObject($id);

$this->assertObjectExists($request);
\assert(null !== $object);

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

Expand Down Expand Up @@ -850,9 +860,10 @@ public function historyCompareRevisionsAction($id = null, $baseRevision = null,
$this->admin->checkAccess('historyCompareRevisions');

$request = $this->getRequest();
$id = $request->get($this->admin->getIdParameter());
$this->assertObjectExists($request, true);

$this->assertObjectExists($request);
$id = $request->get($this->admin->getIdParameter());
\assert(null !== $id);

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

Expand Down Expand Up @@ -1014,10 +1025,12 @@ public function aclAction($deprecatedId = null) // NEXT_MAJOR: Remove the unused
}

$request = $this->getRequest();
$this->assertObjectExists($request, true);

$id = $request->get($this->admin->getIdParameter());
\assert(null !== $id);
$object = $this->admin->getObject($id);

$this->assertObjectExists($request);
\assert(null !== $object);

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

Expand Down Expand Up @@ -1658,23 +1671,27 @@ protected function handleXmlHttpRequestSuccessResponse(Request $request, object
], Response::HTTP_OK);
}

final protected function assertObjectExists(Request $request): void
final protected function assertObjectExists(Request $request, bool $strict = false): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which cases you want to call it with false?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For listAction and createAction.

There is no id in the request for the current admin.
But if it's a childAdmin, there will be id for the parent admin, grand-parent admin, and so on.

{
$admin = $this->admin;

while (null !== $admin) {
$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
));
}
} elseif ($strict || $admin !== $this->admin) {
throw $this->createNotFoundException(sprintf(
'Unable to find the %s object id of the admin "%s".',
$admin->getClassnameLabel(),
\get_class($admin)
));
}

$admin = $admin->isChild() ? $admin->getParent() : null;
Expand Down
Loading