<?php
namespace App\Controller\Engine;
use App\Entity\User;
use App\Repository\BaseClientSetupRepository;
use App\Repository\ThemeRepository;
use App\Service\AuthenticationService;
use App\Service\BaseEnvService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\AvatarRepository;
use Symfony\Component\Yaml\Yaml;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
class LoginController extends AbstractController
{
/**
* @Route("/login_endpoint", name="login_endpoint", options={"expose"=true})
* @param AuthenticationService $authentication
* @return JsonResponse
*/
public function login(AuthenticationService $authentication): JsonResponse
{
$user = $this->getUser();
return $this->json([
'username' => $user->getUsername(),
'roles' => $authentication->getUserRoleHierarchy($user),
]);
}
/**
* readSystemUser()
* This adds a safety net around the user object, so we only return to the client the information they need.
* @Route("/api/profile/read_system_user", name="profile_Read_system_user", methods={"GET"}, options={"expose"=true})
* @param AvatarRepository $avatarRepo
* @param AuthenticationService $authentication
* @return JsonResponse
*/
public function readSystemUser(
AvatarRepository $avatarRepo,
BaseEnvService $baseEnvService,
ThemeRepository $themeRepo,
AuthenticationService $authentication,
Request $request
): JsonResponse {
/** @var User $user */
$user = $this->getUser();
if ($user == null) {
$id = null;
$user = 'unknown';
$username = 'unknown';
$email = 'no-reply@si.te';
$avatar = null;
$chatOpen = false;
} 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
$id = $user->getId();
$username = $user->getUsername();
$email = $user->getEmail();
$nicename = $user->getNicename();
$gdpr = $user->getGpdr();
if(null != $user->getProfileImage()){
$profileImage = $user->getProfileImage()->getId();
} else {
$profileImage = null;
}
$avatarEntry = $avatarRepo->findOneBy(['user' => $id]);
$avatar = null;
$theme = $baseEnvService->getClientTheme();
$lc = $themeRepo->findOneBy(['owner' => $id]);
if($avatarEntry !== null){
if($avatarEntry->getHair() !== null){
$hair = $avatarEntry->getHair()->getImage()->getImagePath();
} else {
$hair = '/public/profile-pic.png';
}
if($avatarEntry->getBling() !== null){
$bling = $avatarEntry->getBling()->getImage()->getImagePath();
} else {
$bling = '/public/profile-pic.png';
}
if($avatarEntry->getSkin() !== null){
$skin = $avatarEntry->getSkin()->getImage()->getImagePath();
} else {
$skin = '/public/profile-pic.png';
}
if($avatarEntry->getOutfit() !== null){
$outfit = $avatarEntry->getOutfit()->getImage()->getImagePath();
} else {
$outfit = '/public/profile-pic.png';
}
$avatarParts = [
"color" => "#c3c3c3",
"skin" => $skin,
"outfit" => $outfit,
"hair" => $hair,
"bling" => $bling,
];
} else {
$avatarParts = [
"color" => "#c3c3c3",
"skin" => '/profile-pic.png',
"outfit" => '/profile-pic.png',
"hair" => '/profile-pic.png',
"bling" => '/profile-pic.png',
];
}
$language = $baseEnvService->getLanguage();
if (null !== $user->getLanguage()) {
$language = $user->getLanguage();
}
if(null !== $avatarEntry){
$avatar = $avatarParts;
} else {
}
$roles = $authentication->getUserRoleHierarchy($user);
if(false == filter_var($baseEnvService->chatOpenRemember(), FILTER_VALIDATE_BOOLEAN)){
$chatOpen = filter_var($baseEnvService->chatOpenDefault(), FILTER_VALIDATE_BOOLEAN);
// dd('one');
} else {
$chatOpen = filter_var($user->getChatOpen(), FILTER_VALIDATE_BOOLEAN);
// if(null != $user->getChatOpen()){
// dd('two', $user->getChatOpen(), $chatOpen);
// } else {
// dd('tre');
// $chatOpen = filter_var($baseEnvService->chatOpenDefault(), FILTER_VALIDATE_BOOLEAN);
// }
}
}
$apiFilters = $request->getSession()->get('apiFilters', null); // base3-sys1127
return $this->json([
'id' => $id,
'avatar' => $avatar,
'nicename' => $nicename,
'working' => true,
'username' => $username,
'email' => $email,
'roles' => $roles,
'gdpr' => $gdpr,
'apiFilters' => $apiFilters,
'theme' => $theme,
'profileImage' => $profileImage,
'language' => $language,
'chatOpen' => $chatOpen,
]);
}
/**
* @Route("/api/profile/update_chat_state", name="profile_update_chat_state", options={"expose"=true})
* @IsGranted("ROLE_USER")
* @param ActivityTouchpoint $activityTouchpoint
* @param EntityManagerInterface $em
* @param Request $request
* @return JsonResponse
*/
public function updateChatState(Request $request)
{
$data = json_decode($request->getContent(), true);
$entityManager = $this->getDoctrine()->getManager();
$user = $entityManager->getRepository(User::class)->find($this->getUser()->getId());
if (!$user) {
throw $this->createNotFoundException(
'No User Found'
);
}
$user->setChatOpen($data['chatOpen']);
$entityManager->flush();
return $this->json(201);
}
/**
* @Route("/load-translation", name="load_translation", methods="GET", options={"expose"=true})
* @return Response
*/
public function loadTranslation(BaseEnvService $baseEnvService, Request $request)
{
$locale = $baseEnvService->getLanguage();
if (null !== $request->getSession()->get('_locale')) {
$locale = $request->getSession()->get('_locale');
}
try {
/* eg $tranlationsorigin = [
* "Home" => "액션_Home",
* ];
*/
$translations = Yaml::parseFile("../translations/messages.{$locale}.yaml");
} catch(ParseException $exception){
printf('Unable to parse the YAML string: %s', $exception->getMessage());
}
return new JsonResponse($translations);
}
/**
*@Method({"POST"})
*@Route("/update_language", name="update_language", options={"expose"=true})
*/
public function updateLanguage(BaseEnvService $bes, BaseClientSetupRepository $besRepo, Request $request)
{
// $clientId = $bes->getClient()['id'];
// $client = $besRepo->find($clientId);
// $client->setLanguage($data['lang']);
$data = json_decode($request->getContent(), true) ?? [];
$user = $this->getUser();
$user->setLanguage($data['lang']);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
return $this->json(['message'=>"Thanks, we have updated your language to ","language"=>$data['lang']]);
}
/**
* gpdrOnboard()
* This adds a safety net around the user object, so we only return to the client the information they need.
* could be used to harness the onboardinig flow based on user settings
*@Method({"GET"})
*@Route("/gdpr_onboard", name="gdpr_onboard", options={"expose"=true})
*/
public function gpdrOnboard(Request $request)
{
$user = $this->getUser();
$id = $user->getId();
$entityManager = $this->getDoctrine()->getManager();
$product = $entityManager->getRepository(User::class)->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
$product->setGdpr(1);
$entityManager->flush();
return new JsonResponse(201);
}
/**
* saveGdpr()
* This adds a safety net around the user object, so we only return to the client the information they need.
*@Method({"GET"})
*@Route("/save_nicename", name="save_nicename", options={"expose"=true})
*/
public function saveGdpr(Request $request)
{
$data = json_decode($request->getContent(), true) ?? [];
$submittedToken = $data['token'];
// 'delete-item' is the same value used in the template to generate the token
if ($this->isCsrfTokenValid('save-nicename', $submittedToken)) {
$request->request->replace($data);
// dd($data['nicename']);
$user = $this->getUser();
$id = $user->getId();
$entityManager = $this->getDoctrine()->getManager();
$product = $entityManager->getRepository(User::class)->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
$product->setGdpr(true);
$entityManager->flush();
return new JsonResponse(201);
} else {
return new JsonResponse(500);
}
// return $this->redirectToRoute('product_show', [
// 'id' => $product->getId()
// ]);
// // $user = $this->getUser();
// $entityManager = $this->getDoctrine()->getManager();
// dd($slug)
// // $user = new User();
// // $user->setUser(1);
// // tell Doctrine you want to (eventually) save the Product (no queries yet)
// $entityManager->persist($avatar);
}
}