<?php
namespace Knitink\CoreBundle\Controller;
use Knitink\CoreBundle\Domain\UserManager;
use Knitink\ManagementBundle\Domain\ClientManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class DefaultController extends AbstractController
{
public function login($token, Request $request, ClientManager $clientManager)
{
/*The user needs to be registered */
$client = $clientManager->searchOneBy(["token"=>$token]);
$user = null;
if($client){
$user = $client->getTokenUser();
}
// Check if the user exists !
if(!$user){
throw $this->createAccessDeniedException('Token doesnt exists');
/*
return new Response(
'Token doesnt exists',
Response::HTTP_UNAUTHORIZED
['Content-type' => 'application/json']
);
*/
}
//Handle getting or creating the user entity likely with a posted form
// The third parameter "main" can change according to the name of your firewall in security.yml
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
// If the firewall name is not main, then the set value would be instead:
// $this->get('session')->set('_security_XXXFIREWALLNAMEXXX', serialize($token));
$this->get('session')->set('_security_main', serialize($token));
// Fire the login event manually
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
/*
* Now the user is authenticated !!!!
* Do what you need to do now, like render a view, redirect to route etc.
*/
return $this->redirectToRoute("knk_core_index");
}
public function index(Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED'); // IS_AUTHENTICATED_FULLY
$user = $this->getUser();
if($user){
// Seteamos el idioma
$locale = (method_exists($user, 'getLocale')?$user->getLocale():null);
if($locale != null) {
$this->get('session')->set('_locale', $locale);
}
// Redirigimos al dashboard
return $this->redirectToRoute("knk_core_dashboard");
}else{
//return $this->redirectToRoute('globunet_user_security_login');
}
}
public function dashboard(UserManager $userManager)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
$response = null;
if ($this->isGranted('ROLE_KNK_UNIV') and $this->isGranted('ROLE_KNK_MNGT')){
$users = [];
if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
$users = $userManager->cgetByUser($this->getUser());
}
$response = $this->render('knitink/core_bundle/default/index.html.twig', ['users' => $users]);
} else if($this->isGranted('ROLE_KNK_MNGT_DEALER') or $this->isGranted('ROLE_KNK_MNGT_INSTALLER')){
$url = 'knk_core_management';
$response = $this->redirectToRoute($url);
}else if($this->isGranted('ROLE_KNK_MNGT_SUPPORT')){
$url = 'knk_core_control';
$response = $this->redirectToRoute($url);
}else{
$url = 'knk_core_universe';
$response = $this->redirectToRoute($url);
}
return $response;
}
public function universe()
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
$user = $this->getUser();
if($user){
$count = 0;
if($user instanceof \Knitink\CoreBundle\Entity\User){
$count = $user->getClients()->count();
}
if($this->isGranted('ROLE_KNK_UNIV_SUPER_ADMIN') || $count > 1 ){
$url = 'knk_univ_clients';
}else{
$this->get('session')->set('database', null);
$this->get('session')->set('client', null);
$url = 'knk_univ_home';
}
return $this->redirectToRoute($url);
}else{
return $this->redirectToRoute('globunet_user_security_login');
}
}
public function management()
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
$user = $this->getUser();
if($user){
$url = 'knk_mngt_home';
return $this->redirectToRoute($url);
}else{
return $this->redirectToRoute('globunet_user_security_login');
}
}
public function control()
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
$user = $this->getUser();
if($user){
$url = 'knk_ctrl_home';
return $this->redirectToRoute($url);
}else{
return $this->redirectToRoute('globunet_user_security_login');
}
}
public function locale(Request $request, $locale)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
//$locale = $request->getLocale();
$this->get('session')->set('_locale', $locale);
$referer = $request->headers->get('referer');
return $this->redirect($referer);
/*
$user = $this->getUser();
if($user){
return $this->redirectToRoute('knk_univ_home');
}else{
return $this->redirectToRoute('globunet_user_security_login');
}
*/
}
public function legal()
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
return $this->render('knitink/core_bundle/default/legal.html.twig', []);
}
public function dataProtectionPolicy()
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
return $this->render('knitink/core_bundle/default/data-protection-policy.html.twig', []);
}
public function cookies()
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
return $this->render('knitink/core_bundle/default/cookies-policy.html.twig', []);
}
public function cookiesAccept()
{
$request = Request::createFromGlobals();
$content = json_decode($request->getContent(), true);
if (isset($content['cookies-consent']) && $content['cookies-consent'] == 'ok') {
$expires = new \DateTime("now");
$dateInterval = new \DateInterval(sprintf('P%dD', 365));
$expires->add($dateInterval);
$cookie = new Cookie('knitink_accept_cookies_policy_consent','ok', $expires);
$jsonResponse = new JsonResponse(['success' => true]);
$jsonResponse->headers->setCookie($cookie);
return $jsonResponse;
} else {
return new JsonResponse(['success' => false], 400);
}
}
}