44
55namespace App \Command ;
66
7+ use App \Entity \Group ;
78use App \Entity \User ;
89use Doctrine \ORM \EntityManagerInterface ;
910use Random \RandomException ;
@@ -61,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6162 $ io ->title ("Creating users from $ filename " );
6263
6364 /**
64- * @var array{email: string, name: string, roles: list<string>}[] $users
65+ * @var array{email: string, name: string, roles: list<string>, group: string|null }[] $users
6566 */
6667 $ users = $ io ->progressIterate (self ::parseUsers ($ filename ));
6768
@@ -75,7 +76,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7576 foreach ($ users as $ user ) {
7677 $ password = self ::generateRandomPassword ();
7778
78- $ user = (new User ())->setName ($ user ['name ' ])->setEmail ($ user ['email ' ])->setRoles ($ user ['roles ' ]);
79+ /**
80+ * @var Group|null $group
81+ */
82+ $ group = null ;
83+
84+ if (null !== $ user ['group ' ]) {
85+ $ group = $ this ->entityManager ->getRepository (Group::class)->findOneBy (['name ' => $ user ['group ' ]]);
86+ if (null === $ group ) {
87+ $ io ->warning ("Group {$ user ['group ' ]} not found for user {$ user ['email ' ]}. Skipping. " );
88+ continue ;
89+ }
90+ }
91+
92+ $ user = (new User ())
93+ ->setName ($ user ['name ' ])
94+ ->setEmail ($ user ['email ' ])
95+ ->setRoles ($ user ['roles ' ])
96+ ->setGroup ($ group );
97+
7998 $ hashedPassword = $ this ->passwordHasher ->hashPassword ($ user , $ password );
8099 $ user ->setPassword ($ hashedPassword );
81100
@@ -102,7 +121,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
102121 }
103122
104123 /**
105- * @return array{email: string, name: string, roles: list<string>}[]
124+ * @return array{email: string, name: string, roles: list<string>, group: string|null }[]
106125 */
107126 private static function parseUsers (string $ filename ): array
108127 {
@@ -119,6 +138,7 @@ private static function parseUsers(string $filename): array
119138 $ emailIndex = array_search ('email ' , $ header , true );
120139 $ nameIndex = array_search ('name ' , $ header , true );
121140 $ rolesIndex = array_search ('roles ' , $ header , true );
141+ $ groupIndex = array_search ('group ' , $ header , true );
122142
123143 if (!\is_int ($ emailIndex ) || !\is_int ($ nameIndex ) || !\is_int ($ rolesIndex )) {
124144 throw new \RuntimeException ("Could not find email, name, or roles in the header of $ filename. " );
@@ -129,15 +149,17 @@ private static function parseUsers(string $filename): array
129149 $ email = $ row [$ emailIndex ];
130150 $ name = $ row [$ nameIndex ];
131151 $ roles = $ row [$ rolesIndex ];
152+ $ group = false !== $ groupIndex ? $ row [$ groupIndex ] : null ;
132153
133- if (!\is_string ($ email ) || !\is_string ($ name ) || !\is_string ($ roles )) {
154+ if (!\is_string ($ email ) || !\is_string ($ name ) || !\is_string ($ roles ) || (! \is_string ( $ group ) && null !== $ group ) ) {
134155 throw new \RuntimeException ("Invalid row in $ filename. " );
135156 }
136157
137158 $ users [] = [
138159 'email ' => $ email ,
139160 'name ' => $ name ,
140161 'roles ' => explode (', ' , $ roles ),
162+ 'group ' => $ group ,
141163 ];
142164 }
143165
0 commit comments