浏览代码

Ménage userManager

François 5 年之前
父节点
当前提交
0418fcc247
共有 2 个文件被更改,包括 14 次插入9 次删除
  1. 2 8
      src/Controller/SecurityController.php
  2. 12 1
      src/Service/UserManager.php

+ 2 - 8
src/Controller/SecurityController.php

@@ -55,12 +55,7 @@ class SecurityController extends AbstractController
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())
         {
-            $encoded = $encoder->encodePassword($user, $user->getPassword());
-            $user->setPassword($encoded);
-            $userManager->generateToken($user);
-            $em = $this->getDoctrine()->getManager();
-            $em->persist($user);
-            $em->flush();
+            $userManager->enregistrerUser($user);
             $this->addFlash('success', 'L\'utilisateur a été ajouté');
             return $this->redirectToRoute('admin_index');
         }
@@ -72,13 +67,12 @@ class SecurityController extends AbstractController
     /**
      * @Route("/admin/edituser/{id}", name="admin_edituser")
      */
-    public function editUserAction(Request $request, UserPasswordEncoderInterface $encoder, User $user)
+    public function editUserAction(Request $request, User $user, EntityManagerInterface $em)
     {
         $form = $this->createForm(UserEditType::class, $user);
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())
         {
-            $em = $this->getDoctrine()->getManager();
             $em->flush();
             $this->addFlash('success', 'L\'utilisateur a été modifié');
             return $this->redirectToRoute('admin_index');

+ 12 - 1
src/Service/UserManager.php

@@ -35,13 +35,24 @@ class UserManager
         $user->setMail($mail);
         $user->setRoles($roles);
         $user->setActivated($activated);
-        $encoded = $this->passwordEncoder->encodePassword($user, $password);
+        $user->setPassword($password);
+        $this->enregistrerUser($user);
+    }
+
+    public function enregistrerUser (User $user)
+    {
+        $encoded = $this->passwordEncoder->encodePassword($user, $user->getPassword());
         $user->setPassword($encoded);
         $this->generateToken($user);
         $this->em->persist($user);
         $this->em->flush();
     }
 
+    public function editUser ()
+    {
+        $this->em->flush();
+    }
+
     public function generateToken(User $user)
     {
         $user->setToken($this->tokenGenerator->generateToken());