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

Make CsrfTokenManager public #487

Merged
merged 3 commits into from
Oct 18, 2022
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
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ jobs:
fail-fast: false
matrix:
php: ["8.0", "8.1"]
symfony: ["^5.4", "^6.0"]
symfony: ["^5.4", "~6.0.0", "~6.1.0"]
twig: ["^2.12", "^3.0"]
exclude:
-
php: "8.0"
symfony: "~6.1.0"

steps:
-
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* TODO Remove on sylius/resource-bundle 2.0
*/
final class CsrfTokenManagerPass implements CompilerPassInterface
Copy link
Member

Choose a reason for hiding this comment

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

It would be just the art for the sake of art, but we could try to cover this pass with configuration test as well :P

{
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('security.csrf.token_manager')) {
return;
}

$csrdTokenManagerDefinition = $container->getDefinition('security.csrf.token_manager');
$csrdTokenManagerDefinition->setPublic(true);
}
}
14 changes: 8 additions & 6 deletions src/Bundle/SyliusResourceBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\Bundle\ResourceBundle;

use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\CsrfTokenManagerPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\DoctrineContainerRepositoryFactoryPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\DoctrineTargetEntitiesResolverPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\Helper\TargetEntitiesResolver;
Expand Down Expand Up @@ -41,15 +42,16 @@ public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(new WinzouStateMachinePass());
$container->addCompilerPass(new RegisterStateMachinePass());
$container->addCompilerPass(new RegisterResourcesPass());
$container->addCompilerPass(new RegisterFqcnControllersPass());
$container->addCompilerPass(new DoctrineTargetEntitiesResolverPass(new TargetEntitiesResolver()), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
$container->addCompilerPass(new CsrfTokenManagerPass());
$container->addCompilerPass(new DoctrineContainerRepositoryFactoryPass());
$container->addCompilerPass(new RegisterResourceRepositoryPass());
$container->addCompilerPass(new DoctrineTargetEntitiesResolverPass(new TargetEntitiesResolver()), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
$container->addCompilerPass(new RegisterFormBuilderPass());
$container->addCompilerPass(new RegisterFqcnControllersPass());
$container->addCompilerPass(new RegisterResourceRepositoryPass());
$container->addCompilerPass(new RegisterResourcesPass());
$container->addCompilerPass(new RegisterStateMachinePass());
$container->addCompilerPass(new TwigPass());
$container->addCompilerPass(new WinzouStateMachinePass());

$container->registerExtension(new PagerfantaExtension(true));
$container->addCompilerPass(new PagerfantaBridgePass(true), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1); // Should run after all passes from BabDevPagerfantaBundle
Expand Down
24 changes: 20 additions & 4 deletions src/Bundle/test/src/Tests/Controller/ScienceBookUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public function it_allows_indexing_books(): void
$content = $response->getContent();
$this->assertStringContainsString('<h1>Books</h1>', $content);
$this->assertStringContainsString(
sprintf('<tr><td>%d</td><td>A Brief History of Time</td><td>Stephen Hawking</td></tr>', $scienceBooks['science-book1']->getId()),
sprintf('<td>%d</td><td>A Brief History of Time</td><td>Stephen Hawking</td>', $scienceBooks['science-book1']->getId()),
$content,
);
$this->assertStringContainsString(
sprintf('<tr><td>%d</td><td>The Future of Humanity</td><td>Michio Kaku</td></tr>', $scienceBooks['science-book2']->getId()),
sprintf('<td>%d</td><td>The Future of Humanity</td><td>Michio Kaku</td>', $scienceBooks['science-book2']->getId()),
$content,
);
}
Expand Down Expand Up @@ -111,6 +111,22 @@ public function it_allows_updating_a_book(): void
$this->assertSame($newBookAuthorLastName, $book->getAuthorLastName());
}

/** @test */
public function it_allows_deleting_a_book(): void
{
$this->loadFixturesFromFile('single_science_book.yml');

$this->client->request('GET', '/science-books/');
$this->client->submitForm('Delete');

$this->assertResponseRedirects(null, expectedCode: Response::HTTP_FOUND);

/** @var ScienceBook[] $books */
$books = static::getContainer()->get('app.repository.science_book')->findAll();

$this->assertEmpty($books);
}

/** @test */
public function it_allows_filtering_books(): void
{
Expand All @@ -123,11 +139,11 @@ public function it_allows_filtering_books(): void
$content = $response->getContent();
$this->assertStringContainsString('<h1>Books</h1>', $content);
$this->assertStringContainsString(
sprintf('<tr><td>%d</td><td>A Brief History of Time</td><td>Stephen Hawking</td></tr>', $scienceBooks['science-book1']->getId()),
sprintf('<td>%d</td><td>A Brief History of Time</td><td>Stephen Hawking</td>', $scienceBooks['science-book1']->getId()),
$content,
);
$this->assertStringNotContainsString(
sprintf('<tr><td>%d</td><td>The Future of Humanity</td><td>Michio Kaku</td></tr>', $scienceBooks['science-book2']->getId()),
sprintf('<td>%d</td><td>The Future of Humanity</td><td>Michio Kaku</td>', $scienceBooks['science-book2']->getId()),
$content,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
App\Entity\ScienceBook:
science-book1:
title: "A Brief History of Time"
author: "@stephen-hawking"

App\Entity\Author:
stephen-hawking:
firstName: "Stephen"
lastName: "Hawking"
11 changes: 10 additions & 1 deletion src/Bundle/test/templates/ScienceBook/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
<td>ID</td>
<td>Title</td>
<td>Author</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
{% for book in resources.data %}
<tr><td>{{ book.id }}</td><td>{{ book.title }}</td><td>{{ book.authorFirstName }} {{ book.authorLastName }}</td></tr>
<tr><td>{{ book.id }}</td><td>{{ book.title }}</td><td>{{ book.authorFirstName }} {{ book.authorLastName }}</td>
<td>
<form action="{{ path('app_science_book_delete', {'id': book.id}) }}" method="POST">
<input type="hidden" name="_method" value="DELETE"/>
<input type="hidden" name="_csrf_token" value="{{ csrf_token(book.id) }}" />
<button type="submit">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>