|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\User; |
|
6
|
|
|
use App\Repository\ConfirmationTokenRepository; |
|
7
|
|
|
use App\Repository\UserRepository; |
|
8
|
|
|
use App\Request\User\ConfirmEmailRequest; |
|
9
|
|
|
use App\Request\User\RegisterUserRequest; |
|
10
|
|
|
use App\Service\User\RegisterService; |
|
11
|
|
|
use FOS\RestBundle\Controller\FOSRestController; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
13
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
14
|
|
|
use Swagger\Annotations as SWG; |
|
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
16
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
|
17
|
|
|
use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
|
18
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
19
|
|
|
|
|
20
|
|
|
class UserController extends FOSRestController |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var RegisterService |
|
24
|
|
|
*/ |
|
25
|
|
|
private $registerService; |
|
26
|
|
|
|
|
27
|
|
|
private $translator; |
|
28
|
|
|
|
|
29
|
8 |
|
public function __construct(RegisterService $registerService, TranslatorInterface $translator) |
|
30
|
|
|
{ |
|
31
|
8 |
|
$this->registerService = $registerService; |
|
32
|
8 |
|
$this->translator = $translator; |
|
33
|
8 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Registration |
|
37
|
|
|
* |
|
38
|
|
|
* @Route("/api/users", methods={"POST"}) |
|
39
|
|
|
* @SWG\Parameter(name="registration.username", in="formData", type="string") |
|
40
|
|
|
* @SWG\Parameter(name="registration.password", in="formData", type="string") |
|
41
|
|
|
* @SWG\Parameter(name="registration.email", in="formData", type="string") |
|
42
|
|
|
* @SWG\Response( |
|
43
|
|
|
* description="Registration.", |
|
44
|
|
|
* response=202, |
|
45
|
|
|
* @Model(type=User::class) |
|
46
|
|
|
* ) |
|
47
|
|
|
* @param $request RegisterUserRequest |
|
48
|
|
|
* @return User |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function postUsers(RegisterUserRequest $request) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_ANONYMOUSLY'); |
|
53
|
|
|
|
|
54
|
2 |
|
return $this->registerService->registerByRequest($request); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Confirm email |
|
59
|
|
|
* |
|
60
|
|
|
* @Route("/api/confirmEmail", methods={"POST"}) |
|
61
|
|
|
* @SWG\Parameter(name="token", in="formData", type="string") |
|
62
|
|
|
* @SWG\Response( |
|
63
|
|
|
* description="Email confirmed.", |
|
64
|
|
|
* response=202 |
|
65
|
|
|
* ) |
|
66
|
|
|
* @param $request ConfirmEmailRequest |
|
67
|
|
|
* @param $confirmationTokenRepository ConfirmationTokenRepository |
|
68
|
|
|
* @return JsonResponse |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function postConfirmEmail(ConfirmEmailRequest $request, ConfirmationTokenRepository $confirmationTokenRepository) |
|
71
|
|
|
{ |
|
72
|
2 |
|
$token = $request->get('token'); |
|
73
|
|
|
|
|
74
|
2 |
|
if (null === $confirmationToken = $confirmationTokenRepository->findOneOrNullByToken($token)) { |
|
75
|
1 |
|
throw new BadCredentialsException($this->translator->trans('bad_email_confirmation_token', [ |
|
76
|
1 |
|
'token' => $token, |
|
77
|
1 |
|
], 'users')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
$user = $confirmationToken->getUser(); |
|
81
|
1 |
|
$user->confirmEmail(); |
|
82
|
|
|
|
|
83
|
1 |
|
$entityManager = $this->getDoctrine()->getManager(); |
|
84
|
1 |
|
$entityManager->persist($user); |
|
85
|
1 |
|
$entityManager->remove($confirmationToken); |
|
86
|
1 |
|
$entityManager->flush(); |
|
87
|
|
|
|
|
88
|
1 |
|
return new JsonResponse(null, 202); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get single user |
|
93
|
|
|
* |
|
94
|
|
|
* @Route("/api/users/{id}", methods={"GET"}) |
|
95
|
|
|
* @SWG\Response( |
|
96
|
|
|
* description="REST action which returns user by id.", |
|
97
|
|
|
* response=200, |
|
98
|
|
|
* @Model(type=User::class) |
|
99
|
|
|
* ) |
|
100
|
|
|
* |
|
101
|
|
|
* @param int $id |
|
102
|
|
|
* @return User |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getUsers($id) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @var $userRepository UserRepository |
|
110
|
|
|
*/ |
|
111
|
|
|
$userRepository = $this->getDoctrine()->getRepository(User::class); |
|
112
|
|
|
$user = $userRepository->find($id); |
|
113
|
|
|
|
|
114
|
|
|
if ($user === null) { |
|
115
|
|
|
throw new NotFoundHttpException($this->translator->trans('not_found_by_id', [ |
|
116
|
|
|
'user_id' => $id, |
|
117
|
|
|
], 'users')); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $user; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get all users |
|
125
|
|
|
* |
|
126
|
|
|
* @Route("/api/users", methods={"GET"}) |
|
127
|
|
|
* @SWG\Response( |
|
128
|
|
|
* description="REST action which returns user by id.", |
|
129
|
|
|
* response=201, |
|
130
|
|
|
* @SWG\Schema( |
|
131
|
|
|
* type="array", |
|
132
|
|
|
* @SWG\Items(ref=@Model(type=User::class, groups={"full"})) |
|
133
|
|
|
* ) |
|
134
|
|
|
* ) |
|
135
|
|
|
* |
|
136
|
|
|
* @return User[] |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function getAll() |
|
139
|
|
|
{ |
|
140
|
2 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @var $userRepository UserRepository |
|
144
|
|
|
*/ |
|
145
|
1 |
|
$userRepository = $this->getDoctrine()->getRepository(User::class); |
|
146
|
1 |
|
$users = $userRepository->findAll(); |
|
147
|
|
|
|
|
148
|
1 |
|
return $users; |
|
149
|
|
|
} |
|
150
|
|
|
} |