|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use App\Event\UserCreateEvent; |
|
8
|
|
|
use App\Event\UserPasswordChangeRequestEvent; |
|
9
|
|
|
use App\Form\ErrorHandler; |
|
10
|
|
|
use App\Form\RegisterUserType; |
|
11
|
|
|
use App\Form\UserPasswordResetRequestType; |
|
12
|
|
|
use App\Form\UserPasswordResetType; |
|
13
|
|
|
use App\Manager\UserManagerInterface; |
|
14
|
|
|
use App\Repository\UserRepositoryInterface; |
|
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
16
|
|
|
use SWP\Component\Common\Exception\NotFoundHttpException; |
|
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
19
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
23
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
24
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class ApiUsersController extends AbstractController |
|
27
|
|
|
{ |
|
28
|
|
|
protected $serializer; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(SerializerInterface $serializer) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->serializer = $serializer; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getCurrentUser(): Response |
|
36
|
|
|
{ |
|
37
|
|
|
return new Response($this->serializer->serialize($this->getUser(), 'json', [ |
|
38
|
|
|
'groups' => [ |
|
39
|
|
|
'user_details', |
|
40
|
|
|
'course_details', |
|
41
|
|
|
], |
|
42
|
|
|
])); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function registerOrUpdateUser( |
|
46
|
|
|
Request $request, |
|
47
|
|
|
FormFactoryInterface $formFactory, |
|
48
|
|
|
EntityManagerInterface $entityManager, |
|
49
|
|
|
UserManagerInterface $userManager, |
|
50
|
|
|
TokenStorageInterface $tokenStorage, |
|
51
|
|
|
EventDispatcherInterface $eventDispatcher |
|
52
|
|
|
): Response { |
|
53
|
|
|
if (!$request->request->has('email')) { |
|
54
|
|
|
return new JsonResponse(['message' => 'User email is not provided'], Response::HTTP_BAD_REQUEST); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$user = $userManager->getOrCreateUser($request->request->get('email')); |
|
58
|
|
|
$form = $formFactory->create(RegisterUserType::class, $user); |
|
59
|
|
|
$form->handleRequest($request); |
|
60
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
61
|
|
|
if (null !== $courseName = $form->get('course')->getData()) { |
|
62
|
|
|
$userManager->addCourseByTitle($user, $courseName); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$newUser = false; |
|
66
|
|
|
if (!$entityManager->contains($user)) { |
|
67
|
|
|
$entityManager->persist($user); |
|
68
|
|
|
$newUser = true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$entityManager->flush(); |
|
72
|
|
|
$tokenStorage->getToken()->setUser($user); |
|
73
|
|
|
|
|
74
|
|
|
if ($newUser) { |
|
75
|
|
|
$eventDispatcher->dispatch(new UserCreateEvent($user)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return new Response($this->serializer->serialize($user, 'json', ['groups' => ['user_details']]), Response::HTTP_CREATED); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return new Response($this->serializer->serialize(ErrorHandler::getErrorsFromForm($form), 'json'), Response::HTTP_BAD_REQUEST); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function requestPasswordReset( |
|
85
|
|
|
Request $request, |
|
86
|
|
|
FormFactoryInterface $formFactory, |
|
87
|
|
|
EntityManagerInterface $entityManager, |
|
88
|
|
|
UserManagerInterface $userManager, |
|
89
|
|
|
UserRepositoryInterface $userRepository, |
|
90
|
|
|
EventDispatcherInterface $eventDispatcher |
|
91
|
|
|
): Response { |
|
92
|
|
|
$form = $formFactory->create(UserPasswordResetRequestType::class, []); |
|
93
|
|
|
$form->handleRequest($request); |
|
94
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
95
|
|
|
$user = $userRepository->getOneByEmail($form->getData()['email']); |
|
96
|
|
|
if (null === $user) { |
|
97
|
|
|
throw new NotFoundHttpException('User was not found'); |
|
98
|
|
|
} |
|
99
|
|
|
$userManager->setGeneratedPasswordResetToken($user); |
|
100
|
|
|
$entityManager->flush(); |
|
101
|
|
|
|
|
102
|
|
|
$eventDispatcher->dispatch(new UserPasswordChangeRequestEvent($user)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return new JsonResponse(['success' => true]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function requestPassword( |
|
109
|
|
|
Request $request, |
|
110
|
|
|
FormFactoryInterface $formFactory, |
|
111
|
|
|
EntityManagerInterface $entityManager, |
|
112
|
|
|
UserManagerInterface $userManager, |
|
113
|
|
|
UserRepositoryInterface $userRepository |
|
114
|
|
|
): Response { |
|
115
|
|
|
$form = $formFactory->create(UserPasswordResetType::class, []); |
|
116
|
|
|
$form->handleRequest($request); |
|
117
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
118
|
|
|
$user = $userRepository->getOneByPasswordResetToken($request->query->get('token')); |
|
119
|
|
|
if (null === $user) { |
|
120
|
|
|
throw new NotFoundHttpException('User was not found'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$data = $form->getData(); |
|
124
|
|
|
if ($data['password'] !== $data['repeatedPassword']) { |
|
125
|
|
|
throw new \InvalidArgumentException('Passwords are not equal'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$userManager->resetPassword($user, $data['password']); |
|
129
|
|
|
$entityManager->flush(); |
|
130
|
|
|
|
|
131
|
|
|
return new Response($this->serializer->serialize($user, 'json', ['groups' => ['user_details']]), Response::HTTP_OK); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return new Response($this->serializer->serialize(ErrorHandler::getErrorsFromForm($form), 'json'), Response::HTTP_BAD_REQUEST); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|