src/Controller/User/DefaultController.php line 139

Open in your IDE?
  1. <?php
  2. namespace App\Controller\User;
  3. use App\Security\Firewall\DefaultFirewall;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use App\Entity\User\Profile;
  6. use App\Entity\User\User;
  7. use Exception;
  8. use Psr\Container\ContainerExceptionInterface;
  9. use Psr\Container\NotFoundExceptionInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  17. class DefaultController extends AbstractController
  18. {
  19. use TargetPathTrait;
  20. #[Route('/user/loginSuccess', name: 'login_redirect_route', defaults: ['scope' => 'abacus-matematik'])]
  21. public function redirectUser(Request $request, EntityManagerInterface $em): RedirectResponse
  22. {
  23. $scope = $this->getParameter('abacus.scope');
  24. $request->getSession()->remove('login_method');
  25. $user = $this->getUser();
  26. $path = $this->getTargetPath($request->getSession(), DefaultFirewall::NAME);
  27. if (
  28. $this->isGranted('ROLE_ADMIN')
  29. && $user instanceof User
  30. && $user->getLastLoginWithUnilogin() === null // Avoid 2fa on our end for uni users
  31. && !$user->isTotpAuthenticationEnabled()
  32. && $_ENV['APP_ENV'] === 'prod' // Skip if not prod
  33. ) {
  34. return $this->redirectToRoute('user_2fa_setup');
  35. }
  36. $path = str_replace("?_switch_user=_exit", "", $path ?? '');
  37. if (!empty($path)) {
  38. $redirectToPath = false;
  39. $keyWords = ['textbook', 'teacher/index/quiz', 'teacher/index/peer'];
  40. foreach ($keyWords as $word) {
  41. if (strpos($path, $word) !== false) {
  42. $redirectToPath = true;
  43. break;
  44. }
  45. }
  46. if ($redirectToPath) {
  47. return $this->redirect($path);
  48. }
  49. }
  50. if (empty($user)) {
  51. return $this->redirectToRoute('uni_login_return_route');
  52. }
  53. if ($scope === 'gale') {
  54. if ($this->isGranted('ROLE_AUTHOR')) {
  55. // Using isGranted will use the full role hierarchy, which then includes:
  56. // ROLE_SUPER_ADMIN <- ROLE_ADMIN <- ROLE_EDITOR <- ROLE_AUTHOR
  57. return $this->redirectToRoute('easyadmin_dashboard');
  58. }
  59. else {
  60. // access denied redirect to login session
  61. return $this->redirect('/user/logout');
  62. }
  63. }
  64. if ($this->isGranted('ROLE_ADMIN')) {
  65. return $this->redirectToRoute('a_ba_cus_admin_homepage');
  66. }
  67. if ($this->isGranted('ROLE_TEACHER')) {
  68. return $this->redirectToRoute('a_ba_cus_teacher_homepage');
  69. }
  70. if ($this->isGranted('ROLE_STUDENT')) {
  71. return $this->redirectToRoute('a_ba_cus_student_homepage');
  72. }
  73. return $this->redirectToRoute('abacus_core_homepage');
  74. }
  75. /**
  76. * @throws ContainerExceptionInterface
  77. * @throws NotFoundExceptionInterface
  78. */
  79. #[Route('/user/param/{key}', name: 'user_profile_param', methods: ['GET'])]
  80. public function getParamRow(string $key, EntityManagerInterface $em): JsonResponse{
  81. /* @var User $user */
  82. $user = $this->container->get('security.token_storage')?->getToken()?->getUser();
  83. if (!is_object($user) || !$user instanceof UserInterface) {
  84. return new JsonResponse(["message"=>'This user does not have access to this section.'], 403);
  85. }
  86. $param = $em->getRepository(Profile::class)->findOneBy([
  87. "user" => $user,
  88. "paramKey" => $key
  89. ]);
  90. //echo '<pre>'; print_r([$user->getId(), $key]); exit;
  91. if(is_object($param)){
  92. return new JsonResponse([
  93. "status" => 200,
  94. "data" =>[
  95. "id" => $param->getId(),
  96. "user" => [
  97. "id" => $user->getId(),
  98. "name" => $user->getFullName(),
  99. "email" => $user->getEmail()
  100. ],
  101. "param_key" => $param->getParamKey(),
  102. "param_value" => $param->getParamValue(),
  103. ]
  104. ]);
  105. }
  106. else{
  107. return new JsonResponse([
  108. "status" => 404,
  109. "message" => "No information is saved against this key"
  110. ]);
  111. }
  112. }
  113. /**
  114. * @param string $key
  115. * @throws ContainerExceptionInterface
  116. * @throws NotFoundExceptionInterface
  117. */
  118. #[Route('/user/params', name: 'user_profile_params')]
  119. public function getParams(EntityManagerInterface $em): JsonResponse{
  120. $user = $this->container->get('security.token_storage')->getToken()->getUser();
  121. /* @var User $user */
  122. if (!is_object($user) || !$user instanceof UserInterface) {
  123. //throw new AccessDeniedException('This user does not have access to this section.');
  124. return new JsonResponse(["message"=>'This user does not have access to this section.'], 403);
  125. }
  126. $params = $em->getRepository(Profile::class)->findBy([
  127. "user" => $user
  128. ]);
  129. $profile = [];
  130. foreach ($params as $param){
  131. $profile[] = [
  132. "id" => $param->getId(),
  133. "param_key" => $param->getParamKey(),
  134. "param_value" => $param->getParamValue(),
  135. ];
  136. }
  137. if(is_array($params)){
  138. return new JsonResponse([
  139. "status" => 200,
  140. "data" => $profile,
  141. "user" => [
  142. "id" => $user->getId(),
  143. "name" => $user->getFullName(),
  144. "email" => $user->getEmail()
  145. ],
  146. ]);
  147. }
  148. else{
  149. return new JsonResponse([
  150. "status" => 404,
  151. "message" => "No information found for this user"
  152. ]);
  153. }
  154. }
  155. /**
  156. * @throws ContainerExceptionInterface
  157. * @throws NotFoundExceptionInterface
  158. */
  159. #[Route('/user/param/{key}', name: 'user_set_profile_param', methods: ['POST'])]
  160. public function setParam(string $key, EntityManagerInterface $em, Request $request): JsonResponse{
  161. $value = $request->get("value");
  162. $user = $this->container->get('security.token_storage')->getToken()->getUser();
  163. /* @var User $user */
  164. if (!is_object($user) || !$user instanceof UserInterface) {
  165. //throw new AccessDeniedException('This user does not have access to this section.');
  166. return new JsonResponse(["message"=>'This user does not have access to this section.'], 403);
  167. }
  168. try {
  169. $param = $em->getRepository(Profile::class)->findOneBy([
  170. "user" => $user,
  171. "paramKey" => $key
  172. ]);
  173. if (!is_object($param)) {
  174. $param = new Profile();
  175. $param->setUser($user);
  176. $param->setParamKey($key);
  177. }
  178. $param->setParamValue($value);
  179. $em->persist($param);
  180. $em->flush();
  181. return new JsonResponse([
  182. "status" => 200,
  183. "message" => "Setting saved successfully"
  184. ]);
  185. }
  186. catch (Exception $ex){
  187. return new JsonResponse([
  188. "status" => 500,
  189. "message" => $ex->getMessage()
  190. ]/*, 500*/);
  191. }
  192. }
  193. #[Route('/user/test-param', name: 'user_profile_param_test')]
  194. function testParam(){
  195. return $this->render('param.html.twig');
  196. }
  197. }