3
3
namespace Ggbb \SymfonyUserPermission \Command ;
4
4
5
5
use Doctrine \ORM \EntityManagerInterface ;
6
+ use Ggbb \SymfonyUserPermission \Entity \Interface \UserRoleFieldInterface ;
6
7
use Ggbb \SymfonyUserPermission \Entity \Interface \UserRoleInterface ;
7
8
use Ggbb \SymfonyUserPermission \GgbbUserPermissionBundle ;
8
9
use Symfony \Component \Console \Attribute \AsCommand ;
9
10
use Symfony \Component \Console \Command \Command ;
10
11
use Symfony \Component \Console \Input \InputInterface ;
11
12
use Symfony \Component \Console \Output \OutputInterface ;
13
+ use Symfony \Component \Console \Style \SymfonyStyle ;
12
14
use Symfony \Component \DependencyInjection \ParameterBag \ContainerBagInterface ;
15
+ use Symfony \Component \Security \Core \User \UserInterface ;
13
16
14
17
#[AsCommand(
15
18
name: 'permission:create-default-user-role ' ,
18
21
class CreateDefaultUserRoleCommand extends Command
19
22
{
20
23
const USER_ROLES = [
21
- 'ROLE_ADMIN ' ,
22
24
'ROLE_USER ' ,
25
+ 'ROLE_ADMIN ' ,
23
26
];
24
27
25
28
public function __construct (
@@ -32,16 +35,44 @@ public function __construct(
32
35
33
36
protected function execute (InputInterface $ input , OutputInterface $ output ): int
34
37
{
35
- if (!$ this ->container ->has (GgbbUserPermissionBundle::CONFIG_USER_ROLE_REPOSITORY )) {
36
- @trigger_error ('Config user_role_repository not found ' );
38
+ $ io = new SymfonyStyle ($ input , $ output );
39
+ if (!$ this ->container ->has (GgbbUserPermissionBundle::CONFIG_USER )) {
40
+ @trigger_error ('Config user not found ' );
41
+ }
42
+ if (!$ this ->container ->has (GgbbUserPermissionBundle::CONFIG_USER_ROLE )) {
43
+ @trigger_error ('Config user_role not found ' );
37
44
}
38
45
39
- $ userRoleName = $ this ->container ->get (GgbbUserPermissionBundle::CONFIG_USER_ROLE_REPOSITORY );
46
+ $ defaultUserRole = null ;
47
+ $ userRoleName = $ this ->container ->get (GgbbUserPermissionBundle::CONFIG_USER_ROLE );
40
48
foreach (self ::USER_ROLES as $ role ) {
41
49
/** @var UserRoleInterface $userRole */
42
50
$ userRole = new $ userRoleName ();
43
51
$ userRole ->setRole ($ role );
44
52
$ this ->entityManager ->persist ($ userRole );
53
+ $ io ->info ([
54
+ "Add role {$ role }" ,
55
+ ]);
56
+
57
+ if ($ userRole ->getRole () === self ::USER_ROLES [0 ]) {
58
+ $ defaultUserRole = $ userRole ;
59
+ }
60
+ }
61
+
62
+ if (!$ defaultUserRole ) {
63
+ @trigger_error ('Default user_role not found ' );
64
+ }
65
+
66
+ $ userName = $ this ->container ->get (GgbbUserPermissionBundle::CONFIG_USER );
67
+ $ userRepository = $ this ->entityManager ->getRepository ($ userName );
68
+ $ users = $ userRepository ->findBy (['userRole ' => null ]);
69
+ /** @var UserRoleFieldInterface|UserInterface $user */
70
+ foreach ($ users as $ user ) {
71
+ $ user ->setUserRole ($ defaultUserRole );
72
+ $ this ->entityManager ->persist ($ user );
73
+ $ io ->info ([
74
+ "Add role to user {$ user ->getUserIdentifier ()}" ,
75
+ ]);
45
76
}
46
77
47
78
try {
@@ -52,6 +83,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
52
83
return Command::INVALID ;
53
84
}
54
85
86
+ $ io ->success ([
87
+ "It's ok " ,
88
+ ]);
55
89
return Command::SUCCESS ;
56
90
}
57
91
}
0 commit comments