|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Users\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
|
6
|
|
|
use App\Users\Entity\ConfirmationToken; |
|
7
|
|
|
use App\Users\Entity\User; |
|
8
|
|
|
use App\Users\Repository\ApiTokenRepository; |
|
9
|
|
|
use App\Users\Repository\ConfirmationTokenRepository; |
|
10
|
|
|
use App\Users\Repository\UserRepository; |
|
11
|
|
|
use App\Users\Request\ChangePasswordRequest; |
|
12
|
|
|
use App\Users\Request\PasswordRecoveryRequest; |
|
13
|
|
|
use App\Users\Service\ConfirmationTokenService; |
|
14
|
|
|
use App\Users\Service\SendEmailService; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
|
17
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
18
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
19
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity; |
|
21
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
22
|
|
|
|
|
23
|
|
|
class UserPasswordController extends BaseController |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @Route("/api/users/{id}/password", methods={"POST"}, requirements={"id"="\d+"}) |
|
27
|
|
|
* @param ChangePasswordRequest $request |
|
28
|
|
|
* @param User $user |
|
29
|
|
|
* @param UserPasswordEncoderInterface $passwordEncoder |
|
30
|
|
|
* @return JsonResponse |
|
31
|
|
|
*/ |
|
32
|
|
|
public function postUserPassword(ChangePasswordRequest $request, User $user, UserPasswordEncoderInterface $passwordEncoder) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
|
35
|
|
|
|
|
36
|
|
|
if ($user->getId() !== $this->getUser()->getId()) { |
|
37
|
|
|
throw new AccessDeniedHttpException(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($user->isPasswordValid($request->get('old_password'), $passwordEncoder) === false) { |
|
41
|
|
|
throw new BadRequestHttpException(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$user->setPlainPassword($request->get('new_password')); |
|
45
|
|
|
$this->getDoctrine()->getManager()->flush(); |
|
46
|
|
|
|
|
47
|
|
|
return new JsonResponse(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @Route("/api/users/{email}/recoverPassword", methods={"GET"}) |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function getRecoverPassword(string $email, UserRepository $users, ConfirmationTokenRepository $tokens, SendEmailService $emailService) |
|
54
|
|
|
{ |
|
55
|
3 |
|
if (null === $user = $users->loadUserByEmail($email)) { |
|
56
|
1 |
|
throw new NotFoundHttpException(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
if ($this->getUser() !== null) { |
|
60
|
1 |
|
throw new AccessDeniedHttpException(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
$token = $tokens->findByUserAndType($user, ConfirmationToken::TYPE_PASSWORD_RECOVERY); |
|
64
|
|
|
|
|
65
|
1 |
|
if ($token !== null) { |
|
66
|
|
|
return $this->json([ |
|
67
|
|
|
'status' => 'token_already_sent' |
|
68
|
|
|
]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
$emailService->sendPasswordRecoveryConfirmation($user); |
|
72
|
|
|
|
|
73
|
1 |
|
return $this->json([ |
|
74
|
1 |
|
'status' => 'success', |
|
75
|
|
|
]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @Route("/api/passwordRecovery", methods={"POST"}); |
|
80
|
|
|
* |
|
81
|
|
|
* @param PasswordRecoveryRequest $request |
|
82
|
|
|
* @param ConfirmationTokenRepository $tokenRepository |
|
83
|
|
|
* @param ApiTokenRepository $apiTokenRepository |
|
84
|
|
|
* |
|
85
|
|
|
* @return JsonResponse |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function postRecoverPassword(PasswordRecoveryRequest $request, ConfirmationTokenRepository $tokenRepository, ApiTokenRepository $apiTokenRepository) |
|
88
|
|
|
{ |
|
89
|
2 |
|
if ($this->getUser() !== null) { |
|
90
|
|
|
throw new AccessDeniedHttpException(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
if (null === $token = $tokenRepository->findByToken($request->get('token'))) { |
|
94
|
|
|
throw new NotFoundHttpException(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
$user = $token->getUser(); |
|
98
|
2 |
|
$user->setPlainPassword($request->get('password')); |
|
99
|
|
|
|
|
100
|
2 |
|
$oldApiTokens = $apiTokenRepository->findAllByUser($user->getId()); |
|
101
|
|
|
|
|
102
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
|
103
|
2 |
|
$em->remove($token); |
|
104
|
|
|
|
|
105
|
2 |
|
foreach ($oldApiTokens as $oldApiToken) { |
|
106
|
2 |
|
$em->remove($oldApiToken); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
$em->flush(); |
|
110
|
|
|
|
|
111
|
2 |
|
return new JsonResponse(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|