aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/UserBundle/Controller/ManageController.php
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2016-10-02 15:00:02 +0200
committerGitHub <noreply@github.com>2016-10-02 15:00:02 +0200
commit1dc3bee6b916b228c596a45d20dc6ae14ac555cb (patch)
tree2d3573ebc6e6fbd71dd63f15653f3347cde8164e /src/Wallabag/UserBundle/Controller/ManageController.php
parent18b8dc0e9984fce01b231810347fef7d32c06ac7 (diff)
parentccc7faec094387681a59faa5ca79a86e33f06972 (diff)
downloadwallabag-1dc3bee6b916b228c596a45d20dc6ae14ac555cb.tar.gz
wallabag-1dc3bee6b916b228c596a45d20dc6ae14ac555cb.tar.zst
wallabag-1dc3bee6b916b228c596a45d20dc6ae14ac555cb.zip
Merge pull request #2327 from wallabag/user-management
Add users management UI
Diffstat (limited to 'src/Wallabag/UserBundle/Controller/ManageController.php')
-rw-r--r--src/Wallabag/UserBundle/Controller/ManageController.php149
1 files changed, 149 insertions, 0 deletions
diff --git a/src/Wallabag/UserBundle/Controller/ManageController.php b/src/Wallabag/UserBundle/Controller/ManageController.php
new file mode 100644
index 00000000..92ee2b41
--- /dev/null
+++ b/src/Wallabag/UserBundle/Controller/ManageController.php
@@ -0,0 +1,149 @@
1<?php
2
3namespace Wallabag\UserBundle\Controller;
4
5use FOS\UserBundle\Event\UserEvent;
6use FOS\UserBundle\FOSUserEvents;
7use Symfony\Component\HttpFoundation\Request;
8use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
10use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11use Wallabag\UserBundle\Entity\User;
12use Wallabag\CoreBundle\Entity\Config;
13
14/**
15 * User controller.
16 */
17class 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}