Skip to content

Commit 1173784

Browse files
committed
feat: show Profile in secured action
1 parent a365a7f commit 1173784

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

config/packages/security.yaml

+2-9
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,10 @@ security:
2727
# Note: Only the *first* access control that matches will be used
2828
access_control:
2929
# - { path: ^/admin, roles: ROLE_ADMIN }
30-
# - { path: ^/profile, roles: ROLE_USER }
30+
- { path: ^/profile, roles: ROLE_USER }
3131

3232
when@test:
3333
security:
3434
password_hashers:
35-
# By default, password hashers are resource intensive and take time. This is
36-
# important to generate secure password hashes. In tests however, secure hashes
37-
# are not important, waste resources and increase test times. The following
38-
# reduces the work factor to the lowest possible values.
3935
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
40-
algorithm: auto
41-
cost: 4 # Lowest possible value for bcrypt
42-
time_cost: 3 # Lowest possible value for argon
43-
memory_cost: 10 # Lowest possible value for argon
36+
algorithm: plaintext

docker-compose.override.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ services:
44
###> doctrine/doctrine-bundle ###
55
database:
66
ports:
7-
- "5432"
7+
- "5432:5432"
88
###< doctrine/doctrine-bundle ###

src/Controller/ProfileController.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller;
6+
7+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8+
use Symfony\Component\HttpFoundation\Response;
9+
use Symfony\Component\Routing\Annotation\Route;
10+
11+
class ProfileController extends AbstractController
12+
{
13+
#[Route("/profile", name: 'profile_index')]
14+
public function index(): Response
15+
{
16+
return $this->render('profile/index.html.twig');
17+
}
18+
}

templates/profile/index.html.twig

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block body %}
4+
This is profile page for {{ app.user.email }}.
5+
{% endblock %}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Controller;
6+
7+
use App\Entity\User;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
10+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
11+
use Zenstruck\Foundry\Test\ResetDatabase;
12+
13+
class ProfileControllerTest extends WebTestCase
14+
{
15+
use ResetDatabase;
16+
17+
private KernelBrowser $client;
18+
private User $user;
19+
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
$this->user = new User();
25+
$this->user->setEmail('[email protected]');
26+
$this->user->setPassword('test');
27+
$this->user->setRoles(['ROLE_USER']);
28+
29+
$this->client = static::createClient();
30+
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
31+
$em->persist($this->user);
32+
$em->flush();
33+
}
34+
35+
public function test(): void
36+
{
37+
$this->client->loginUser($this->user);
38+
$crawler = $this->client->request('GET', '/profile');
39+
$this->assertTrue($this->client->getResponse()->isOk());
40+
$this->assertTrue($crawler->filter('body:contains("[email protected]")')->count() === 1);
41+
}
42+
43+
public function testNotAuthenticated(): void
44+
{
45+
$this->client->request('GET', '/profile');
46+
$this->assertEquals(401, $this->client->getResponse()->getStatusCode());
47+
}
48+
}

0 commit comments

Comments
 (0)