Skip to content

Commit 0d2629e

Browse files
committed
fix: retrieve request array with all() instead of get()
1 parent 52b4255 commit 0d2629e

File tree

13 files changed

+162
-18
lines changed

13 files changed

+162
-18
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
APP_ENV=dev
2-
APP_SECRET=nSw!OiGdwCk1VYSL1zUo@F3l%$NtJW$saFU8i%6uy5waoI4!vs
2+
APP_SECRET=nSw!OiGdwCk1VYSL1zUo@F3l%$$NtJW$$saFU8i%6uy5waoI4!vs
33
DATABASE_URL=mysql://dev:dev@mysql:3306/numbernine_dev?serverVersion=5.7

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"imagine/imagine": "^1.2",
3838
"mpratt/relativetime": "^1.5",
3939
"nette/php-generator": "^4.0",
40-
"numberninecms/common": "^0.1.11",
40+
"numberninecms/common": "^0.1.12",
4141
"psr/cache": ">=1.0",
4242
"psr/log": ">=1.1",
4343
"scienta/doctrine-json-functions": "^4.1",

src/Controller/Admin/Api/ContentEntity/ContentEntitiesDeleteAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public function __invoke(
3838

3939
$this->denyAccessUnlessGranted($contentType->getMappedCapability(Capabilities::DELETE_POSTS));
4040

41-
/** @var array $ids */
42-
$ids = $request->request->get('ids');
41+
$ids = $request->request->all('ids');
4342

4443
try {
4544
if (empty($ids)) {

src/Controller/Admin/Api/ContentEntity/ContentEntitiesRestoreAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public function __invoke(
3131
ResponseFactory $responseFactory,
3232
string $type
3333
): JsonResponse {
34-
/** @var array $ids */
35-
$ids = $request->request->get('ids');
34+
$ids = $request->request->all('ids');
3635

3736
try {
3837
$contentService->restoreEntitiesOfType($type, $ids);

src/Controller/Admin/Api/Menu/MenuUpdateAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public function __invoke(
3030
ResponseFactory $responseFactory,
3131
Menu $menu
3232
): JsonResponse {
33-
/** @var array $menuItems */
34-
$menuItems = $request->request->get('menuItems') ?: [];
33+
$menuItems = $request->request->all('menuItems');
3534

3635
$menu->setMenuItems($menuItems);
3736

src/Controller/Admin/Api/PageBuilder/PageBuilderAreaComponentsUpdateAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public function __invoke(
3434
ThemeOptionsReadWriter $themeOptionsReadWriter,
3535
string $area
3636
): JsonResponse {
37-
/** @var array $components */
38-
$components = $request->request->get('components');
37+
$components = $request->request->all('components');
3938
$text = $arrayToShortcodeConverter->convertMany($components);
4039

4140
$areas = $themeOptionsReadWriter->read($themeStore->getCurrentTheme(), 'areas', []);

src/Controller/Admin/Api/PageBuilder/PageBuilderEntityComponentsUpdateAction.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,31 @@
1313

1414
use Doctrine\ORM\EntityManagerInterface;
1515
use NumberNine\Content\ArrayToShortcodeConverter;
16+
use NumberNine\Controller\Admin\CanEditPostsTrait;
1617
use NumberNine\Entity\ContentEntity;
1718
use NumberNine\Http\ResponseFactory;
19+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1820
use Symfony\Component\HttpFoundation\JsonResponse;
1921
use Symfony\Component\HttpFoundation\Request;
2022
use Symfony\Component\Routing\Annotation\Route;
2123

2224
#[Route(path: 'page_builder/{id<\d+>}/components', name: 'numbernine_admin_pagebuilder_post_entity_components', options: ['expose' => true], methods: [
2325
'POST',
2426
])]
25-
final class PageBuilderEntityComponentsUpdateAction
27+
final class PageBuilderEntityComponentsUpdateAction extends AbstractController
2628
{
29+
use CanEditPostsTrait;
30+
2731
public function __invoke(
2832
Request $request,
2933
EntityManagerInterface $entityManager,
3034
ResponseFactory $responseFactory,
3135
ArrayToShortcodeConverter $arrayToShortcodeConverter,
3236
ContentEntity $contentEntity
3337
): JsonResponse {
34-
/** @var array $components */
35-
$components = $request->request->get('components');
38+
$this->assertCanEditPosts($this->getUser(), $contentEntity);
39+
40+
$components = $request->request->all('components');
3641
$text = $arrayToShortcodeConverter->convertMany($components);
3742

3843
$contentEntity->setContent($text);

src/Controller/Admin/Api/PageBuilder/PageBuilderShortcodePresetCreateUpdateAction.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function __invoke(
3434
PresetRepository $templateRepository,
3535
string $name
3636
): JsonResponse {
37-
/** @var array $component */
3837
$component = $request->request->all();
3938

4039
if (empty($component)) {

src/Controller/Admin/Api/Taxonomy/TermDeleteAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public function __invoke(
3232
ResponseFactory $responseFactory,
3333
string $taxonomy
3434
): JsonResponse {
35-
/** @var array $ids */
36-
$ids = $request->request->get('ids');
35+
$ids = $request->request->all('ids');
3736

3837
try {
3938
$termRepository->removeCollection($ids);

src/Controller/Admin/Api/User/UsersDeleteAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public function __invoke(
3535
): JsonResponse {
3636
$this->denyAccessUnlessGranted(Capabilities::DELETE_USERS);
3737

38-
/** @var array $ids */
39-
$ids = $request->request->get('ids');
38+
$ids = $request->request->all('ids');
4039
/** @var string $associatedContent */
4140
$associatedContent = $request->request->get('associatedContent', 'reassign');
4241

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/*
3+
* This file is part of the NumberNine package.
4+
*
5+
* (c) William Arin <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace NumberNine\Controller\Admin;
14+
15+
use NumberNine\Content\ContentService;
16+
use NumberNine\Entity\ContentEntity;
17+
use NumberNine\Entity\User;
18+
use NumberNine\Security\Capabilities;
19+
use Symfony\Component\Security\Core\User\UserInterface;
20+
use Symfony\Contracts\Service\Attribute\Required;
21+
22+
/**
23+
* @property ContentService $contentService
24+
*
25+
* @method UserInterface getUser()
26+
* @method void denyAccessUnlessGranted(string $attribute)
27+
*/
28+
trait CanEditPostsTrait
29+
{
30+
#[Required]
31+
public ContentService $contentService;
32+
33+
private function assertCanEditPosts(UserInterface $user, ContentEntity $entity): void
34+
{
35+
$contentType = $this->contentService->getContentType($entity->getType());
36+
$this->denyAccessUnlessGranted($contentType->getMappedCapability(Capabilities::EDIT_POSTS));
37+
38+
if (
39+
$user instanceof User
40+
&& $entity->getAuthor() instanceof User
41+
&& $user->getId() !== $entity->getAuthor()->getId()
42+
) {
43+
$this->denyAccessUnlessGranted($contentType->getMappedCapability(Capabilities::EDIT_OTHERS_POSTS));
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"components":[{"id":"1670c881-3da2-4b8d-b96d-ae9dc5f641f8","name":"text","parameters":{"content":"Add a new component to this page..."},"computed":{},"position":0,"label":"Text","children":[],"siblingsPosition":["top","bottom"],"siblingsShortcodes":[],"icon":"file-alt","editable":true,"container":false,"responsive":[],"collapsed":false},{"type":"NumberNine\\Shortcode\\ButtonShortcode","name":"button","parameters":{"content":"","text":"View more...","case":"normal","color":"primary","style":"default","size":"normal","expand":false,"link":"","custom_class":""},"responsive":[],"computed":[],"editable":true,"container":false,"leaf":true,"id":"0dc6f0ce-d80c-41eb-af7c-b28ba799a9a0","position":0,"label":"Button","siblingsPosition":["top","bottom"],"siblingsShortcodes":[],"icon":"mdi-gesture-tap-button"}]}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/*
3+
* This file is part of the NumberNine package.
4+
*
5+
* (c) William Arin <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace NumberNine\Tests\Functional\Controller\Admin\Api\PageBuilder;
14+
15+
use NumberNine\Bundle\Test\UserAwareTestCase;
16+
use NumberNine\Entity\Post;
17+
use NumberNine\Model\Content\PublishingStatusInterface;
18+
use NumberNine\Security\Capabilities;
19+
20+
/**
21+
* @internal
22+
* @coversNothing
23+
*/
24+
final class PageBuilderEntityComponentsUpdateActionTest extends UserAwareTestCase
25+
{
26+
public function testNotLoggedInUserCantAccessUrl(): void
27+
{
28+
$post = $this->getPost();
29+
30+
$this->client->request('POST', $this->urlGenerator->generate(
31+
'numbernine_admin_pagebuilder_post_entity_components',
32+
['id' => $post->getId()],
33+
));
34+
self::assertResponseRedirects($this->urlGenerator->generate('numbernine_login'));
35+
}
36+
37+
public function testNonAllowedUserCantAccessUrl(): void
38+
{
39+
$this->setCapabilitiesThenLogin([Capabilities::ACCESS_ADMIN]);
40+
$post = $this->getPost();
41+
42+
$this->client->request('POST', $this->urlGenerator->generate(
43+
'numbernine_admin_pagebuilder_post_entity_components',
44+
['id' => $post->getId()],
45+
));
46+
self::assertResponseRedirects($this->urlGenerator->generate('numbernine_login'));
47+
}
48+
49+
public function testUpdatePostComponents(): void
50+
{
51+
$this->setCapabilitiesThenLogin([
52+
Capabilities::ACCESS_ADMIN,
53+
Capabilities::EDIT_POSTS,
54+
Capabilities::EDIT_OTHERS_POSTS,
55+
]);
56+
$post = $this->getPost();
57+
58+
$this->client->request(
59+
'POST',
60+
$this->urlGenerator->generate(
61+
'numbernine_admin_pagebuilder_post_entity_components',
62+
['id' => $post->getId()],
63+
),
64+
[],
65+
[],
66+
['CONTENT_TYPE' => 'application/json'],
67+
file_get_contents(
68+
__DIR__ . '/../../../../../Fixtures/Controller/Admin/Api/PageBuilder/PageBuilderEntityComponentsUpdateActionTest/content.json'
69+
),
70+
);
71+
72+
self::assertResponseIsSuccessful();
73+
74+
$post = $this->entityManager->getRepository(Post::class)->find($post->getId());
75+
static::assertSame(<<<'CONTENT'
76+
Add a new component to this page...
77+
[button text="View more..." case="normal" color="primary" style="default" size="normal"]
78+
CONTENT, $post->getContent());
79+
}
80+
81+
private function getPost(): Post
82+
{
83+
$author = $this->createUser('Contributor');
84+
85+
$post = (new Post())
86+
->setTitle('My blog post')
87+
->setCustomType('post')
88+
->setAuthor($author)
89+
->setStatus(PublishingStatusInterface::STATUS_PUBLISH)
90+
->setCreatedAt(new \DateTime('2013/12/18'))
91+
->setPublishedAt(new \DateTime('2015/05/13'))
92+
;
93+
94+
$this->entityManager->persist($post);
95+
$this->entityManager->flush();
96+
97+
return $post;
98+
}
99+
}

0 commit comments

Comments
 (0)