Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
15.96% |
15 / 94 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
ProfileController | |
15.96% |
15 / 94 |
|
33.33% |
2 / 6 |
167.96 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
collectivityShowAction | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
formPrePersistData | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
collectivityEditAction | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
20 | |||
userEditAction | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
20 | |||
userSsoUnlinkAction | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of the MADIS - RGPD Management application. |
5 | * |
6 | * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU Affero General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU Affero General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Affero General Public License |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Domain\User\Controller; |
25 | |
26 | use App\Application\Controller\ControllerHelper; |
27 | use App\Application\Symfony\Security\UserProvider; |
28 | use App\Domain\User\Form\Type\CollectivityType; |
29 | use App\Domain\User\Form\Type\ReviewDataType; |
30 | use App\Domain\User\Form\Type\UserType; |
31 | use App\Domain\User\Model\ReviewData; |
32 | use App\Domain\User\Model\User; |
33 | use App\Domain\User\Repository; |
34 | use Doctrine\ORM\EntityManagerInterface; |
35 | use Gaufrette\FilesystemInterface; |
36 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
37 | use Symfony\Component\Form\Form; |
38 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
39 | use Symfony\Component\HttpFoundation\RequestStack; |
40 | use Symfony\Component\HttpFoundation\Response; |
41 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
42 | use Symfony\Component\Security\Core\Security; |
43 | use Symfony\Component\String\Slugger\SluggerInterface; |
44 | |
45 | class ProfileController extends AbstractController |
46 | { |
47 | // use ControllerTrait; |
48 | |
49 | /** |
50 | * @var EntityManagerInterface |
51 | */ |
52 | private $entityManager; |
53 | |
54 | /** |
55 | * @var ControllerHelper |
56 | */ |
57 | private $helper; |
58 | |
59 | /** |
60 | * @var UserProvider |
61 | */ |
62 | private $userProvider; |
63 | |
64 | /** |
65 | * @var RequestStack |
66 | */ |
67 | private $requestStack; |
68 | |
69 | /** |
70 | * @var Repository\Collectivity |
71 | */ |
72 | private $collectivityRepository; |
73 | |
74 | /** |
75 | * @var Repository\User |
76 | */ |
77 | private $userRepository; |
78 | |
79 | /** |
80 | * @var Security |
81 | */ |
82 | private $security; |
83 | private ?string $sso_type; |
84 | |
85 | /** |
86 | * @var FilesystemInterface |
87 | */ |
88 | protected $logoFilesystem; |
89 | |
90 | protected SluggerInterface $slugger; |
91 | |
92 | public function __construct( |
93 | EntityManagerInterface $entityManager, |
94 | ControllerHelper $helper, |
95 | RequestStack $requestStack, |
96 | UserProvider $userProvider, |
97 | Repository\Collectivity $collectivityRepository, |
98 | Repository\User $userRepository, |
99 | Security $security, |
100 | ?string $sso_type, |
101 | FilesystemInterface $logoFilesystem, |
102 | SluggerInterface $slugger, |
103 | ) { |
104 | $this->entityManager = $entityManager; |
105 | $this->helper = $helper; |
106 | $this->requestStack = $requestStack; |
107 | $this->userProvider = $userProvider; |
108 | $this->collectivityRepository = $collectivityRepository; |
109 | $this->userRepository = $userRepository; |
110 | $this->sso_type = $sso_type; |
111 | $this->security = $security; |
112 | $this->logoFilesystem = $logoFilesystem; |
113 | $this->slugger = $slugger; |
114 | } |
115 | |
116 | /** |
117 | * Show user collectivity information. |
118 | * |
119 | * @throws \Twig\Error\LoaderError |
120 | * @throws \Twig\Error\RuntimeError |
121 | * @throws \Twig\Error\SyntaxError |
122 | */ |
123 | public function collectivityShowAction(): Response |
124 | { |
125 | $object = $this->userProvider->getAuthenticatedUser()->getCollectivity(); |
126 | |
127 | return $this->helper->render('User/Profile/collectivity_show.html.twig', [ |
128 | 'object' => $object, |
129 | 'sections' => ReviewDataType::getSections(), |
130 | ]); |
131 | } |
132 | |
133 | public function formPrePersistData($object, $form = null): void |
134 | { |
135 | /** @var Form $reviewDataForm */ |
136 | $reviewDataForm = $form->get('reviewData'); |
137 | if ($reviewDataForm) { |
138 | /** @var UploadedFile $logoFile */ |
139 | $logoFile = $reviewDataForm->get('logo')->getData(); |
140 | if ($logoFile) { |
141 | $originalFilename = pathinfo($logoFile->getClientOriginalName(), PATHINFO_FILENAME); |
142 | // this is needed to safely include the file name as part of the URL |
143 | $safeFilename = $this->slugger->slug($originalFilename); |
144 | $newFilename = $safeFilename . '-' . uniqid() . '.' . $logoFile->guessExtension(); |
145 | $this->logoFilesystem->write($newFilename, \fopen($logoFile->getRealPath(), 'r')); |
146 | |
147 | // updates the 'brochureFilename' property to store the PDF file name |
148 | // instead of its contents |
149 | /** @var ReviewData $reviewData */ |
150 | $reviewData = $object->getReviewData(); |
151 | $reviewData->setLogo('/uploads/collectivity/logos/' . $newFilename); |
152 | $object->setReviewData($reviewData); |
153 | } |
154 | |
155 | if ($reviewDataForm->has('deleteLogo')) { |
156 | $deleteLogo = $reviewDataForm->get('deleteLogo')->getData(); |
157 | if ($deleteLogo) { |
158 | /** @var ReviewData $reviewData */ |
159 | $reviewData = $object->getReviewData(); |
160 | $reviewData->setLogo(null); |
161 | $object->setReviewData($reviewData); |
162 | } |
163 | } |
164 | } |
165 | } |
166 | |
167 | /** |
168 | * Generate collectivity edit form for user. |
169 | * |
170 | * @throws \Twig\Error\LoaderError |
171 | * @throws \Twig\Error\RuntimeError |
172 | * @throws \Twig\Error\SyntaxError |
173 | */ |
174 | public function collectivityEditAction(): Response |
175 | { |
176 | $request = $this->requestStack->getMasterRequest(); |
177 | $object = $this->userProvider->getAuthenticatedUser()->getCollectivity(); |
178 | |
179 | if (!$this->security->isGranted('ROLE_USER')) { |
180 | throw new AccessDeniedHttpException(); |
181 | } |
182 | $form = $this->helper->createForm( |
183 | CollectivityType::class, |
184 | $object, |
185 | [ |
186 | 'validation_groups' => [ |
187 | 'default', |
188 | 'collectivity_user', |
189 | 'edit', |
190 | ], |
191 | ] |
192 | ); |
193 | |
194 | $form->handleRequest($request); |
195 | if ($form->isSubmitted() && $form->isValid()) { |
196 | $this->formPrePersistData($object, $form); |
197 | $this->entityManager->persist($object); |
198 | $this->collectivityRepository->update($object); |
199 | |
200 | $this->helper->addFlash('success', $this->helper->trans('user.organization.flashbag.success.my_organization_edit')); |
201 | |
202 | return $this->helper->redirectToRoute('user_profile_collectivity_show', ['id' => $object->getId()]); |
203 | } |
204 | |
205 | return $this->helper->render('User/Profile/collectivity_edit.html.twig', [ |
206 | 'form' => $form->createView(), |
207 | 'object' => $object, |
208 | ]); |
209 | } |
210 | |
211 | /** |
212 | * Generate user edit form. |
213 | * |
214 | * @throws \Twig\Error\LoaderError |
215 | * @throws \Twig\Error\RuntimeError |
216 | * @throws \Twig\Error\SyntaxError |
217 | */ |
218 | public function userEditAction(): Response |
219 | { |
220 | $request = $this->requestStack->getMasterRequest(); |
221 | $object = $this->userProvider->getAuthenticatedUser(); |
222 | |
223 | $services = false; |
224 | |
225 | if ($object) { |
226 | $services = $object->getServices(); |
227 | } |
228 | |
229 | $form = $this->helper->createForm( |
230 | UserType::class, |
231 | $object, |
232 | [ |
233 | 'validation_groups' => [ |
234 | 'default', |
235 | 'collectivity_user', |
236 | 'edit', |
237 | ], |
238 | ] |
239 | ); |
240 | |
241 | $form->handleRequest($request); |
242 | if ($form->isSubmitted() && $form->isValid()) { |
243 | $this->userRepository->update($object); |
244 | |
245 | $this->helper->addFlash('success', $this->helper->trans('user.user.flashbag.success.my_profil_edit')); |
246 | |
247 | return $this->helper->redirectToRoute('user_profile_user_edit'); |
248 | } |
249 | |
250 | return $this->helper->render('User/Profile/user_edit.html.twig', [ |
251 | 'form' => $form->createView(), |
252 | 'roles' => $object->getRoles(), |
253 | 'services' => $services, |
254 | 'sso_type' => $this->sso_type, |
255 | 'sso_associated' => null !== $object->getSsoKey(), |
256 | ]); |
257 | } |
258 | |
259 | public function userSsoUnlinkAction(): Response |
260 | { |
261 | $object = $this->userProvider->getAuthenticatedUser(); |
262 | $object->setSsoKey(null); |
263 | $this->entityManager->persist($object); |
264 | $this->entityManager->flush(); |
265 | $this->helper->addFlash('success', |
266 | $this->helper->trans('user.user.flashbag.success.sso_unlink') |
267 | ); |
268 | |
269 | return $this->helper->redirectToRoute('user_profile_user_edit'); |
270 | } |
271 | } |