1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Users\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
use App\Users\Entity\User; |
7
|
|
|
use App\Users\Event\UserRegisteredEvent; |
8
|
|
|
use App\Users\Repository\ConfirmationTokenRepository; |
9
|
|
|
use App\Users\Repository\UserRepository; |
10
|
|
|
use App\Users\Request\ConfirmEmailRequest; |
11
|
|
|
use App\Users\Request\RegisterUserRequest; |
12
|
|
|
use App\Users\Service\RegisterService; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
17
|
|
|
use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
18
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
19
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
20
|
|
|
|
21
|
|
|
class UserController extends BaseController |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Registration |
25
|
|
|
* |
26
|
|
|
* @Route("/api/users", methods={"POST"}) |
27
|
|
|
* @param RegisterUserRequest $request |
28
|
|
|
* @param RegisterService $registerService |
29
|
|
|
* @param EventDispatcherInterface $dispatcher |
30
|
|
|
* @param ValidatorInterface $validator |
31
|
|
|
* @return JsonResponse|\Symfony\Component\HttpFoundation\Response |
32
|
|
|
*/ |
33
|
2 |
|
public function postUsers(RegisterUserRequest $request, RegisterService $registerService, EventDispatcherInterface $dispatcher, ValidatorInterface $validator) |
34
|
|
|
{ |
35
|
2 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_ANONYMOUSLY'); // todo (not working) |
36
|
|
|
|
37
|
2 |
|
$registeredUser = $registerService->registerByRequest($request); |
38
|
2 |
|
$errors = $validator->validate($registeredUser); |
39
|
|
|
|
40
|
2 |
|
if ($errors && 0 !== $errors->count()) { |
41
|
|
|
return $request->getErrorResponse($errors); |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
$this->getDoctrine()->getManager()->flush(); |
45
|
|
|
|
46
|
2 |
|
$userRegisteredEvent = new UserRegisteredEvent($registeredUser); |
47
|
2 |
|
$dispatcher->dispatch(UserRegisteredEvent::NAME, $userRegisteredEvent); |
48
|
|
|
|
49
|
2 |
|
return $this->response($registeredUser, 200, [], [ |
50
|
2 |
|
'groups' => ['view'], |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Confirm email |
56
|
|
|
* |
57
|
|
|
* @Route("/api/confirmEmail", methods={"POST"}) |
58
|
|
|
* @param ConfirmEmailRequest $request |
59
|
|
|
* @param ConfirmationTokenRepository $confirmationTokenRepository |
60
|
|
|
* @param TranslatorInterface $translator |
61
|
|
|
* @return JsonResponse |
62
|
|
|
*/ |
63
|
2 |
|
public function postConfirmEmail(ConfirmEmailRequest $request, ConfirmationTokenRepository $confirmationTokenRepository, TranslatorInterface $translator) |
64
|
|
|
{ |
65
|
2 |
|
$token = $request->get('token'); |
66
|
|
|
|
67
|
2 |
|
if (null === $confirmationToken = $confirmationTokenRepository->findByToken($token)) { |
68
|
1 |
|
throw new BadCredentialsException($translator->trans('bad_email_confirmation_token', [ |
69
|
1 |
|
'token' => $token, |
70
|
1 |
|
], 'users')); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$user = $confirmationToken->getUser(); |
74
|
1 |
|
$user->confirmEmail(); |
75
|
|
|
|
76
|
1 |
|
$entityManager = $this->getDoctrine()->getManager(); |
77
|
1 |
|
$entityManager->persist($user); |
78
|
1 |
|
$entityManager->remove($confirmationToken); |
79
|
1 |
|
$entityManager->flush(); |
80
|
|
|
|
81
|
1 |
|
return new JsonResponse(null, 202); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get single user |
86
|
|
|
* |
87
|
|
|
* @Route("/api/users/{id}", methods={"GET"}) |
88
|
|
|
* @param $id |
89
|
|
|
* @param TranslatorInterface $translator |
90
|
|
|
* @return JsonResponse |
91
|
|
|
*/ |
92
|
3 |
|
public function getUsers($id, TranslatorInterface $translator) |
93
|
|
|
{ |
94
|
3 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var $userRepository \App\Users\Repository\UserRepository |
98
|
|
|
*/ |
99
|
2 |
|
$userRepository = $this->getDoctrine()->getRepository(User::class); |
100
|
2 |
|
$user = $userRepository->find($id); |
101
|
|
|
|
102
|
2 |
|
if ($user === null) { |
103
|
1 |
|
throw new NotFoundHttpException($translator->trans('not_found_by_id', [ |
104
|
1 |
|
'user_id' => $id, |
105
|
1 |
|
], 'users')); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return $this->response($user, 200, [], [ |
109
|
1 |
|
'groups' => ['view'], |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get all users |
115
|
|
|
* |
116
|
|
|
* @Route("/api/users", methods={"GET"}) |
117
|
|
|
* |
118
|
|
|
* @return User[] |
119
|
|
|
*/ |
120
|
3 |
|
public function getAll() |
121
|
|
|
{ |
122
|
3 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @var $userRepository UserRepository |
126
|
|
|
*/ |
127
|
2 |
|
$userRepository = $this->getDoctrine()->getRepository(User::class); |
128
|
2 |
|
$users = $userRepository->findAll(); |
129
|
|
|
|
130
|
2 |
|
return $this->response($users, 200, [], [ |
131
|
2 |
|
'groups' => ['list'], |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
} |