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
|
|
|
$encoder = $this->get("security.password_encoder"); |
|
|
|
|
36
|
|
|
|
37
|
|
|
if ($encoder->getEncoder($account)->isPasswordValid($account->getPassword(), $request->get("password"), '') === true) { |
38
|
|
|
if ($account->getisActive() == false) { |
39
|
|
|
return new JsonResponse([ |
40
|
|
|
"success" => false, |
41
|
|
|
"message" => "You account is disabled" |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return new JsonResponse([ |
46
|
|
|
"success" => true, |
47
|
|
|
"token" => $api->getToken($account) |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return new JsonResponse([ |
53
|
|
|
"success" => false, |
54
|
|
|
"message" => "bad identifiant and/or password" |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* method that test if user steel logged and send token or new token if it was expired |
60
|
|
|
* @Route("/api/users/test-token", name="ribsadmin_api_test_token", methods={"POST"}) |
61
|
|
|
* @param Request $request |
62
|
|
|
* @param Api $api |
63
|
|
|
* @param Session $session |
64
|
|
|
* @return JsonResponse |
65
|
|
|
* @throws \Exception |
66
|
|
|
*/ |
67
|
|
|
public function testUserToken(Request $request, Api $api, Session $session): JsonResponse |
68
|
|
|
{ |
69
|
|
|
$test_logged = $api->userIslogged($request->get("infos"), $request->get("token")); |
|
|
|
|
70
|
|
|
|
71
|
|
|
if ($test_logged === false) { |
72
|
|
|
return new JsonResponse([ |
73
|
|
|
"success" => $test_logged, |
74
|
|
|
"error_message" => "Votre compte a été archivé, vous ne pouvez plus vous connecter", |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return new JsonResponse([ |
79
|
|
|
"success" => $test_logged, |
80
|
|
|
"token" => $api->getToken($session->get("account")), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.