-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2UserRegistrationRequestApiController.php
More file actions
329 lines (308 loc) · 11.2 KB
/
OAuth2UserRegistrationRequestApiController.php
File metadata and controls
329 lines (308 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php namespace App\Http\Controllers\Api\OAuth2;
/**
* Copyright 2019 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Http\Controllers\GetAllTrait;
use App\libs\Auth\Repositories\IUserRegistrationRequestRepository;
use App\libs\OAuth2\IUserScopes;
use App\ModelSerializers\SerializerRegistry;
use App\Services\Auth\IUserService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use OAuth2\IResourceServerContext;
use OpenApi\Attributes as OA;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
use Utils\Services\ILogService;
/**
* Class OAuth2UserRegistrationRequestApiController
* @package App\Http\Controllers\Api\OAuth2
*/
#[OA\Get(
path: '/api/v1/user-registration-requests',
operationId: 'getUserRegistrationRequests',
summary: 'Get all user registration requests',
security: [['OAuth2UserRegistrationRequestApi' => [IUserScopes::Registration]]],
tags: ['User Registration Requests'],
parameters: [
new OA\Parameter(
name: 'page',
description: 'Page number',
in: 'query',
required: false,
schema: new OA\Schema(type: 'integer')
),
new OA\Parameter(
name: 'per_page',
description: 'Items per page',
in: 'query',
required: false,
schema: new OA\Schema(type: 'integer')
),
new OA\Parameter(
name: 'filter',
description: 'Filter criteria (first_name, last_name, email, is_redeemed) ("=@" starts with, "==" exact match); is_redeemed supports "==" only.',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'order',
description: 'Order criteria. Accepted fields: id. Use +id for ascending, -id for descending.',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string')
),
],
responses: [
new OA\Response(
response: HttpResponse::HTTP_OK,
description: 'OK',
content: new OA\JsonContent(ref: '#/components/schemas/PaginatedUserRegistrationRequestResponse')
),
new OA\Response(
response: HttpResponse::HTTP_PRECONDITION_FAILED,
description: 'Precondition Failed'
),
new OA\Response(
response: HttpResponse::HTTP_INTERNAL_SERVER_ERROR,
description: 'Server Error'
),
]
)]
final class OAuth2UserRegistrationRequestApiController extends OAuth2ProtectedController
{
use GetAllTrait;
/**
* @var IUserService
*/
private $user_service;
/**
* @param IUserRegistrationRequestRepository $repository
* @param IUserService $user_service
* @param IResourceServerContext $resource_server_context
* @param ILogService $log_service
*/
public function __construct
(
IUserRegistrationRequestRepository $repository,
IUserService $user_service,
IResourceServerContext $resource_server_context,
ILogService $log_service
)
{
parent::__construct($resource_server_context, $log_service);
$this->repository = $repository;
$this->user_service = $user_service;
}
protected function getAllSerializerType(): string
{
return SerializerRegistry::SerializerType_Public;
}
/**
* @return array
*/
protected function getFilterRules(): array
{
return [
'first_name' => ['=@', '=='],
'last_name' => ['=@', '=='],
'email' => ['=@', '=='],
'is_redeemed' => ['==']
];
}
public function getOrderRules(): array
{
return [
'id',
];
}
/**
* @return array
*/
protected function getFilterValidatorRules(): array
{
return [
'first_name' => 'sometimes|required|string',
'last_name' => 'sometimes|required|string',
'email' => 'sometimes|required|email',
'is_redeemed' => 'sometimes|required|boolean'
];
}
/**
* @return \Illuminate\Http\JsonResponse|mixed
*/
#[OA\Post(
path: '/api/v1/user-registration-requests',
operationId: 'createUserRegistrationRequest',
summary: 'Create a user registration request',
security: [['OAuth2UserRegistrationRequestApi' => [IUserScopes::Registration]]],
tags: ['User Registration Requests'],
requestBody: new OA\RequestBody(
description: 'User registration request data',
required: true,
content: new OA\JsonContent(ref: '#/components/schemas/CreateUserRegistrationRequestRequest')
),
responses: [
new OA\Response(
response: HttpResponse::HTTP_CREATED,
description: 'Created',
content: new OA\JsonContent(ref: '#/components/schemas/UserRegistrationRequest')
),
new OA\Response(
response: HttpResponse::HTTP_BAD_REQUEST,
description: 'Bad Request'
),
new OA\Response(
response: HttpResponse::HTTP_PRECONDITION_FAILED,
description: 'Precondition Failed'
),
new OA\Response(
response: HttpResponse::HTTP_NOT_FOUND,
description: 'Not Found'
),
new OA\Response(
response: HttpResponse::HTTP_INTERNAL_SERVER_ERROR,
description: 'Server Error'
),
]
)]
public function register(){
try {
if(!Request::isJson()) return $this->error400();
$payload = Request::json()->all();
// Creates a Validator instance and validates the data.
$validation = Validator::make($payload, [
'first_name' => 'nullable|sometimes|string|max:100',
'last_name' => 'nullable|sometimes|string|max:100',
'company' => 'nullable|sometimes|string|max:100',
'email' => 'required|string|email|max:255',
'country' => 'sometimes|required|string|country_iso_alpha2_code',
]);
if ($validation->fails()) {
$messages = $validation->messages()->toArray();
return $this->error412
(
$messages
);
}
$registration_request = $this->user_service->createRegistrationRequest
(
$this->resource_server_context->getCurrentClientId(),
$payload
);
return $this->created(SerializerRegistry::getInstance()->getSerializer($registration_request)->serialize());
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412([$ex1->getMessage()]);
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(['message'=> $ex2->getMessage()]);
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $id
* @return \Illuminate\Http\JsonResponse|mixed
*/
#[OA\Put(
path: '/api/v1/user-registration-requests/{id}',
operationId: 'updateUserRegistrationRequest',
summary: 'Update a user registration request',
security: [['OAuth2UserRegistrationRequestApi' => [IUserScopes::Registration]]],
tags: ['User Registration Requests'],
parameters: [
new OA\Parameter(
name: 'id',
description: 'Registration request ID',
in: 'path',
required: true,
schema: new OA\Schema(type: 'integer')
),
],
requestBody: new OA\RequestBody(
description: 'User registration request data to update',
required: true,
content: new OA\JsonContent(ref: '#/components/schemas/UpdateUserRegistrationRequestRequest')
),
responses: [
new OA\Response(
response: HttpResponse::HTTP_CREATED,
description: 'OK',
content: new OA\JsonContent(ref: '#/components/schemas/UserRegistrationRequest')
),
new OA\Response(
response: HttpResponse::HTTP_BAD_REQUEST,
description: 'Bad Request'
),
new OA\Response(
response: HttpResponse::HTTP_PRECONDITION_FAILED,
description: 'Precondition Failed'
),
new OA\Response(
response: HttpResponse::HTTP_NOT_FOUND,
description: 'Not Found'
),
new OA\Response(
response: HttpResponse::HTTP_INTERNAL_SERVER_ERROR,
description: 'Server Error'
),
]
)]
public function update($id){
try {
if(!Request::isJson()) return $this->error400();
$payload = Request::json()->all();
// Creates a Validator instance and validates the data.
$validation = Validator::make($payload, [
'first_name' => 'nullable|sometimes|string|max:100',
'last_name' => 'nullable|sometimes|string|max:100',
'company' => 'nullable|sometimes|string|max:100',
'country' => 'nullable|sometimes|required|string|country_iso_alpha2_code',
]);
if ($validation->fails()) {
$messages = $validation->messages()->toArray();
return $this->error412
(
$messages
);
}
$registration_request = $this->user_service->updateRegistrationRequest
(
intval($id),
$payload
);
return $this->updated(SerializerRegistry::getInstance()->getSerializer($registration_request)->serialize());
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412([$ex1->getMessage()]);
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(['message'=> $ex2->getMessage()]);
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}