ApiController::testUserToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Controller;
4
5
use PiouPiou\RibsAdminBundle\Entity\Account;
6
use PiouPiou\RibsAdminBundle\Service\Api;
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Session\Session;
11
use Symfony\Component\Routing\Annotation\Route;
12
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
13
14
class ApiController extends AbstractController
15
{
16
    /**
17
     * this method is user to authenticate a user by an api request
18
     * if success it return a token api that expire in 20 minutes
19
     * @Route("/api/users/authenticate", name="ribsadmin_api_login", methods={"POST"})
20
     * @param Request $request
21
     * @param Api $api
22
     * @param EncoderFactoryInterface $encoder
23
     * @return JsonResponse
24
     * @throws \Exception
25
     */
26
    public function login(Request $request, Api $api, EncoderFactoryInterface $encoder): JsonResponse
27
    {
28
        $em = $this->getDoctrine()->getManager();
29
30
        $account = $em->getRepository(Account::class)->findOneBy([
31
            "username" => $request->get("username"),
32
        ]);
33
34
        if ($account) {
35
            if ($encoder->getEncoder($account)->isPasswordValid($account->getPassword(), $request->get("password"), '') === true) {
0 ignored issues
show
Bug introduced by
It seems like $request->get('password') can also be of type null; however, parameter $raw of Symfony\Component\Securi...face::isPasswordValid() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            if ($encoder->getEncoder($account)->isPasswordValid($account->getPassword(), /** @scrutinizer ignore-type */ $request->get("password"), '') === true) {
Loading history...
36
                if ($account->getisActive() == false) {
37
                    return new JsonResponse([
38
                        "success" => false,
39
                        "error_message" => "You account is disabled"
40
                    ]);
41
                }
42
43
                return new JsonResponse([
44
                    "success" => true,
45
                    "token" => $api->getToken($account)
46
                ]);
47
            }
48
        }
49
50
        return new JsonResponse([
51
            "success" => false,
52
            "error_message" => "bad identifiant and/or password"
53
        ]);
54
    }
55
56
    /**
57
     * method that test if user steel logged and send token or new token if it was expired
58
     * @Route("/api/users/test-token", name="ribsadmin_api_test_token", methods={"POST"})
59
     * @param Request $request
60
     * @param Api $api
61
     * @param Session $session
62
     * @return JsonResponse
63
     * @throws \Exception
64
     */
65
    public function testUserToken(Request $request, Api $api, Session $session): JsonResponse
66
    {
67
        $test_logged = $api->userIslogged($request->get("infos"), $request->get("token"));
0 ignored issues
show
Bug introduced by
It seems like $request->get('infos') can also be of type null; however, parameter $infos_jwt of PiouPiou\RibsAdminBundle...ice\Api::userIslogged() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $test_logged = $api->userIslogged(/** @scrutinizer ignore-type */ $request->get("infos"), $request->get("token"));
Loading history...
Bug introduced by
It seems like $request->get('token') can also be of type null; however, parameter $token of PiouPiou\RibsAdminBundle...ice\Api::userIslogged() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $test_logged = $api->userIslogged($request->get("infos"), /** @scrutinizer ignore-type */ $request->get("token"));
Loading history...
68
69
        if ($test_logged === false) {
70
            return new JsonResponse([
71
                "success" => $test_logged,
72
                "error_message" => "Votre compte a été archivé, vous ne pouvez plus vous connecter",
73
            ]);
74
        }
75
76
        return new JsonResponse([
77
            "success" => $test_logged,
78
            "token" => $api->getToken($session->get("account")),
79
        ]);
80
    }
81
}
82