|
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) { |
|
|
|
|
|
|
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")); |
|
|
|
|
|
|
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
|
|
|
|