<?php
namespace App\Controller\User;
use App\Security\Firewall\DefaultFirewall;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\User\Profile;
use App\Entity\User\User;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class DefaultController extends AbstractController
{
use TargetPathTrait;
#[Route('/user/loginSuccess', name: 'login_redirect_route', defaults: ['scope' => 'abacus-matematik'])]
public function redirectUser(Request $request, EntityManagerInterface $em): RedirectResponse
{
$scope = $this->getParameter('abacus.scope');
$request->getSession()->remove('login_method');
$user = $this->getUser();
$path = $this->getTargetPath($request->getSession(), DefaultFirewall::NAME);
if (
$this->isGranted('ROLE_ADMIN')
&& $user instanceof User
&& $user->getLastLoginWithUnilogin() === null // Avoid 2fa on our end for uni users
&& !$user->isTotpAuthenticationEnabled()
&& $_ENV['APP_ENV'] === 'prod' // Skip if not prod
) {
return $this->redirectToRoute('user_2fa_setup');
}
$path = str_replace("?_switch_user=_exit", "", $path ?? '');
if (!empty($path)) {
$redirectToPath = false;
$keyWords = ['textbook', 'teacher/index/quiz', 'teacher/index/peer'];
foreach ($keyWords as $word) {
if (strpos($path, $word) !== false) {
$redirectToPath = true;
break;
}
}
if ($redirectToPath) {
return $this->redirect($path);
}
}
if (empty($user)) {
return $this->redirectToRoute('uni_login_return_route');
}
if ($scope === 'gale') {
if ($this->isGranted('ROLE_AUTHOR')) {
// Using isGranted will use the full role hierarchy, which then includes:
// ROLE_SUPER_ADMIN <- ROLE_ADMIN <- ROLE_EDITOR <- ROLE_AUTHOR
return $this->redirectToRoute('easyadmin_dashboard');
}
else {
// access denied redirect to login session
return $this->redirect('/user/logout');
}
}
if ($this->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('a_ba_cus_admin_homepage');
}
if ($this->isGranted('ROLE_TEACHER')) {
return $this->redirectToRoute('a_ba_cus_teacher_homepage');
}
if ($this->isGranted('ROLE_STUDENT')) {
return $this->redirectToRoute('a_ba_cus_student_homepage');
}
return $this->redirectToRoute('abacus_core_homepage');
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
#[Route('/user/param/{key}', name: 'user_profile_param', methods: ['GET'])]
public function getParamRow(string $key, EntityManagerInterface $em): JsonResponse{
/* @var User $user */
$user = $this->container->get('security.token_storage')?->getToken()?->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
return new JsonResponse(["message"=>'This user does not have access to this section.'], 403);
}
$param = $em->getRepository(Profile::class)->findOneBy([
"user" => $user,
"paramKey" => $key
]);
//echo '<pre>'; print_r([$user->getId(), $key]); exit;
if(is_object($param)){
return new JsonResponse([
"status" => 200,
"data" =>[
"id" => $param->getId(),
"user" => [
"id" => $user->getId(),
"name" => $user->getFullName(),
"email" => $user->getEmail()
],
"param_key" => $param->getParamKey(),
"param_value" => $param->getParamValue(),
]
]);
}
else{
return new JsonResponse([
"status" => 404,
"message" => "No information is saved against this key"
]);
}
}
/**
* @param string $key
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
#[Route('/user/params', name: 'user_profile_params')]
public function getParams(EntityManagerInterface $em): JsonResponse{
$user = $this->container->get('security.token_storage')->getToken()->getUser();
/* @var User $user */
if (!is_object($user) || !$user instanceof UserInterface) {
//throw new AccessDeniedException('This user does not have access to this section.');
return new JsonResponse(["message"=>'This user does not have access to this section.'], 403);
}
$params = $em->getRepository(Profile::class)->findBy([
"user" => $user
]);
$profile = [];
foreach ($params as $param){
$profile[] = [
"id" => $param->getId(),
"param_key" => $param->getParamKey(),
"param_value" => $param->getParamValue(),
];
}
if(is_array($params)){
return new JsonResponse([
"status" => 200,
"data" => $profile,
"user" => [
"id" => $user->getId(),
"name" => $user->getFullName(),
"email" => $user->getEmail()
],
]);
}
else{
return new JsonResponse([
"status" => 404,
"message" => "No information found for this user"
]);
}
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
#[Route('/user/param/{key}', name: 'user_set_profile_param', methods: ['POST'])]
public function setParam(string $key, EntityManagerInterface $em, Request $request): JsonResponse{
$value = $request->get("value");
$user = $this->container->get('security.token_storage')->getToken()->getUser();
/* @var User $user */
if (!is_object($user) || !$user instanceof UserInterface) {
//throw new AccessDeniedException('This user does not have access to this section.');
return new JsonResponse(["message"=>'This user does not have access to this section.'], 403);
}
try {
$param = $em->getRepository(Profile::class)->findOneBy([
"user" => $user,
"paramKey" => $key
]);
if (!is_object($param)) {
$param = new Profile();
$param->setUser($user);
$param->setParamKey($key);
}
$param->setParamValue($value);
$em->persist($param);
$em->flush();
return new JsonResponse([
"status" => 200,
"message" => "Setting saved successfully"
]);
}
catch (Exception $ex){
return new JsonResponse([
"status" => 500,
"message" => $ex->getMessage()
]/*, 500*/);
}
}
#[Route('/user/test-param', name: 'user_profile_param_test')]
function testParam(){
return $this->render('param.html.twig');
}
}