1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* UserController controller des utilisateurs. |
4
|
|
|
* |
5
|
|
|
* PHP Version 5 |
6
|
|
|
* |
7
|
|
|
* @author Quétier Laurent <[email protected]> |
8
|
|
|
* @copyright 2014 Dev-Int GLSR |
9
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
10
|
|
|
* |
11
|
|
|
* @version since 1.0.0 |
12
|
|
|
* |
13
|
|
|
* @link https://github.com/Dev-Int/glsr |
14
|
|
|
*/ |
15
|
|
|
namespace AppBundle\Controller; |
16
|
|
|
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use AppBundle\Controller\AbstractController; |
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
23
|
|
|
use AppBundle\Entity\User; |
24
|
|
|
use AppBundle\Form\Type\UserType; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* User controller. |
28
|
|
|
* |
29
|
|
|
* @category Controller |
30
|
|
|
* |
31
|
|
|
* @Route("/admin/user") |
32
|
|
|
*/ |
33
|
|
|
class UserController extends AbstractController |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Lists all User entities. |
37
|
|
|
* |
38
|
|
|
* @Route("/", name="user") |
39
|
|
|
* @Method("GET") |
40
|
|
|
* @Template() |
41
|
|
|
*/ |
42
|
|
|
public function indexAction(Request $request) |
43
|
|
|
{ |
44
|
|
|
$etm = $this->getDoctrine()->getManager(); |
45
|
|
|
$qbd = $etm->getRepository('AppBundle:User')->getUsers(); |
46
|
|
|
$this->addQueryBuilderSort($qbd, 'user'); |
47
|
|
|
$paginator = $this->get('knp_paginator')->paginate($qbd, $request->query->get('page', 1), 5); |
48
|
|
|
|
49
|
|
|
return array( |
50
|
|
|
'paginator' => $paginator, |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Finds and displays a User entity. |
56
|
|
|
* |
57
|
|
|
* @Route("/{id}/show", name="user_show", requirements={"id"="\d+"}) |
58
|
|
|
* @Method("GET") |
59
|
|
|
* @Template() |
60
|
|
|
*/ |
61
|
|
|
public function showAction(User $user) |
62
|
|
|
{ |
63
|
|
|
$return = $this->abstractShowAction($user, 'user'); |
64
|
|
|
|
65
|
|
|
return $return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Displays a form to create a new User entity. |
70
|
|
|
* |
71
|
|
|
* @Route("/new", name="user_new") |
72
|
|
|
* @Method("GET") |
73
|
|
|
* @Template() |
74
|
|
|
*/ |
75
|
|
|
public function newAction() |
76
|
|
|
{ |
77
|
|
|
$return = $this->abstractNewAction( |
78
|
|
|
'User', |
79
|
|
|
'AppBundle\Entity\User', |
80
|
|
|
'AppBundle\Form\Type\UserType' |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Creates a new User entity. |
88
|
|
|
* |
89
|
|
|
* @Route("/create", name="user_create") |
90
|
|
|
* @Method("POST") |
91
|
|
|
* @Template("AppBundle:User:new.html.twig") |
92
|
|
|
*/ |
93
|
|
|
public function createAction(Request $request) |
94
|
|
|
{ |
95
|
|
|
$user = new User(); |
96
|
|
|
$form = $this->createForm(new UserType(), $user); |
97
|
|
View Code Duplication |
if ($form->handleRequest($request)->isValid()) { |
|
|
|
|
98
|
|
|
$user->setEnabled(true); |
99
|
|
|
$userManager = $this->get('fos_user.user_manager'); |
100
|
|
|
$userManager->updateUser($user); |
101
|
|
|
|
102
|
|
|
return $this->redirectToRoute('user_show', array('id', $user->getId())); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return array( |
106
|
|
|
'user' => $user, |
107
|
|
|
'form' => $form->createView(), |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Displays a form to edit an existing User entity. |
113
|
|
|
* |
114
|
|
|
* @Route("/{id}/edit", name="user_edit", requirements={"id"="\d+"}) |
115
|
|
|
* @Method("GET") |
116
|
|
|
* @Template() |
117
|
|
|
*/ |
118
|
|
|
public function editAction(User $user) |
119
|
|
|
{ |
120
|
|
|
$editForm = $this->createForm(new UserType(), $user, array( |
121
|
|
|
'action' => $this->generateUrl('user_update', array('id' => $user->getId())), |
122
|
|
|
'method' => 'PUT', |
123
|
|
|
'passwordRequired' => false, |
124
|
|
|
'lockedRequired' => true |
125
|
|
|
)); |
126
|
|
|
$deleteForm = $this->createDeleteForm($user->getId(), 'user_delete'); |
127
|
|
|
|
128
|
|
|
return array( |
129
|
|
|
'user' => $user, |
130
|
|
|
'edit_form' => $editForm->createView(), |
131
|
|
|
'delete_form' => $deleteForm->createView(), |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Edits an existing User entity. |
137
|
|
|
* |
138
|
|
|
* @Route("/{id}/update", name="user_update", requirements={"id"="\d+"}) |
139
|
|
|
* @Method("PUT") |
140
|
|
|
* @Template("AppBundle:User:edit.html.twig") |
141
|
|
|
*/ |
142
|
|
|
public function updateAction(User $user, Request $request) |
143
|
|
|
{ |
144
|
|
|
$editForm = $this->createForm(new UserType(), $user, array( |
145
|
|
|
'action' => $this->generateUrl('user_update', array('id' => $user->getId())), |
146
|
|
|
'method' => 'PUT', |
147
|
|
|
'passwordRequired' => false, |
148
|
|
|
'lockedRequired' => true |
149
|
|
|
)); |
150
|
|
|
if ($editForm->handleRequest($request)->isValid()) { |
151
|
|
|
$userManager = $this->get('fos_user.user_manager'); |
152
|
|
|
$userManager->updateUser($user); |
153
|
|
|
|
154
|
|
|
return $this->redirectToRoute('user_edit', array('id' => $user->getId())); |
155
|
|
|
} |
156
|
|
|
$deleteForm = $this->createDeleteForm($user->getId(), 'user_delete'); |
157
|
|
|
|
158
|
|
|
return array( |
159
|
|
|
'user' => $user, |
160
|
|
|
'edit_form' => $editForm->createView(), |
161
|
|
|
'delete_form' => $deleteForm->createView(), |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Save order. |
168
|
|
|
* |
169
|
|
|
* @Route("/order/{entity}/{field}/{type}", name="user_sort") |
170
|
|
|
*/ |
171
|
|
|
public function sortAction($entity, $field, $type) |
172
|
|
|
{ |
173
|
|
|
$this->setOrder('user', $entity, $field, $type); |
174
|
|
|
|
175
|
|
|
return $this->redirectToRoute('user'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Deletes a User entity. |
180
|
|
|
* |
181
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
182
|
|
|
* @Route("/{id}/delete", name="user_delete", requirements={"id"="\d+"}) |
183
|
|
|
* @Method("DELETE") |
184
|
|
|
*/ |
185
|
|
|
public function deleteAction(User $user, Request $request) |
186
|
|
|
{ |
187
|
|
|
$this->abstractDeleteAction($user, $request, 'user'); |
188
|
|
|
|
189
|
|
|
return $this->redirectToRoute('user'); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.