@@ -322,6 +322,7 @@ public function editAction(Request $request, int $teamId): Response
322
322
$ form ->handleRequest ($ request );
323
323
324
324
if ($ form ->isSubmitted () && $ form ->isValid ()) {
325
+ $ this ->possiblyAddUser ($ team );
325
326
$ this ->assetUpdater ->updateAssets ($ team );
326
327
$ this ->saveEntity ($ this ->em , $ this ->eventLogService , $ this ->dj , $ team ,
327
328
$ team ->getTeamid (), false );
@@ -358,21 +359,7 @@ public function addAction(Request $request): Response
358
359
$ form ->handleRequest ($ request );
359
360
360
361
if ($ form ->isSubmitted () && $ form ->isValid ()) {
361
- /** @var User $user */
362
- $ user = $ team ->getUsers ()->first ();
363
- if ($ team ->getAddUserForTeam () === Team::CREATE_NEW_USER ) {
364
- // Create a user for the team.
365
- $ user = new User ();
366
- $ user ->setUsername ($ team ->getNewUsername ());
367
- $ team ->addUser ($ user );
368
- // Make sure the user has the team role to make validation work.
369
- $ role = $ this ->em ->getRepository (Role::class)->findOneBy (['dj_role ' => 'team ' ]);
370
- $ user ->addUserRole ($ role );
371
- // Set the user's name to the team name when creating a new user.
372
- $ user ->setName ($ team ->getEffectiveName ());
373
- } elseif ($ team ->getAddUserForTeam () === Team::ADD_EXISTING_USER ) {
374
- $ team ->addUser ($ team ->getExistingUser ());
375
- }
362
+ $ this ->possiblyAddUser ($ team );
376
363
$ this ->em ->persist ($ team );
377
364
$ this ->assetUpdater ->updateAssets ($ team );
378
365
$ this ->saveEntity ($ this ->em , $ this ->eventLogService , $ this ->dj , $ team , null , true );
@@ -384,4 +371,28 @@ public function addAction(Request $request): Response
384
371
'form ' => $ form ,
385
372
]);
386
373
}
374
+
375
+ /**
376
+ * Add an existing or new user to a team if configured to do so
377
+ */
378
+ protected function possiblyAddUser (Team $ team ): void
379
+ {
380
+ if ($ team ->getAddUserForTeam () === Team::CREATE_NEW_USER ) {
381
+ // Create a user for the team.
382
+ $ user = new User ();
383
+ $ user ->setUsername ($ team ->getNewUsername ());
384
+ // Set the external ID if we need to do so.
385
+ if ($ this ->eventLogService ->externalIdFieldForEntity (User::class)) {
386
+ $ user ->setExternalid ($ team ->getNewUsername ());
387
+ }
388
+ $ team ->addUser ($ user );
389
+ // Make sure the user has the team role to make validation work.
390
+ $ role = $ this ->em ->getRepository (Role::class)->findOneBy (['dj_role ' => 'team ' ]);
391
+ $ user ->addUserRole ($ role );
392
+ // Set the user's name to the team name when creating a new user.
393
+ $ user ->setName ($ team ->getEffectiveName ());
394
+ } elseif ($ team ->getAddUserForTeam () === Team::ADD_EXISTING_USER ) {
395
+ $ team ->addUser ($ team ->getExistingUser ());
396
+ }
397
+ }
387
398
}
0 commit comments