66use CodedMonkey \Dirigent \Doctrine \Repository \UserRepository ;
77use CodedMonkey \Dirigent \Form \RegistrationFormType ;
88use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
9+ use Symfony \Bundle \SecurityBundle \Security ;
910use Symfony \Component \HttpFoundation \Request ;
1011use Symfony \Component \HttpFoundation \Response ;
1112use Symfony \Component \Routing \Attribute \Route ;
@@ -21,9 +22,7 @@ public function __construct(
2122 #[Route('/login ' , name: 'dashboard_login ' )]
2223 public function login (AuthenticationUtils $ authenticationUtils ): Response
2324 {
24- $ userCount = $ this ->userRepository ->count ([]);
25-
26- if (0 === $ userCount ) {
25+ if ($ this ->userRepository ->noUsers ()) {
2726 return $ this ->redirectToRoute ('dashboard_register ' );
2827 }
2928
@@ -38,12 +37,13 @@ public function login(AuthenticationUtils $authenticationUtils): Response
3837 }
3938
4039 #[Route('/register ' , name: 'dashboard_register ' )]
41- public function register (Request $ request ): Response
40+ public function register (Request $ request, Security $ security ): Response
4241 {
4342 $ registrationEnabled = $ this ->getParameter ('dirigent.security.registration_enabled ' );
44- $ userCount = $ this ->userRepository ->count ([] );
43+ $ noUsers = $ this ->userRepository ->noUsers ( );
4544
46- if (!$ registrationEnabled && 0 !== $ userCount ) {
45+ // Redirect to the homepage page if registration is disabled, but allow it if there are no users yet
46+ if (!$ registrationEnabled && !$ noUsers ) {
4747 return $ this ->redirectToRoute ('dashboard ' );
4848 }
4949
@@ -54,13 +54,14 @@ public function register(Request $request): Response
5454 $ form ->handleRequest ($ request );
5555
5656 if ($ form ->isSubmitted () && $ form ->isValid ()) {
57- if (0 === $ userCount ) {
57+ // The first user gets owner privileges
58+ if ($ noUsers ) {
5859 $ user ->setRoles (['ROLE_SUPER_ADMIN ' , 'ROLE_USER ' ]);
5960 }
6061
6162 $ this ->userRepository ->save ($ user , true );
6263
63- return $ this -> redirectToRoute ( ' dashboard_login ' );
64+ return $ security -> login ( $ user , ' security.authenticator.form_login.main ' , ' main ' );
6465 }
6566
6667 return $ this ->render ('dashboard/security/register.html.twig ' , [
0 commit comments