profiles->findAll(); if (0 === count($all)) { $io->warning('Aucun profil trouvé.'); return Command::SUCCESS; } // Promote first profile to ROLE_ADMIN if none exists $hasAdmin = false; foreach ($all as $profile) { if (in_array('ROLE_ADMIN', $profile->getRoles(), true)) { $hasAdmin = true; break; } } $isFirst = true; $count = 0; foreach ($all as $profile) { // Set password: first letter of firstName + "123" $firstLetter = mb_strtoupper(mb_substr($profile->getFirstName(), 0, 1)); $plain = $firstLetter.'123'; $hashed = $this->passwordHasher->hashPassword($profile, $plain); $profile->setPassword($hashed); // Set roles: first profile → ADMIN, others → VIEWER (minimum to use the app) if (!$hasAdmin && $isFirst) { $profile->setRoles(['ROLE_ADMIN']); $io->writeln(sprintf(' %s %s → mdp: %s — ROLE_ADMIN', $profile->getFirstName(), $profile->getLastName(), $plain)); $isFirst = false; } elseif (in_array('ROLE_USER', $profile->getRoles(), true) && !in_array('ROLE_VIEWER', $profile->getRoles(), true) && !in_array('ROLE_GESTIONNAIRE', $profile->getRoles(), true) && !in_array('ROLE_ADMIN', $profile->getRoles(), true)) { $profile->setRoles(['ROLE_VIEWER']); $io->writeln(sprintf(' %s %s → mdp: %s — ROLE_VIEWER', $profile->getFirstName(), $profile->getLastName(), $plain)); } else { $io->writeln(sprintf(' %s %s → mdp: %s — %s', $profile->getFirstName(), $profile->getLastName(), $plain, implode(', ', $profile->getRoles()))); } ++$count; } $this->em->flush(); $io->success(sprintf('%d mot(s) de passe initialisé(s).', $count)); return Command::SUCCESS; } }