4
4
5
5
namespace PhpList \RestBundle \Controller ;
6
6
7
+ use PhpList \RestBundle \Entity \CreateSessionRequest ;
8
+ use PhpList \RestBundle \Serializer \AdministratorTokenNormalizer ;
9
+ use PhpList \RestBundle \Service \Manager \SessionManager ;
10
+ use PhpList \RestBundle \Validator \RequestValidator ;
7
11
use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
8
12
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
9
- use PhpList \Core \Domain \Model \Identity \Administrator ;
10
13
use PhpList \Core \Domain \Model \Identity \AdministratorToken ;
11
14
use PhpList \Core \Domain \Repository \Identity \AdministratorRepository ;
12
- use PhpList \Core \Domain \Repository \Identity \AdministratorTokenRepository ;
13
15
use PhpList \Core \Security \Authentication ;
14
16
use PhpList \RestBundle \Controller \Traits \AuthenticationTrait ;
15
17
use Symfony \Component \HttpFoundation \JsonResponse ;
16
18
use Symfony \Component \HttpFoundation \Request ;
17
19
use Symfony \Component \HttpFoundation \Response ;
18
20
use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
19
- use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
20
- use Symfony \Component \HttpKernel \Exception \UnauthorizedHttpException ;
21
21
use Symfony \Component \Routing \Attribute \Route ;
22
22
use Symfony \Component \Serializer \SerializerInterface ;
23
23
use OpenApi \Attributes as OA ;
26
26
* This controller provides methods to create and destroy REST API sessions.
27
27
*
28
28
* @author Oliver Klee <[email protected] >
29
+ * @author Tatevik Grigoryan <[email protected] >
29
30
*/
31
+ #[Route('/sessions ' )]
30
32
class SessionController extends AbstractController
31
33
{
32
34
use AuthenticationTrait;
33
35
34
36
private AdministratorRepository $ administratorRepository ;
35
- private AdministratorTokenRepository $ tokenRepository ;
36
37
private SerializerInterface $ serializer ;
38
+ private SessionManager $ sessionManager ;
37
39
38
40
public function __construct (
39
41
Authentication $ authentication ,
40
42
AdministratorRepository $ administratorRepository ,
41
- AdministratorTokenRepository $ tokenRepository ,
42
- SerializerInterface $ serializer
43
+ SerializerInterface $ serializer ,
44
+ SessionManager $ sessionManager ,
43
45
) {
44
46
$ this ->authentication = $ authentication ;
45
47
$ this ->administratorRepository = $ administratorRepository ;
46
- $ this ->tokenRepository = $ tokenRepository ;
47
48
$ this ->serializer = $ serializer ;
49
+ $ this ->sessionManager = $ sessionManager ;
48
50
}
49
51
50
- /**
51
- * Creates a new session (if the provided credentials are valid).
52
- *
53
- * @throws UnauthorizedHttpException
54
- */
55
- #[Route('/sessions ' , name: 'create_session ' , methods: ['POST ' ])]
52
+ #[Route('' , name: 'create_session ' , methods: ['POST ' ])]
56
53
#[OA \Post(
57
54
path: '/sessions ' ,
58
55
description: 'Given valid login data, this will generate a login token that will be valid for 1 hour. ' ,
@@ -105,21 +102,18 @@ public function __construct(
105
102
)
106
103
]
107
104
)]
108
- public function createSession (Request $ request ): JsonResponse
109
- {
110
- $ this ->validateCreateRequest ($ request );
111
- $ administrator = $ this ->administratorRepository ->findOneByLoginCredentials (
112
- $ request ->getPayload ()->get ('login_name ' ),
113
- $ request ->getPayload ()->get ('password ' )
114
- );
115
- if ($ administrator === null ) {
116
- throw new UnauthorizedHttpException ('' , 'Not authorized ' , null , 1500567098 );
117
- }
105
+ public function createSession (
106
+ Request $ request ,
107
+ RequestValidator $ validator ,
108
+ AdministratorTokenNormalizer $ normalizer
109
+ ): JsonResponse {
110
+ /** @var CreateSessionRequest $createSessionRequest */
111
+ $ createSessionRequest = $ validator ->validate ($ request , CreateSessionRequest::class);
112
+ $ token = $ this ->sessionManager ->createSession ($ createSessionRequest );
118
113
119
- $ token = $ this ->createAndPersistToken ($ administrator );
120
- $ json = $ this ->serializer ->serialize ($ token , 'json ' );
114
+ $ json = $ normalizer ->normalize ($ token , 'json ' );
121
115
122
- return new JsonResponse ($ json , Response::HTTP_CREATED , [], true );
116
+ return new JsonResponse ($ json , Response::HTTP_CREATED , [], false );
123
117
}
124
118
125
119
/**
@@ -129,7 +123,7 @@ public function createSession(Request $request): JsonResponse
129
123
*
130
124
* @throws AccessDeniedHttpException
131
125
*/
132
- #[Route('/sessions/ {sessionId} ' , name: 'delete_session ' , methods: ['DELETE ' ])]
126
+ #[Route('/{sessionId} ' , name: 'delete_session ' , methods: ['DELETE ' ])]
133
127
#[OA \Delete(
134
128
path: '/sessions/{sessionId} ' ,
135
129
description: 'Delete the session passed as a parameter. ' ,
@@ -177,7 +171,7 @@ public function createSession(Request $request): JsonResponse
177
171
)
178
172
]
179
173
)]
180
- public function deleteAction (
174
+ public function deleteSession (
181
175
Request $ request ,
182
176
#[MapEntity(mapping: ['sessionId ' => 'id ' ])] AdministratorToken $ token
183
177
): JsonResponse {
@@ -186,43 +180,8 @@ public function deleteAction(
186
180
throw new AccessDeniedHttpException ('You do not have access to this session. ' , null , 1519831644 );
187
181
}
188
182
189
- $ this ->tokenRepository -> remove ($ token );
183
+ $ this ->sessionManager -> deleteSession ($ token );
190
184
191
185
return new JsonResponse (null , Response::HTTP_NO_CONTENT , [], false );
192
186
}
193
-
194
- /**
195
- * Validates the request. If is it not valid, throws an exception.
196
- *
197
- * @param Request $request
198
- *
199
- * @return void
200
- *
201
- * @throws BadRequestHttpException
202
- */
203
- private function validateCreateRequest (Request $ request ): void
204
- {
205
- if ($ request ->getContent () === '' ) {
206
- throw new BadRequestHttpException ('Empty JSON data ' , null , 1500559729 );
207
- }
208
- if (empty ($ request ->getPayload ()->get ('login_name ' )) || empty ($ request ->getPayload ()->get ('password ' ))) {
209
- throw new BadRequestHttpException ('Incomplete credentials ' , null , 1500562647 );
210
- }
211
- }
212
-
213
- /**
214
- * @param Administrator $administrator
215
- *
216
- * @return AdministratorToken
217
- */
218
- private function createAndPersistToken (Administrator $ administrator ): AdministratorToken
219
- {
220
- $ token = new AdministratorToken ();
221
- $ token ->setAdministrator ($ administrator );
222
- $ token ->generateExpiry ();
223
- $ token ->generateKey ();
224
- $ this ->tokenRepository ->save ($ token );
225
-
226
- return $ token ;
227
- }
228
187
}
0 commit comments