]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Controller/ManageController.php
92ee2b4162649fad67e3158ca91c105d13e58ff4
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Controller / ManageController.php
1 <?php
2
3 namespace Wallabag\UserBundle\Controller;
4
5 use FOS\UserBundle\Event\UserEvent;
6 use FOS\UserBundle\FOSUserEvents;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
10 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11 use Wallabag\UserBundle\Entity\User;
12 use Wallabag\CoreBundle\Entity\Config;
13
14 /**
15 * User controller.
16 */
17 class ManageController extends Controller
18 {
19 /**
20 * Lists all User entities.
21 *
22 * @Route("/", name="user_index")
23 * @Method("GET")
24 */
25 public function indexAction()
26 {
27 $em = $this->getDoctrine()->getManager();
28
29 $users = $em->getRepository('WallabagUserBundle:User')->findAll();
30
31 return $this->render('WallabagUserBundle:Manage:index.html.twig', array(
32 'users' => $users,
33 ));
34 }
35
36 /**
37 * Creates a new User entity.
38 *
39 * @Route("/new", name="user_new")
40 * @Method({"GET", "POST"})
41 */
42 public function newAction(Request $request)
43 {
44 $userManager = $this->container->get('fos_user.user_manager');
45
46 $user = $userManager->createUser();
47 // enable created user by default
48 $user->setEnabled(true);
49
50 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
51 'validation_groups' => ['Profile'],
52 ]);
53 $form->handleRequest($request);
54
55 if ($form->isSubmitted() && $form->isValid()) {
56 $userManager->updateUser($user);
57
58 // dispatch a created event so the associated config will be created
59 $event = new UserEvent($user, $request);
60 $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
61
62 $this->get('session')->getFlashBag()->add(
63 'notice',
64 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
65 );
66
67 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
68 }
69
70 return $this->render('WallabagUserBundle:Manage:new.html.twig', array(
71 'user' => $user,
72 'form' => $form->createView(),
73 ));
74 }
75
76 /**
77 * Displays a form to edit an existing User entity.
78 *
79 * @Route("/{id}/edit", name="user_edit")
80 * @Method({"GET", "POST"})
81 */
82 public function editAction(Request $request, User $user)
83 {
84 $deleteForm = $this->createDeleteForm($user);
85 $editForm = $this->createForm('Wallabag\UserBundle\Form\UserType', $user);
86 $editForm->handleRequest($request);
87
88 if ($editForm->isSubmitted() && $editForm->isValid()) {
89 $em = $this->getDoctrine()->getManager();
90 $em->persist($user);
91 $em->flush();
92
93 $this->get('session')->getFlashBag()->add(
94 'notice',
95 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
96 );
97
98 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
99 }
100
101 return $this->render('WallabagUserBundle:Manage:edit.html.twig', array(
102 'user' => $user,
103 'edit_form' => $editForm->createView(),
104 'delete_form' => $deleteForm->createView(),
105 'twofactor_auth' => $this->getParameter('twofactor_auth'),
106 ));
107 }
108
109 /**
110 * Deletes a User entity.
111 *
112 * @Route("/{id}", name="user_delete")
113 * @Method("DELETE")
114 */
115 public function deleteAction(Request $request, User $user)
116 {
117 $form = $this->createDeleteForm($user);
118 $form->handleRequest($request);
119
120 if ($form->isSubmitted() && $form->isValid()) {
121 $this->get('session')->getFlashBag()->add(
122 'notice',
123 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
124 );
125
126 $em = $this->getDoctrine()->getManager();
127 $em->remove($user);
128 $em->flush();
129 }
130
131 return $this->redirectToRoute('user_index');
132 }
133
134 /**
135 * Creates a form to delete a User entity.
136 *
137 * @param User $user The User entity
138 *
139 * @return \Symfony\Component\Form\Form The form
140 */
141 private function createDeleteForm(User $user)
142 {
143 return $this->createFormBuilder()
144 ->setAction($this->generateUrl('user_delete', array('id' => $user->getId())))
145 ->setMethod('DELETE')
146 ->getForm()
147 ;
148 }
149 }