8000 minor #1404 cleanup: use roles constants (COil) · symfony/demo@04c1798 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 04c1798

Browse files
committed
minor #1404 cleanup: use roles constants (COil)
This PR was squashed before being merged into the main branch. Discussion ---------- cleanup: use roles constants Before we had to hard-code roles strings: ``` use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; class PostController extends Controller { /** * `@IsGranted`("ROLE_ADMIN") * * or use `@Security` for more flexibility: * * `@Security`("is_granted('ROLE_ADMIN') and is_granted('ROLE_FRIENDLY_USER')") */ public function index() { // ... } } ``` But with attributes, can use constants. I find this cleaner. I have already used this on several projects and I didn't find drawbacks. Commits ------- f057af8 cleanup: use roles constants
2 parents ee044c7 + f057af8 commit 04c1798

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 <javier.eguiluz@gmail.com>
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 <monteil.romain@gmail.com>
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', 'jane_admin@symfony.com', ['ROLE_ADMIN']],
107-
['Tom Doe', 'tom_admin', 'kitten', 'tom_admin@symfony.com', ['ROLE_ADMIN']],
108-
['John Doe', 'john_user', 'kitten', 'john_user@symfony.com', ['ROLE_USER']],
106+
['Jane Doe', 'jane_admin', 'kitten', 'jane_admin@symfony.com', [User::ROLE_ADMIN]],
107+
['Tom Doe', 'tom_admin', 'kitten', 'tom_admin@symfony.com', [User::ROLE_ADMIN]],
108+
['John Doe', 'john_user', 'kitten', 'john_user@symfony.com', [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)
0