Bläddra i källkod

Ménage UserManager

François Drouhard 5 år sedan
förälder
incheckning
e5767b6c3f
3 ändrade filer med 43 tillägg och 26 borttagningar
  1. 9 21
      src/Controller/SecurityController.php
  2. 15 5
      src/Repository/UserRepository.php
  3. 19 0
      src/Service/UserManager.php

+ 9 - 21
src/Controller/SecurityController.php

@@ -14,8 +14,8 @@ use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\Routing\Annotation\Route;
 use App\Form\UserType;
 use App\Entity\User;
+use App\Repository\UserRepository;
 use Doctrine\ORM\EntityManagerInterface;
-use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
 use Symfony\Component\HttpFoundation\Response;
@@ -47,7 +47,7 @@ class SecurityController extends AbstractController
     /**
      * @Route("/admin/createuser", name="admin_createuser")
      */
-    public function createUserAction(Request $request, UserManager $userManager, UserPasswordEncoderInterface $encoder)
+    public function createUserAction(Request $request, UserManager $userManager)
     {
         $user = new User;
         $form = $this->createForm(UserType::class, $user);
@@ -85,10 +85,8 @@ class SecurityController extends AbstractController
     /**
      * @Route("/admin", name="admin_index")
      */
-    public function indexAction(Request $request)
+    public function indexAction(Request $request, UserRepository $repo)
     {
-        $em = $this->getDoctrine()->getManager();
-        $repo = $em->getRepository('App:User');
         $users = $repo->findAll();
 
         return $this->render('security/liste.html.twig', array(
@@ -99,13 +97,12 @@ class SecurityController extends AbstractController
     /**
      * @Route("/admin/deluser/{id}", name="admin_deluser")
      */
-    public function delUserAction (Request $request, User $user, EntityManagerInterface $em)
+    public function delUserAction (Request $request, User $user, UserManager $userManager)
     {
         $form = $this->get('form.factory')->create();
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid()) {
-            $em->remove($user);
-            $em->flush();
+            $userManager->removeUser($user);
             $this->addFlash("success", "L'utilisateur a bien été supprimé");
             return $this->redirectToRoute('admin_index');
         }
@@ -118,20 +115,14 @@ class SecurityController extends AbstractController
     /**
      * @Route("/register", name="security_register")
      */
-    public function registerAction(Request $request, UserPasswordEncoderInterface $encoder, UserManager $userManager, Mail $mail, EntityManagerInterface $em)
+    public function registerAction(Request $request, UserManager $userManager, Mail $mail)
     {
         $user = new User();
-        $user->setActivated(false);
         $form = $this->createForm(UserRegisterType::class, $user);
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())
         {
-            $user->setRoles(array('ROLE_USER'));
-            $encoded = $encoder->encodePassword($user, $user->getPassword());
-            $user->setPassword($encoded);
-            $userManager->generateToken($user);
-            $em->persist($user);
-            $em->flush();
+            $userManager->register($user);
 
             $adresse = $this->generateUrl('security_activate', array('token'=>$user->getToken()), UrlGeneratorInterface::ABSOLUTE_URL);
 
@@ -182,17 +173,14 @@ class SecurityController extends AbstractController
     /**
      * @Route("/resetpassword/token={token}", name="security_resetpassword")
      */
-    public function resetPasswordAction(Request $request, UserPasswordEncoderInterface $encoder, UserManager $userManager, EntityManagerInterface $em, User $user = null)
+    public function resetPasswordAction(Request $request, UserManager $userManager, User $user = null)
     {
         if ($user != null) {
             if ($user->isValidToken()) {
                 $form = $this->createForm(UserEditPasswordType::class, $user);
                 $form->handleRequest($request);
                 if ($form->isSubmitted() && $form->isValid()) {
-                    $encoded = $encoder->encodePassword($user, $user->getPassword());
-                    $user->setPassword($encoded);
-                    $userManager->generateToken($user);
-                    $em->flush();
+                    $userManager->resetPassword($user);
                     $this->addFlash('success', 'Mot de passe changé, veuillez vous connecter');
                     return $this->redirectToRoute('videotheque_liste');
                 }

+ 15 - 5
src/Repository/UserRepository.php

@@ -2,14 +2,24 @@
 
 namespace App\Repository;
 
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\Common\Persistence\ManagerRegistry;
+use App\Entity\User;
+
 /**
- * UserRepository
- *
- * This class was generated by the Doctrine ORM. Add your own custom
- * repository methods below.
+ * @method User|null find($id, $lockMode = null, $lockVersion = null)
+ * @method User|null findOneBy(array $criteria, array $orderBy = null)
+ * @method User[]    findAll()
+ * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  */
-class UserRepository extends \Doctrine\ORM\EntityRepository
+
+class UserRepository extends ServiceEntityRepository
 {
+    public function __construct(ManagerRegistry $registry)
+    {
+        parent::__construct($registry, User::class);
+    }
+
     public function findUser($userId)
     {
         $qb = $this

+ 19 - 0
src/Service/UserManager.php

@@ -39,6 +39,19 @@ class UserManager
         $this->enregistrerUser($user);
     }
 
+    public function register(User $user)
+    {
+        $user->setActivated(false);
+        $user->setRoles(array('ROLE_USER'));
+        $this->enregistrerUser($user);
+    }
+
+
+    public function resetPassword(User $user)
+    {
+        $user->setActivated($user);
+        $this->enregistrerUser($user);
+    }
     public function enregistrerUser (User $user)
     {
         $encoded = $this->passwordEncoder->encodePassword($user, $user->getPassword());
@@ -53,6 +66,12 @@ class UserManager
         $this->em->flush();
     }
 
+    public function removeUser(User $user)
+    {
+        $this->em->remove($user);
+        $this->em->flush();
+    }
+
     public function generateToken(User $user)
     {
         $user->setToken($this->tokenGenerator->generateToken());