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