Skip to content

Commit f057af8

Browse files
COiljaviereguiluz
authored andcommitted
cleanup: use roles constants
1 parent ee044c7 commit f057af8

File tree

7 files changed

+20
-13
lines changed

7 files changed

+20
-13
lines changed

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/packages/security.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ security:
6969
# additional security lives in the controllers
7070
- { path: '^/(%app_locales%)/admin', roles: ROLE_ADMIN }
7171

72+
# The ROLE_ADMIN role inherits from the ROLE_USER role
7273
role_hierarchy:
7374
ROLE_ADMIN: ROLE_USER
7475

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 to find usages all over the application rather
36+
// than doing a full-text search on the "ROLE_" string.
37+
// It also prevents from making typo errors.
38+
final public const ROLE_USER = 'ROLE_USER';
39+
final 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);

0 commit comments

Comments
 (0)