|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
ÁTICA - Aplicación web para la gestión documental de centros educativos |
|
4
|
|
|
|
|
5
|
|
|
Copyright (C) 2015-2017: Luis Ramón López López |
|
6
|
|
|
|
|
7
|
|
|
This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
it under the terms of the GNU Affero General Public License as published by |
|
9
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
(at your option) any later version. |
|
11
|
|
|
|
|
12
|
|
|
This program is distributed in the hope that it will be useful, |
|
13
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
GNU Affero General Public License for more details. |
|
16
|
|
|
|
|
17
|
|
|
You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
along with this program. If not, see [http://www.gnu.org/licenses/]. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppBundle\Controller\Admin; |
|
22
|
|
|
|
|
23
|
|
|
use AppBundle\Entity\User; |
|
24
|
|
|
use AppBundle\Form\Type\UserType; |
|
25
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
26
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
|
27
|
|
|
use Pagerfanta\Pagerfanta; |
|
28
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
29
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
|
30
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
31
|
|
|
use Symfony\Component\Form\Form; |
|
32
|
|
|
use Symfony\Component\Form\SubmitButton; |
|
33
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @Route("/admin/usuarios") |
|
37
|
|
|
* @Security("is_granted('ROLE_ADMIN')") |
|
38
|
|
|
*/ |
|
39
|
|
|
class UserController extends Controller |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* @Route("/nuevo", name="admin_user_form_new", methods={"GET", "POST"}) |
|
43
|
|
|
* @Route("/{id}", name="admin_user_form_edit", requirements={"id" = "\d+"}, methods={"GET", "POST"}) |
|
44
|
|
|
*/ |
|
45
|
|
|
public function indexAction(User $localUser = null, Request $request) |
|
46
|
|
|
{ |
|
47
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
48
|
|
|
|
|
49
|
|
|
if (null === $localUser) { |
|
50
|
|
|
$localUser = new User(); |
|
51
|
|
|
$em->persist($localUser); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$form = $this->createForm(UserType::class, $localUser, [ |
|
55
|
|
|
'own' => $this->getUser()->getId() === $localUser->getId(), |
|
56
|
|
|
'admin' => $this->getUser()->isGlobalAdministrator(), |
|
57
|
|
|
'new' => $localUser->getId() === null |
|
58
|
|
|
]); |
|
59
|
|
|
|
|
60
|
|
|
$form->handleRequest($request); |
|
61
|
|
|
|
|
62
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
63
|
|
|
|
|
64
|
|
|
$message = $this->processPasswordChange($localUser, $form); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
|
|
$em->flush(); |
|
68
|
|
|
$this->addFlash('success', $message); |
|
69
|
|
|
return $this->redirectToRoute('admin_user_list'); |
|
70
|
|
|
} catch (\Exception $e) { |
|
71
|
|
|
$this->addFlash('error', $this->get('translator')->trans('message.error', [], 'user')); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$title = $this->get('translator')->trans($localUser->getId() ? 'title.edit' : 'title.new', [], 'user'); |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
$breadcrumb = [$localUser->getId() ? ['fixed' => (string) $localUser] : ['fixed' => $this->get('translator')->trans('title.new', [], 'user')]]; |
|
79
|
|
|
|
|
80
|
|
|
return $this->render('admin/user/user_form.html.twig', [ |
|
81
|
|
|
'menu_path' => 'admin_user_list', |
|
82
|
|
|
'breadcrumb' => $breadcrumb, |
|
83
|
|
|
'title' => $title, |
|
84
|
|
|
'form' => $form->createView(), |
|
85
|
|
|
'user' => $localUser |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @Route("/listar/{page}", name="admin_user_list", requirements={"page" = "\d+"}, defaults={"page" = "1"}, methods={"GET"}) |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
public function listAction($page, Request $request) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
/** @var QueryBuilder $queryBuilder */ |
|
95
|
|
|
$queryBuilder = $this->getDoctrine()->getManager()->createQueryBuilder(); |
|
96
|
|
|
|
|
97
|
|
|
$queryBuilder |
|
98
|
|
|
->select('u') |
|
99
|
|
|
->from('AppBundle:User', 'u') |
|
100
|
|
|
->orderBy('u.lastName') |
|
101
|
|
|
->addOrderBy('u.firstName'); |
|
102
|
|
|
|
|
103
|
|
|
$q = $request->get('q', null); |
|
104
|
|
|
if ($q) { |
|
105
|
|
|
$queryBuilder |
|
106
|
|
|
->where('u.id = :q') |
|
107
|
|
|
->orWhere('u.loginUsername LIKE :tq') |
|
108
|
|
|
->orWhere('u.firstName LIKE :tq') |
|
109
|
|
|
->orWhere('u.lastName LIKE :tq') |
|
110
|
|
|
->orWhere('u.emailAddress LIKE :tq') |
|
111
|
|
|
->setParameter('tq', '%'.$q.'%') |
|
112
|
|
|
->setParameter('q', $q); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$adapter = new DoctrineORMAdapter($queryBuilder, false); |
|
116
|
|
|
$pager = new Pagerfanta($adapter); |
|
117
|
|
|
$pager |
|
118
|
|
|
->setMaxPerPage($this->getParameter('page.size')) |
|
119
|
|
|
->setCurrentPage($q ? 1 : $page); |
|
120
|
|
|
|
|
121
|
|
|
$title = $this->get('translator')->trans('title.list', [], 'user'); |
|
122
|
|
|
|
|
123
|
|
|
return $this->render('admin/user/list.html.twig', [ |
|
124
|
|
|
'title' => $title, |
|
125
|
|
|
'pager' => $pager, |
|
126
|
|
|
'q' => $q, |
|
127
|
|
|
'domain' => 'user' |
|
128
|
|
|
]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @Route("/eliminar", name="admin_user_delete", methods={"POST"}) |
|
133
|
|
|
*/ |
|
134
|
|
|
public function deleteAction(Request $request) |
|
135
|
|
|
{ |
|
136
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
137
|
|
|
|
|
138
|
|
|
/** @var QueryBuilder $queryBuilder */ |
|
139
|
|
|
$queryBuilder = $em->createQueryBuilder(); |
|
140
|
|
|
|
|
141
|
|
|
$items = $request->request->get('users', []); |
|
142
|
|
|
if (count($items) === 0) { |
|
143
|
|
|
return $this->redirectToRoute('admin_user_list'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$users = $queryBuilder |
|
147
|
|
|
->select('u') |
|
148
|
|
|
->from('AppBundle:User', 'u') |
|
149
|
|
|
->where('u.id IN (:items)') |
|
150
|
|
|
->andWhere('u.id != :current') |
|
151
|
|
|
->setParameter('items', $items) |
|
152
|
|
|
->setParameter('current', $this->getUser()->getId()) |
|
153
|
|
|
->orderBy('u.firstName') |
|
154
|
|
|
->addOrderBy('u.lastName') |
|
155
|
|
|
->getQuery() |
|
156
|
|
|
->getResult(); |
|
157
|
|
|
|
|
158
|
|
|
if ($request->get('confirm', '') === 'ok') { |
|
159
|
|
|
try { |
|
160
|
|
|
/* Borrar primero las pertenencias */ |
|
161
|
|
|
$em->createQueryBuilder() |
|
162
|
|
|
->delete('AppBundle:Membership', 'm') |
|
163
|
|
|
->where('m.user IN (:items)') |
|
164
|
|
|
->setParameter('items', $items) |
|
165
|
|
|
->getQuery() |
|
166
|
|
|
->execute(); |
|
167
|
|
|
|
|
168
|
|
|
$em->createQueryBuilder() |
|
169
|
|
|
->delete('AppBundle:User', 'u') |
|
170
|
|
|
->where('u IN (:items)') |
|
171
|
|
|
->setParameter('items', $items) |
|
172
|
|
|
->getQuery() |
|
173
|
|
|
->execute(); |
|
174
|
|
|
|
|
175
|
|
|
$em->flush(); |
|
176
|
|
|
$this->addFlash('success', $this->get('translator')->trans('message.deleted', [], 'user')); |
|
177
|
|
|
} catch (\Exception $e) { |
|
178
|
|
|
$this->addFlash('error', $this->get('translator')->trans('message.delete_error', [], 'user')); |
|
179
|
|
|
} |
|
180
|
|
|
return $this->redirectToRoute('admin_user_list'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $this->render('admin/user/delete.html.twig', [ |
|
184
|
|
|
'menu_path' => 'admin_user_list', |
|
185
|
|
|
'breadcrumb' => [['fixed' => $this->get('translator')->trans('title.delete', [], 'user')]], |
|
186
|
|
|
'title' => $this->get('translator')->trans('title.delete', [], 'user'), |
|
187
|
|
|
'users' => $users |
|
188
|
|
|
]); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param User $user |
|
193
|
|
|
* @param Form $form |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
private function processPasswordChange(User $user, Form $form) |
|
197
|
|
|
{ |
|
198
|
|
|
// Si es solicitado, cambiar la contraseña |
|
199
|
|
|
$passwordSubmit = $form->get('changePassword'); |
|
200
|
|
|
if (($passwordSubmit instanceof SubmitButton) && $passwordSubmit->isClicked()) { |
|
201
|
|
|
$user->setPassword($this->container->get('security.password_encoder') |
|
202
|
|
|
->encodePassword($user, $form->get('newPassword')->get('first')->getData())); |
|
203
|
|
|
$message = $this->get('translator')->trans('message.password_changed', [], 'user'); |
|
204
|
|
|
} else { |
|
205
|
|
|
$message = $this->get('translator')->trans('message.saved', [], 'user'); |
|
206
|
|
|
} |
|
207
|
|
|
return $message; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.