forked from tchapi/davis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserController.php
More file actions
347 lines (284 loc) 路 15.4 KB
/
Copy pathUserController.php
File metadata and controls
347 lines (284 loc) 路 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
namespace App\Controller\Admin;
use App\Entity\AddressBook;
use App\Entity\Calendar;
use App\Entity\CalendarInstance;
use App\Entity\CalendarSubscription;
use App\Entity\Principal;
use App\Entity\SchedulingObject;
use App\Entity\User;
use App\Form\UserType;
use App\Services\Utils;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsCsrfTokenValid;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/users', name: 'user_')]
class UserController extends AbstractController
{
#[Route('/', name: 'index', methods: ['GET'])]
public function users(ManagerRegistry $doctrine): Response
{
$results = $doctrine->getRepository(Principal::class)->findAllMainPrincipalsWithUserIds();
return $this->render('users/index.html.twig', [
'results' => $results,
]);
}
#[Route('/new', name: 'create', methods: ['GET', 'POST'])]
#[Route('/edit/{userId}', name: 'edit', methods: ['GET', 'POST'])]
public function userCreate(ManagerRegistry $doctrine, Utils $utils, Request $request, ?int $userId, TranslatorInterface $trans): Response
{
if ($userId) {
$user = $doctrine->getRepository(User::class)->findOneById($userId);
if (!$user) {
throw $this->createNotFoundException('User not found');
}
$oldHash = $user->getPassword();
$principal = $doctrine->getRepository(Principal::class)->findOneByUri(Principal::PREFIX.$user->getUsername());
} else {
$user = new User();
$principal = new Principal();
}
$form = $this->createForm(UserType::class, $user, ['new' => !$userId]);
$form->get('displayName')->setData($principal->getDisplayName());
$form->get('email')->setData($principal->getEmail());
$form->get('isAdmin')->setData($principal->getIsAdmin());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$displayName = $form->get('displayName')->getData();
$email = $form->get('email')->getData();
$isAdmin = $form->get('isAdmin')->getData();
// Create password for user
if ($userId && is_null($user->getPassword())) {
// The user is not new and does not want to change its password
$user->setPassword($oldHash);
} else {
$hash = password_hash($user->getPassword(), PASSWORD_DEFAULT);
$user->setPassword($hash);
}
$entityManager = $doctrine->getManager();
// If it's a new user, create default calendar and address book, and principal
if (null === $user->getId()) {
$principal->setUri(Principal::PREFIX.$user->getUsername());
$calendarInstance = new CalendarInstance();
$calendar = new Calendar();
$calendarInstance->setPrincipalUri(Principal::PREFIX.$user->getUsername())
->setUri('default') // No risk of collision since unicity is guaranteed by the new user principal
->setDisplayName($trans->trans('default.calendar.title'))
->setDescription($trans->trans('default.calendar.description', ['user' => $displayName]))
->setCalendar($calendar);
// Enable delegation by default
$principalProxyRead = new Principal();
$principalProxyRead->setUri($principal->getUri().Principal::READ_PROXY_SUFFIX)
->setIsMain(false);
$entityManager->persist($principalProxyRead);
$principalProxyWrite = new Principal();
$principalProxyWrite->setUri($principal->getUri().Principal::WRITE_PROXY_SUFFIX)
->setIsMain(false);
$entityManager->persist($principalProxyWrite);
$addressbook = new AddressBook();
$addressbook->setPrincipalUri(Principal::PREFIX.$user->getUsername())
->setUri('default') // No risk of collision since unicity is guaranteed by the new user principal
->setDisplayName($trans->trans('default.addressbook.title'))
->setDescription($trans->trans('default.addressbook.description', ['user' => $displayName]));
$entityManager->persist($calendarInstance);
$entityManager->persist($addressbook);
$entityManager->persist($principal);
}
$principal->setDisplayName($displayName)
->setEmail($email)
->setIsAdmin($isAdmin);
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('success', $trans->trans('user.saved'));
return $this->redirectToRoute('user_index');
}
return $this->render('users/edit.html.twig', [
'form' => $form->createView(),
'userId' => $userId,
'username' => $user->getUsername(),
]);
}
#[Route('/delete/{userId}', name: 'delete', methods: ['POST'])]
#[IsCsrfTokenValid('delete-users')]
public function userDelete(ManagerRegistry $doctrine, int $userId, TranslatorInterface $trans): Response
{
$user = $doctrine->getRepository(User::class)->findOneById($userId);
if (!$user) {
throw $this->createNotFoundException('User not found');
}
$entityManager = $doctrine->getManager();
$entityManager->remove($user);
$principal = $doctrine->getRepository(Principal::class)->findOneByUri(Principal::PREFIX.$user->getUsername());
$principalProxyRead = $doctrine->getRepository(Principal::class)->findOneByUri($principal->getUri().Principal::READ_PROXY_SUFFIX);
$principalProxyWrite = $doctrine->getRepository(Principal::class)->findOneByUri($principal->getUri().Principal::WRITE_PROXY_SUFFIX);
$entityManager->remove($principal);
if ($principalProxyRead) {
$entityManager->remove($principalProxyRead);
}
if ($principalProxyWrite) {
$entityManager->remove($principalProxyWrite);
}
$principalUri = Principal::PREFIX.$user->getUsername();
// Remove calendars and addressbooks
$calendars = $doctrine->getRepository(CalendarInstance::class)->findByPrincipalUri($principalUri);
foreach ($calendars ?? [] as $instance) {
// We're only removing the calendar objects / changes / and calendar if the deleted user is an owner,
// which means that the underlying calendar instance should not have another principal as owner.
$hasDifferentOwner = $doctrine->getRepository(CalendarInstance::class)->hasDifferentOwner($instance->getCalendar()->getId(), $principalUri);
if (!$hasDifferentOwner) {
foreach ($instance->getCalendar()->getObjects() ?? [] as $object) {
$entityManager->remove($object);
}
foreach ($instance->getCalendar()->getChanges() ?? [] as $change) {
$entityManager->remove($change);
}
// We need to remove the shared versions of this calendar, too
foreach ($instance->getCalendar()->getInstances() ?? [] as $instances) {
$entityManager->remove($instances);
}
$entityManager->remove($instance->getCalendar());
}
$entityManager->remove($instance);
}
$calendarsSubscriptions = $doctrine->getRepository(CalendarSubscription::class)->findByPrincipalUri($principalUri);
foreach ($calendarsSubscriptions ?? [] as $subscription) {
$entityManager->remove($subscription);
}
$schedulingObjects = $doctrine->getRepository(SchedulingObject::class)->findByPrincipalUri($principalUri);
foreach ($schedulingObjects ?? [] as $object) {
$entityManager->remove($object);
}
$addressbooks = $doctrine->getRepository(AddressBook::class)->findByPrincipalUri($principalUri);
foreach ($addressbooks ?? [] as $addressbook) {
foreach ($addressbook->getCards() ?? [] as $card) {
$entityManager->remove($card);
}
foreach ($addressbook->getChanges() ?? [] as $change) {
$entityManager->remove($change);
}
$entityManager->remove($addressbook);
}
$entityManager->flush();
$this->addFlash('success', $trans->trans('user.deleted'));
return $this->redirectToRoute('user_index');
}
#[Route('/delegates/{userId}', name: 'delegates', methods: ['GET'])]
public function userDelegates(ManagerRegistry $doctrine, int $userId): Response
{
$user = $doctrine->getRepository(User::class)->findOneById($userId);
if (!$user) {
throw $this->createNotFoundException('User not found');
}
$principalUri = Principal::PREFIX.$user->getUsername();
$principal = $doctrine->getRepository(Principal::class)->findOneByUri($principalUri);
$allPrincipalsExcept = $doctrine->getRepository(Principal::class)->findAllExceptPrincipal($principalUri);
// Get delegates. They are not linked to the principal in itself, but to its proxies
$principalProxyRead = $doctrine->getRepository(Principal::class)->findOneByUri($principal->getUri().Principal::READ_PROXY_SUFFIX);
$principalProxyWrite = $doctrine->getRepository(Principal::class)->findOneByUri($principal->getUri().Principal::WRITE_PROXY_SUFFIX);
return $this->render('users/delegates.html.twig', [
'principal' => $principal,
'userId' => $userId,
'delegation' => $principalProxyRead && $principalProxyWrite,
'principalProxyRead' => $principalProxyRead,
'principalProxyWrite' => $principalProxyWrite,
'allPrincipals' => $allPrincipalsExcept,
]);
}
#[Route('/delegation/{userId}/{toggle}', name: 'delegation_toggle', methods: ['POST'], requirements: ['toggle' => '(on|off)'])]
#[IsCsrfTokenValid('delegation-toggle')]
public function userToggleDelegation(ManagerRegistry $doctrine, int $userId, string $toggle): Response
{
$user = $doctrine->getRepository(User::class)->findOneById($userId);
if (!$user) {
throw $this->createNotFoundException('User not found');
}
$principalUri = Principal::PREFIX.$user->getUsername();
$principal = $doctrine->getRepository(Principal::class)->findOneByUri($principalUri);
if (!$principal) {
throw $this->createNotFoundException('Principal not found');
}
$entityManager = $doctrine->getManager();
if ('on' === $toggle) {
$principalProxyRead = new Principal();
$principalProxyRead->setUri($principal->getUri().Principal::READ_PROXY_SUFFIX)
->setIsMain(false);
$entityManager->persist($principalProxyRead);
$principalProxyWrite = new Principal();
$principalProxyWrite->setUri($principal->getUri().Principal::WRITE_PROXY_SUFFIX)
->setIsMain(false);
$entityManager->persist($principalProxyWrite);
} else {
$principalProxyRead = $doctrine->getRepository(Principal::class)->findOneByUri($principal->getUri().Principal::READ_PROXY_SUFFIX);
$principalProxyRead && $entityManager->remove($principalProxyRead);
$principalProxyWrite = $doctrine->getRepository(Principal::class)->findOneByUri($principal->getUri().Principal::WRITE_PROXY_SUFFIX);
$principalProxyWrite && $entityManager->remove($principalProxyWrite);
// Remove also delegates
$principal->removeAllDelegees();
}
$entityManager->flush();
return $this->redirectToRoute('user_delegates', ['userId' => $userId]);
}
#[Route('/delegates/{userId}/add', name: 'delegate_add', methods: ['POST'])]
#[IsCsrfTokenValid('delegate-add')]
public function userDelegateAdd(ManagerRegistry $doctrine, Request $request, int $userId): Response
{
if (!is_numeric($request->request->get('principalId'))) {
throw new BadRequestHttpException();
}
$user = $doctrine->getRepository(User::class)->findOneById($userId);
if (!$user) {
throw $this->createNotFoundException('User not found');
}
$principalUri = Principal::PREFIX.$user->getUsername();
$newMemberToAdd = $doctrine->getRepository(Principal::class)->findOneById($request->request->get('principalId'));
if (!$newMemberToAdd) {
throw $this->createNotFoundException('Member not found');
}
// Depending on write access or not, attach to the correct principal
if ('true' === $request->request->get('write')) {
// Let's check that there wasn't a read proxy first
$principalProxyRead = $doctrine->getRepository(Principal::class)->findOneByUri($principalUri.Principal::READ_PROXY_SUFFIX);
if (!$principalProxyRead) {
throw $this->createNotFoundException('Principal linked to this calendar not found');
}
$principalProxyRead->removeDelegee($newMemberToAdd);
// And then add the Write access
$principal = $doctrine->getRepository(Principal::class)->findOneByUri($principalUri.Principal::WRITE_PROXY_SUFFIX);
} else {
$principal = $doctrine->getRepository(Principal::class)->findOneByUri($principalUri.Principal::READ_PROXY_SUFFIX);
}
if (!$principal) {
throw $this->createNotFoundException('Principal linked to this calendar not found');
}
$principal->addDelegee($newMemberToAdd);
$entityManager = $doctrine->getManager();
$entityManager->flush();
return $this->redirectToRoute('user_delegates', ['userId' => $userId]);
}
#[Route('/delegates/{userId}/remove/{principalProxyId}/{delegateId}', name: 'delegate_remove', methods: ['POST'], requirements: ['principalProxyId' => "\d+", 'delegateId' => "\d+"])]
#[IsCsrfTokenValid('delete-delegates')]
public function userDelegateRemove(ManagerRegistry $doctrine, int $userId, int $principalProxyId, int $delegateId): Response
{
$user = $doctrine->getRepository(User::class)->findOneById($userId);
if (!$user) {
throw $this->createNotFoundException('User not found');
}
$principalProxy = $doctrine->getRepository(Principal::class)->findOneById($principalProxyId);
if (!$principalProxy) {
throw $this->createNotFoundException('Principal linked to this calendar not found');
}
$memberToRemove = $doctrine->getRepository(Principal::class)->findOneById($delegateId);
if (!$memberToRemove) {
throw $this->createNotFoundException('Member not found');
}
$principalProxy->removeDelegee($memberToRemove);
$entityManager = $doctrine->getManager();
$entityManager->flush();
return $this->redirectToRoute('user_delegates', ['userId' => $userId]);
}
}