src/Controller/Engine/LoginController.php line 201

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Engine;
  3. use App\Entity\User;
  4. use App\Repository\BaseClientSetupRepository;
  5. use App\Repository\ThemeRepository;
  6. use App\Service\AuthenticationService;
  7. use App\Service\BaseEnvService;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use App\Repository\AvatarRepository;
  14. use Symfony\Component\Yaml\Yaml;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  16. class LoginController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/login_endpoint", name="login_endpoint",  options={"expose"=true})
  20.      * @param AuthenticationService $authentication
  21.      * @return JsonResponse
  22.      */
  23.     public function login(AuthenticationService $authentication): JsonResponse
  24.     {
  25.         $user $this->getUser();
  26.         return $this->json([
  27.             'username' => $user->getUsername(),
  28.             'roles' => $authentication->getUserRoleHierarchy($user),
  29.         ]);
  30.     }
  31.     /**
  32.      * readSystemUser()
  33.      * This adds a safety net around the user object, so we only return to the client the information they need.
  34.      * @Route("/api/profile/read_system_user", name="profile_Read_system_user", methods={"GET"}, options={"expose"=true})
  35.      * @param AvatarRepository $avatarRepo
  36.      * @param AuthenticationService $authentication
  37.      * @return JsonResponse
  38.      */
  39.     public function readSystemUser(
  40.         AvatarRepository $avatarRepo,
  41.         BaseEnvService $baseEnvService,
  42.         ThemeRepository $themeRepo,
  43.         AuthenticationService $authentication,
  44.         Request $request
  45.     ): JsonResponse {
  46.         /** @var User $user */
  47.         $user $this->getUser();
  48.         if ($user == null) {
  49.             $id null;
  50.             $user 'unknown';
  51.             $username 'unknown';
  52.             $email 'no-reply@si.te';
  53.             $avatar null;
  54.             $chatOpen false;
  55.         } else { // we use this pattern here as exposing user data to the view can be dangerous, the return value of the getUser method may change over time so by expressely revealing only those columns we wish to expose we prevent much of the risk
  56.             $id $user->getId();
  57.             $username $user->getUsername();
  58.             $email $user->getEmail();
  59.             $nicename $user->getNicename();
  60.             $gdpr $user->getGpdr();
  61.             if(null != $user->getProfileImage()){
  62.                 $profileImage $user->getProfileImage()->getId();
  63.             } else {
  64.                 $profileImage null;
  65.             }
  66.             $avatarEntry $avatarRepo->findOneBy(['user' => $id]);
  67.             $avatar null;
  68.             
  69.             $theme $baseEnvService->getClientTheme();
  70.             $lc $themeRepo->findOneBy(['owner' => $id]);
  71.             if($avatarEntry !== null){
  72.                 if($avatarEntry->getHair() !== null){
  73.                     $hair $avatarEntry->getHair()->getImage()->getImagePath();
  74.                 } else {
  75.                     $hair '/public/profile-pic.png';
  76.                 }
  77.                 if($avatarEntry->getBling() !== null){
  78.                     $bling $avatarEntry->getBling()->getImage()->getImagePath();
  79.                 } else {
  80.                     $bling '/public/profile-pic.png';
  81.                 }
  82.                 if($avatarEntry->getSkin() !== null){
  83.                     $skin $avatarEntry->getSkin()->getImage()->getImagePath();
  84.                 } else {
  85.                     $skin '/public/profile-pic.png';
  86.                 }
  87.                 if($avatarEntry->getOutfit() !== null){
  88.                     $outfit $avatarEntry->getOutfit()->getImage()->getImagePath();
  89.                 } else {
  90.                     $outfit '/public/profile-pic.png';
  91.                 }
  92.                 $avatarParts = [
  93.                     "color" => "#c3c3c3",
  94.                     "skin" => $skin,
  95.                     "outfit" => $outfit,
  96.                     "hair" => $hair,
  97.                     "bling" => $bling,
  98.                 ];
  99.             } else {
  100.                 $avatarParts = [
  101.                     "color" => "#c3c3c3",
  102.                     "skin" => '/profile-pic.png',
  103.                     "outfit" => '/profile-pic.png',
  104.                     "hair" => '/profile-pic.png',
  105.                     "bling" => '/profile-pic.png',
  106.                 ];
  107.             }
  108.             $language $baseEnvService->getLanguage();
  109.             if (null !== $user->getLanguage()) {
  110.                 $language $user->getLanguage();
  111.             }
  112.             if(null !== $avatarEntry){
  113.                 $avatar $avatarParts;
  114.             } else {
  115.             }
  116.             $roles $authentication->getUserRoleHierarchy($user);
  117.             
  118.             if(false == filter_var($baseEnvService->chatOpenRemember(), FILTER_VALIDATE_BOOLEAN)){
  119.                 $chatOpen filter_var($baseEnvService->chatOpenDefault(), FILTER_VALIDATE_BOOLEAN);
  120.                 // dd('one');
  121.             } else {
  122.                     $chatOpen filter_var($user->getChatOpen(), FILTER_VALIDATE_BOOLEAN);
  123.                 // if(null != $user->getChatOpen()){
  124.                 // dd('two', $user->getChatOpen(), $chatOpen);
  125.                                 // } else {
  126.                 // dd('tre');
  127.                     // $chatOpen = filter_var($baseEnvService->chatOpenDefault(), FILTER_VALIDATE_BOOLEAN);
  128.                 // }
  129.             }
  130.         }
  131.         $apiFilters $request->getSession()->get('apiFilters'null); // base3-sys1127
  132.         return $this->json([
  133.             'id' => $id,
  134.             'avatar' => $avatar,
  135.             'nicename' => $nicename,
  136.             'working' => true,
  137.             'username' => $username,
  138.             'email' => $email,
  139.             'roles' => $roles,
  140.             'gdpr' => $gdpr,
  141.             'apiFilters' => $apiFilters,
  142.             'theme' => $theme,
  143.             'profileImage' => $profileImage,
  144.             'language' => $language,
  145.             'chatOpen' => $chatOpen,
  146.         ]);
  147.     }
  148.     /**
  149.      * @Route("/api/profile/update_chat_state", name="profile_update_chat_state", options={"expose"=true})
  150.      * @IsGranted("ROLE_USER")
  151.      * @param ActivityTouchpoint $activityTouchpoint
  152.      * @param EntityManagerInterface $em
  153.      * @param Request $request
  154.      * @return JsonResponse
  155.      */
  156.     public function updateChatState(Request $request)
  157.     {
  158.         $data json_decode($request->getContent(), true);
  159.         $entityManager $this->getDoctrine()->getManager();
  160.         $user $entityManager->getRepository(User::class)->find($this->getUser()->getId());
  161.         if (!$user) {
  162.             throw $this->createNotFoundException(
  163.                 'No User Found'
  164.             );
  165.         }
  166.         $user->setChatOpen($data['chatOpen']);
  167.         $entityManager->flush();
  168.         return $this->json(201);
  169.     }
  170.     /**
  171.      * @Route("/load-translation", name="load_translation", methods="GET", options={"expose"=true})
  172.      * @return Response
  173.      */
  174.     public function loadTranslation(BaseEnvService $baseEnvServiceRequest $request)
  175.     {
  176.         $locale $baseEnvService->getLanguage();
  177.         if (null !== $request->getSession()->get('_locale')) {
  178.             $locale $request->getSession()->get('_locale');
  179.         }
  180.         try {
  181.             /* eg $tranlationsorigin = [
  182.              *       "Home" => "액션_Home",
  183.              *    ]; 
  184.              */
  185.             $translations Yaml::parseFile("../translations/messages.{$locale}.yaml");
  186.         } catch(ParseException $exception){
  187.             printf('Unable to parse the YAML string: %s'$exception->getMessage());
  188.         }
  189.         return new JsonResponse($translations);
  190.         
  191.     }
  192.     /**
  193.      *@Method({"POST"})
  194.      *@Route("/update_language", name="update_language", options={"expose"=true})
  195.      */
  196.     public function updateLanguage(BaseEnvService $besBaseClientSetupRepository $besRepoRequest $request)
  197.     {
  198.         // $clientId = $bes->getClient()['id'];
  199.         // $client = $besRepo->find($clientId);
  200.         // $client->setLanguage($data['lang']);
  201.         $data json_decode($request->getContent(), true) ?? [];
  202.         $user $this->getUser();
  203.         $user->setLanguage($data['lang']);
  204.         $entityManager $this->getDoctrine()->getManager();
  205.         $entityManager->flush();
  206.         
  207.         return $this->json(['message'=>"Thanks, we have updated your language to ","language"=>$data['lang']]);
  208.     }
  209.     /**
  210.      * gpdrOnboard()
  211.      * This adds a safety net around the user object, so we only return to the client the information they need.
  212.      * could be used to harness the onboardinig flow based on user settings
  213.      *@Method({"GET"})
  214.      *@Route("/gdpr_onboard", name="gdpr_onboard", options={"expose"=true})
  215.      */
  216.     public function gpdrOnboard(Request $request)
  217.     {
  218.         $user $this->getUser();
  219.         $id $user->getId();
  220.         $entityManager $this->getDoctrine()->getManager();
  221.         $product $entityManager->getRepository(User::class)->find($id);
  222.         if (!$product) {
  223.             throw $this->createNotFoundException(
  224.                 'No product found for id '.$id
  225.             );
  226.         }
  227.         $product->setGdpr(1);
  228.         $entityManager->flush();
  229.         return new JsonResponse(201);
  230.     }
  231.     /**
  232.      * saveGdpr()
  233.      * This adds a safety net around the user object, so we only return to the client the information they need.
  234.      *@Method({"GET"})
  235.      *@Route("/save_nicename", name="save_nicename", options={"expose"=true})
  236.      */
  237.     public function saveGdpr(Request $request)
  238.     {
  239.         $data json_decode($request->getContent(), true) ?? [];
  240.         
  241.         $submittedToken $data['token'];
  242.         // 'delete-item' is the same value used in the template to generate the token
  243.         if ($this->isCsrfTokenValid('save-nicename'$submittedToken)) {
  244.             $request->request->replace($data);
  245.             // dd($data['nicename']);
  246.             $user $this->getUser();
  247.             $id $user->getId();
  248.             $entityManager $this->getDoctrine()->getManager();
  249.             $product $entityManager->getRepository(User::class)->find($id);
  250.             if (!$product) {
  251.                 throw $this->createNotFoundException(
  252.                     'No product found for id '.$id
  253.                 );
  254.             }
  255.             $product->setGdpr(true);
  256.             $entityManager->flush();
  257.             return new JsonResponse(201);
  258.         } else {
  259.             return new JsonResponse(500);
  260.         }
  261.         // return $this->redirectToRoute('product_show', [
  262.         //     'id' => $product->getId()
  263.         // ]);
  264.         // // $user = $this->getUser();
  265.         // $entityManager = $this->getDoctrine()->getManager();
  266.         // dd($slug)
  267.         // // $user = new User();
  268.         // // $user->setUser(1);
  269.         // // tell Doctrine you want to (eventually) save the Product (no queries yet)
  270.         // $entityManager->persist($avatar);
  271.     }
  272. }