|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\IcsAccessToken; |
|
6
|
|
|
use App\Entity\User; |
|
7
|
|
|
use App\Form\NotificationsType; |
|
8
|
|
|
use App\Grouping\Grouper; |
|
9
|
|
|
use App\Grouping\UserTypeAndGradeStrategy; |
|
10
|
|
|
use App\Repository\DeviceTokenRepositoryInterface; |
|
11
|
|
|
use App\Repository\UserRepositoryInterface; |
|
12
|
|
|
use App\Section\SectionResolverInterface; |
|
13
|
|
|
use App\Security\Voter\DeviceTokenVoter; |
|
14
|
|
|
use App\Settings\NotificationSettings; |
|
15
|
|
|
use App\Sorting\Sorter; |
|
16
|
|
|
use App\Sorting\StringGroupStrategy; |
|
17
|
|
|
use App\Sorting\UserUsernameStrategy; |
|
18
|
|
|
use App\Utils\ArrayUtils; |
|
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
22
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
23
|
|
|
|
|
24
|
|
|
#[Route(path: '/profile')] |
|
25
|
|
|
class ProfileController extends AbstractController { |
|
26
|
|
|
|
|
27
|
|
|
private const RemoveAppCrsfTokenKey = '_remove_app_csrf'; |
|
28
|
|
|
|
|
29
|
|
|
#[Route(path: '', name: 'profile')] |
|
30
|
|
|
public function index(): Response { |
|
31
|
|
|
return $this->render('profile/index.html.twig'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
#[Route(path: '/notifications', name: 'profile_notifications')] |
|
35
|
|
|
public function notifications(Request $request, UserRepositoryInterface $userRepository, NotificationSettings $notificationSettings): Response { |
|
36
|
|
|
/** @var User $user */ |
|
37
|
|
|
$user = $this->getUser(); |
|
38
|
|
|
|
|
39
|
|
|
$allowedEmailUserTypes = $notificationSettings->getEmailEnabledUserTypes(); |
|
40
|
|
|
$isEmailAllowed = ArrayUtils::inArray($user->getUserType(), $allowedEmailUserTypes) !== false; |
|
41
|
|
|
$isPushoverAllowed = ArrayUtils::inArray($user->getUserType(), $notificationSettings->getPushoverEnabledUserTypes()) !== false; |
|
42
|
|
|
$isAllowed = $isEmailAllowed || $isPushoverAllowed; |
|
43
|
|
|
|
|
44
|
|
|
$form = null; |
|
45
|
|
|
|
|
46
|
|
|
if($isAllowed === true) { |
|
47
|
|
|
$form = $this->createForm(NotificationsType::class, $user, [ |
|
48
|
|
|
'allow_email' => $isEmailAllowed, |
|
49
|
|
|
'allow_pushover' => $isPushoverAllowed |
|
50
|
|
|
]); |
|
51
|
|
|
$form->handleRequest($request); |
|
52
|
|
|
|
|
53
|
|
|
if($form->isSubmitted() && $form->isValid()) { |
|
54
|
|
|
$userRepository->persist($user); |
|
55
|
|
|
$this->addFlash('success', 'profile.notifications.success'); |
|
56
|
|
|
|
|
57
|
|
|
return $this->redirectToRoute('profile_notifications'); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->render('profile/notifications.html.twig', [ |
|
62
|
|
|
'form' => $form !== null ? $form->createView() : null, |
|
63
|
|
|
'is_allowed' => $isAllowed, |
|
64
|
|
|
'is_pushover_allowed' => $isPushoverAllowed, |
|
65
|
|
|
'email_allowed' => $isAllowed |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
#[Route(path: '/apps', name: 'profile_apps')] |
|
70
|
|
|
public function apps(DeviceTokenRepositoryInterface $deviceTokenRepository): Response { |
|
71
|
|
|
/** @var User $user */ |
|
72
|
|
|
$user = $this->getUser(); |
|
73
|
|
|
|
|
74
|
|
|
$devices = $deviceTokenRepository->findAllBy($user); |
|
75
|
|
|
|
|
76
|
|
|
return $this->render('profile/apps.html.twig', [ |
|
77
|
|
|
'apps' => $devices, |
|
78
|
|
|
'csrf_key' => self::RemoveAppCrsfTokenKey |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
#[Route(path: '/apps/{uuid}/remove', name: 'profile_remove_app', methods: ['POST'])] |
|
83
|
|
|
public function removeApp(IcsAccessToken $token, Request $request, DeviceTokenRepositoryInterface $deviceTokenRepository): Response { |
|
84
|
|
|
$this->denyAccessUnlessGranted(DeviceTokenVoter::Remove, $token); |
|
85
|
|
|
|
|
86
|
|
|
$csrfToken = $request->request->get('_csrf_token'); |
|
87
|
|
|
if($this->isCsrfTokenValid(self::RemoveAppCrsfTokenKey, $csrfToken)) { |
|
88
|
|
|
$deviceTokenRepository->remove($token); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
$this->addFlash('success', 'profile.apps.remove.success'); |
|
91
|
|
|
} else { |
|
92
|
|
|
$this->addFlash('success', 'profile.apps.remove.error.csrf'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->redirectToRoute('profile_apps'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
#[Route(path: '/switch', name: 'switch_user')] |
|
99
|
|
|
#[Security("is_granted('ROLE_ALLOWED_TO_SWITCH')")] |
|
100
|
|
|
public function switchUser(Grouper $grouper, Sorter $sorter, UserRepositoryInterface $userRepository, SectionResolverInterface $sectionResolver): Response { |
|
101
|
|
|
$users = $userRepository->findAll(); |
|
102
|
|
|
$groups = $grouper->group($users, UserTypeAndGradeStrategy::class, ['section' => $sectionResolver->getCurrentSection()]); |
|
103
|
|
|
$sorter->sort($groups, StringGroupStrategy::class); |
|
104
|
|
|
$sorter->sortGroupItems($groups, UserUsernameStrategy::class); |
|
105
|
|
|
|
|
106
|
|
|
return $this->render('profile/switch.html.twig', [ |
|
107
|
|
|
'groups' => $groups |
|
108
|
|
|
]); |
|
109
|
|
|
} |
|
110
|
|
|
} |