Bläddra i källkod

Commande pour modifier utilisateur

Sangfroid 1 månad sedan
förälder
incheckning
85610a091a
1 ändrade filer med 67 tillägg och 0 borttagningar
  1. 67 0
      src/Command/BlogUserUpdateCommand.php

+ 67 - 0
src/Command/BlogUserUpdateCommand.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace App\Command;
+
+use App\Entity\User;
+use App\Repository\UserRepository;
+use Symfony\Component\Console\Attribute\AsCommand;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Question\Question;
+use Symfony\Component\Console\Style\SymfonyStyle;
+use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
+
+#[AsCommand(
+    name: 'blog:user:update',
+    description: 'Mettre à jour les informations utilisateur',
+)]
+class BlogUserUpdateCommand extends Command
+{
+    public function __construct(
+        protected readonly UserRepository $userRepository,
+        protected UserPasswordHasherInterface $passwordHasher
+    )
+    {
+        parent::__construct();
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $io = new SymfonyStyle($input, $output);
+
+        $users = $this->userRepository->findAll();
+        $question = (new Question("Choisir l'utilisateur", null))
+            ->setAutocompleterValues(array_map(fn($user): string => $user->getUserIdentifier(), $users))
+        ;
+        $username = $io->askQuestion($question);
+
+        $user = $this->userRepository->findOneBy(['username' => $username]);
+
+        if ($user instanceof User) {
+            if ($io->confirm('Voulez-vous changer le pseudo', true)) {
+                $user->setPseudo($io->ask('Nouveau pseudo'), $user->getPseudo());            
+            }
+
+            if ($io->confirm('Voulez-vous changer l\'adresse mail', true)) {
+                $user->setEmail($io->ask('Nouvel email', $user->getEmail()));
+            }
+
+            if ($io->confirm('Voulez-vous changer de mot le passe', false)) {
+                do {
+                    $password = $io->askHidden('Mot de passe');
+                    $confirm = $io->askHidden('Confirmez le mot de passe');
+                } while ($password !== $confirm);
+                $hashedPassword = $this->passwordHasher->hashPassword($user, $password);
+                $this->userRepository->upgradePassword($user, $hashedPassword);
+            }
+        }
+        $this->userRepository->add($user);
+        
+        $io->success('Les informations d\'utilisateur ont été mises à jour');
+
+        return Command::SUCCESS;
+    }
+}