Skip to content

Commit 6da7f01

Browse files
committed
cleanup: use roles constants
1 parent 84ae312 commit 6da7f01

File tree

9 files changed

+25
-12
lines changed

9 files changed

+25
-12
lines changed

config/packages/security.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ security:
6767
access_control:
6868
# this is a catch-all for the admin area
6969
# additional security lives in the controllers
70-
- { path: '^/(%app_locales%)/admin', roles: ROLE_ADMIN }
70+
- { path: '^/(%app_locales%)/admin', roles: !php/const App\Entity\User::ROLE_ADMIN }
7171

72+
# The ROLE_ADMIN role inherits from the ROLE_USER role
7273
role_hierarchy:
73-
ROLE_ADMIN: ROLE_USER
74+
!php/const App\Entity\User::ROLE_ADMIN: !php/const App\Entity\User::ROLE_USER
7475

7576
when@test:
7677
# this configuration simplifies testing URLs protected by the security mechanism

config/packages/twig.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ twig:
44
- 'form/layout.html.twig'
55
- 'form/fields.html.twig'
66

7+
# These variables are now available in all Twig templates
8+
globals:
9+
# We can use PHP constants from the application thanks to the !php/const sintaxis.
10+
ROLE_USER: !php/const App\Entity\User::ROLE_USER
11+
ROLE_ADMIN: !php/const App\Entity\User::ROLE_ADMIN
12+
713
when@test:
814
twig:
915
strict_variables: true

src/Command/AddUserCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
189189
$user->setFullName($fullName);
190190
$user->setUsername($username);
191191
$user->setEmail($email);
192-
$user->setRoles([$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER']);
192+
$user->setRoles([$isAdmin ? User::ROLE_ADMIN : User::ROLE_USER]);
193193

194194
// See https://symfony.com/doc/5.4/security.html#registering-the-user-hashing-passwords
195195
$hashedPassword = $this->passwordHasher->hashPassword($user, $plainPassword);

src/Controller/Admin/BlogController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* @author Javier Eguiluz <[email protected]>
3939
*/
4040
#[Route('/admin/post')]
41-
#[IsGranted('ROLE_ADMIN')]
41+
#[IsGranted(User::ROLE_ADMIN)]
4242
class BlogController extends AbstractController
4343
{
4444
/**

src/Controller/UserController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* @author Romain Monteil <[email protected]>
3333
*/
34-
#[Route('/profile'), IsGranted('ROLE_USER')]
34+
#[Route('/profile'), IsGranted(User::ROLE_USER)]
3535
class UserController extends AbstractController
3636
{
3737
#[Route('/edit', name: 'user_edit', methods: ['GET', 'POST'])]

src/DataFixtures/AppFixtures.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ private function getUserData(): array
103103
{
104104
return [
105105
// $userData = [$fullname, $username, $password, $email, $roles];
106-
['Jane Doe', 'jane_admin', 'kitten', '[email protected]', ['ROLE_ADMIN']],
107-
['Tom Doe', 'tom_admin', 'kitten', '[email protected]', ['ROLE_ADMIN']],
108-
['John Doe', 'john_user', 'kitten', '[email protected]', ['ROLE_USER']],
106+
['Jane Doe', 'jane_admin', 'kitten', '[email protected]', [User::ROLE_ADMIN]],
107+
['Tom Doe', 'tom_admin', 'kitten', '[email protected]', [User::ROLE_ADMIN]],
108+
['John Doe', 'john_user', 'kitten', '[email protected]', [User::ROLE_USER]],
109109
];
110110
}
111111

src/Entity/User.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
#[ORM\Table(name: 'symfony_demo_user')]
3333
class User implements UserInterface, PasswordAuthenticatedUserInterface
3434
{
35+
// we can use constants for roles so we get all usages all over the application
36+
// more easyly than doing a search on the "ROLE_" string. It also prevents from
37+
// doing typo errors.
38+
public const ROLE_USER = 'ROLE_USER';
39+
public const ROLE_ADMIN = 'ROLE_ADMIN';
40+
3541
#[ORM\Id]
3642
#[ORM\GeneratedValue]
3743
#[ORM\Column(type: Types::INTEGER)]
@@ -118,7 +124,7 @@ public function getRoles(): array
118124

119125
// guarantees that a user always has at least one role for security
120126
if (empty($roles)) {
121-
$roles[] = 'ROLE_USER';
127+
$roles[] = self::ROLE_USER;
122128
}
123129

124130
return array_unique($roles);

templates/base.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
</a>
5656
</li>
5757

58-
{% if is_granted('ROLE_ADMIN') %}
58+
{% if is_granted(ROLE_ADMIN) %}
5959
<li>
6060
<a href="{{ path('admin_post_index') }}">
6161
<i class="fa fa-lock" aria-hidden="true"></i> {{ 'menu.admin'|trans }}

templates/security/login.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@
6767
<tr>
6868
<td>john_user</td>
6969
<td>kitten</td>
70-
<td><code>ROLE_USER</code> ({{ 'help.role_user'|trans }})</td>
70+
<td><code>{{ ROLE_USER }}</code> ({{ 'help.role_user'|trans }})</td>
7171
<td>
7272
<button class="btn btn-primary btn-sm" {{ stimulus_action('login', 'prefillJohnUser') }}><i class="fa fa-user"></i></button>
7373
</td>
7474
</tr>
7575
<tr>
7676
<td>jane_admin</td>
7777
<td>kitten</td>
78-
<td><code>ROLE_ADMIN</code> ({{ 'help.role_admin'|trans }})</td>
78+
<td><code>{{ ROLE_ADMIN }}</code> ({{ 'help.role_admin'|trans }})</td>
7979
<td>
8080
<button class="btn btn-primary btn-sm" {{ stimulus_action('login', 'prefillJaneAdmin') }}><i class="fa fa-user"></i></button>
8181
</td>

0 commit comments

Comments
 (0)