SchulIT /
idp
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Controller; |
||
| 4 | |||
| 5 | use App\Entity\User; |
||
| 6 | use App\Form\LinkStudentType; |
||
| 7 | use App\Repository\RegistrationCodeRepositoryInterface; |
||
| 8 | use App\Repository\UserRepositoryInterface; |
||
| 9 | use App\Security\Session\ActiveSessionsResolver; |
||
| 10 | use App\Security\Session\LogoutHelper; |
||
| 11 | use App\Security\Voter\LinkStudentVoter; |
||
| 12 | use App\Service\UserServiceProviderResolver; |
||
| 13 | use SchulIT\CommonBundle\Helper\DateHelper; |
||
| 14 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||
| 15 | use Symfony\Component\HttpFoundation\Request; |
||
| 16 | 1 | use Symfony\Component\HttpFoundation\Response; |
|
| 17 | 1 | use Symfony\Component\Routing\Annotation\Route; |
|
| 18 | use Symfony\Contracts\Translation\TranslatorInterface; |
||
| 19 | |||
| 20 | class DashboardController extends AbstractController { |
||
| 21 | |||
| 22 | #[Route(path: '/')] |
||
| 23 | 5 | public function redirectToDashboard(): Response { |
|
| 24 | return $this->redirectToRoute('dashboard'); |
||
| 25 | 5 | } |
|
| 26 | |||
| 27 | 5 | #[Route(path: '/dashboard', name: 'dashboard')] |
|
| 28 | 5 | public function dashboard(Request $request, UserServiceProviderResolver $resolver, ActiveSessionsResolver $sessionsResolver): Response { |
|
| 29 | /** @var User $user */ |
||
| 30 | 5 | $user = $this->getUser(); |
|
| 31 | 5 | ||
| 32 | 5 | $services = $resolver->getServicesForCurrentUser(); |
|
| 33 | |||
| 34 | $form = null; |
||
| 35 | |||
| 36 | if($this->isGranted(LinkStudentVoter::LINK)) { |
||
| 37 | $form = $this->createForm(LinkStudentType::class); |
||
| 38 | $form->handleRequest($request); |
||
| 39 | } |
||
| 40 | |||
| 41 | return $this->render('dashboard/index.html.twig', [ |
||
| 42 | 'services' => $services, |
||
| 43 | 'students' => $user->getLinkedStudents(), |
||
| 44 | 'links_required' => $user->getType()->isCanLinkStudents() && $user->getLinkedStudents()->count() === 0, |
||
| 45 | 'form' => $form !== null ? $form->createView() : null, |
||
| 46 | 'sessions' => $sessionsResolver->getSessionsForUser($user) |
||
| 47 | ]); |
||
| 48 | } |
||
| 49 | |||
| 50 | #[Route(path: '/link', name: 'link_student')] |
||
| 51 | public function index(Request $request, UserRepositoryInterface $userRepository, DateHelper $dateHelper, |
||
| 52 | RegistrationCodeRepositoryInterface $codeRepository, TranslatorInterface $translator): Response { |
||
| 53 | $this->denyAccessUnlessGranted(LinkStudentVoter::LINK); |
||
| 54 | |||
| 55 | /** @var User $user */ |
||
| 56 | $user = $this->getUser(); |
||
| 57 | |||
| 58 | $form = $this->createForm(LinkStudentType::class); |
||
| 59 | $form->handleRequest($request); |
||
| 60 | |||
| 61 | if($form->isSubmitted() && $form->isValid() && !empty($form->get('code')->getData())) { |
||
| 62 | $code = $codeRepository->findOneByCode($form->get('code')->getData()); |
||
| 63 | |||
| 64 | if($code !== null) { |
||
| 65 | if($code->getValidFrom() !== null && $code->getValidFrom() > $dateHelper->getToday()) { |
||
| 66 | $this->addFlash('error', $translator->trans('register.redeem.error.not_yet_valid', [ |
||
| 67 | '%date%' => $code->getValidFrom()->format($translator->trans('date.format')) |
||
| 68 | ], 'security')); |
||
| 69 | } |
||
| 70 | else if($user->getLinkedStudents()->contains($code->getStudent())) { |
||
| 71 | $this->addFlash('error', 'link.student.error.already_linked'); |
||
| 72 | } if($code->getRedeemingUser() !== null) { |
||
| 73 | $this->addFlash('error', $translator->trans('register.redeem.error.already_redeemed', [], 'security')); |
||
| 74 | } else { |
||
| 75 | $user->addLinkedStudent($code->getStudent()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 76 | $code->setRedeemingUser($user); |
||
| 77 | |||
| 78 | $userRepository->persist($user); |
||
| 79 | $codeRepository->persist($code); |
||
| 80 | |||
| 81 | $this->addFlash('success', 'link.student.success'); |
||
| 82 | } |
||
| 83 | } else { |
||
| 84 | $this->addFlash('error', $translator->trans('register.redeem.error.not_found', [], 'security')); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | return $this->redirectToRoute('dashboard'); |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | #[Route('/destroy', name: 'destroy_sessions')] |
||
| 93 | public function destroySessions(LogoutHelper $helper): Response { |
||
| 94 | /** @var User $user */ |
||
| 95 | $user = $this->getUser(); |
||
| 96 | $helper->logout($user); |
||
| 97 | |||
| 98 | return $this->redirect('/'); |
||
| 99 | } |
||
| 100 | } |