Completed
Push — master ( 036366...035555 )
by Julito
12:18
created

SecurityController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Chamilo\CoreBundle\Entity\User;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Annotation\Route;
12
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
13
use Symfony\Component\Serializer\Encoder\JsonEncoder;
14
use Symfony\Component\Serializer\SerializerInterface;
15
16
/**
17
 * Class SecurityController.
18
 */
19
class SecurityController extends AbstractController
20
{
21
    /** @var SerializerInterface */
22
    private $serializer;
23
24
    public function __construct(SerializerInterface $serializer)
25
    {
26
        $this->serializer = $serializer;
27
    }
28
29
    /**
30
     * @Route("/login", name="login")
31
     */
32
    public function login(AuthenticationUtils $authenticationUtils): Response
33
    {
34
        $error = $authenticationUtils->getLastAuthenticationError();
35
        $lastUsername = $authenticationUtils->getLastUsername();
36
37
        $user = $this->getUser();
38
        $data = null;
39
        if (!empty($user)) {
40
            $userClone = clone $user;
41
            $userClone->setPassword('');
42
            $data = $this->serializer->serialize($userClone, JsonEncoder::FORMAT);
43
        }
44
45
        return $this->render('@ChamiloCore/Index/vue.html.twig', [
46
            'last_username' => $lastUsername,
47
            'is_authenticated' => json_encode(!empty($this->getUser())),
48
            'user' => $data ?? json_encode($data),
49
            'error' => $error,
50
        ]);
51
        /*return $this->render('@ChamiloCore/login.html.twig', [
52
            'last_username' => $lastUsername,
53
            'is_authenticated' => json_encode(!empty($this->getUser())),
54
            'user' => $data ?? json_encode($data),
55
            'error' => $error,
56
        ]);*/
57
    }
58
59
    /**
60
     * @Route("/login_json", name="login_json")
61
     */
62
    public function loginJson(AuthenticationUtils $authenticationUtils): Response
63
    {
64
        $error = $authenticationUtils->getLastAuthenticationError();
65
        $lastUsername = $authenticationUtils->getLastUsername();
66
67
        $user = $this->getUser();
68
        $data = null;
69
        if (!empty($user)) {
70
            $userClone = clone $user;
71
            $userClone->setPassword('');
72
            $data = $this->serializer->serialize($userClone, JsonEncoder::FORMAT);
73
        }
74
75
        /*return $this->render('@ChamiloCore/Index/courses.html.twig', [
76
            'last_username' => $lastUsername,
77
            'is_authenticated' => json_encode(!empty($this->getUser())),
78
            'error' => $error,
79
            'isAuthenticated' => json_encode(!empty($user)),
80
            'user' => $data ?? json_encode($data),
81
        ]);*/
82
83
        return $this->render('@ChamiloCore/login.html.twig', [
84
            'last_username' => $lastUsername,
85
            'is_authenticated' => json_encode(!empty($this->getUser())),
86
            'user' => $data ?? json_encode($data),
87
            'error' => $error,
88
        ]);
89
    }
90
}
91