44
55namespace PhpList \RestBundle \Controller ;
66
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 ;
711use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
812use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
9- use PhpList \Core \Domain \Model \Identity \Administrator ;
1013use PhpList \Core \Domain \Model \Identity \AdministratorToken ;
1114use PhpList \Core \Domain \Repository \Identity \AdministratorRepository ;
12- use PhpList \Core \Domain \Repository \Identity \AdministratorTokenRepository ;
1315use PhpList \Core \Security \Authentication ;
1416use PhpList \RestBundle \Controller \Traits \AuthenticationTrait ;
1517use Symfony \Component \HttpFoundation \JsonResponse ;
1618use Symfony \Component \HttpFoundation \Request ;
1719use Symfony \Component \HttpFoundation \Response ;
1820use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
19- use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
20- use Symfony \Component \HttpKernel \Exception \UnauthorizedHttpException ;
2121use Symfony \Component \Routing \Attribute \Route ;
2222use Symfony \Component \Serializer \SerializerInterface ;
2323use OpenApi \Attributes as OA ;
2626 * This controller provides methods to create and destroy REST API sessions.
2727 *
2828 * @author Oliver Klee <[email protected] > 29+ * @author Tatevik Grigoryan <[email protected] > 2930 */
31+ #[Route('/sessions ' )]
3032class SessionController extends AbstractController
3133{
3234 use AuthenticationTrait;
3335
3436 private AdministratorRepository $ administratorRepository ;
35- private AdministratorTokenRepository $ tokenRepository ;
3637 private SerializerInterface $ serializer ;
38+ private SessionManager $ sessionManager ;
3739
3840 public function __construct (
3941 Authentication $ authentication ,
4042 AdministratorRepository $ administratorRepository ,
41- AdministratorTokenRepository $ tokenRepository ,
42- SerializerInterface $ serializer
43+ SerializerInterface $ serializer ,
44+ SessionManager $ sessionManager ,
4345 ) {
4446 $ this ->authentication = $ authentication ;
4547 $ this ->administratorRepository = $ administratorRepository ;
46- $ this ->tokenRepository = $ tokenRepository ;
4748 $ this ->serializer = $ serializer ;
49+ $ this ->sessionManager = $ sessionManager ;
4850 }
4951
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 ' ])]
5653 #[OA \Post(
5754 path: '/sessions ' ,
5855 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(
105102 )
106103 ]
107104 )]
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 );
118113
119- $ token = $ this ->createAndPersistToken ($ administrator );
120- $ json = $ this ->serializer ->serialize ($ token , 'json ' );
114+ $ json = $ normalizer ->normalize ($ token , 'json ' );
121115
122- return new JsonResponse ($ json , Response::HTTP_CREATED , [], true );
116+ return new JsonResponse ($ json , Response::HTTP_CREATED , [], false );
123117 }
124118
125119 /**
@@ -129,7 +123,7 @@ public function createSession(Request $request): JsonResponse
129123 *
130124 * @throws AccessDeniedHttpException
131125 */
132- #[Route('/sessions/ {sessionId} ' , name: 'delete_session ' , methods: ['DELETE ' ])]
126+ #[Route('/{sessionId} ' , name: 'delete_session ' , methods: ['DELETE ' ])]
133127 #[OA \Delete(
134128 path: '/sessions/{sessionId} ' ,
135129 description: 'Delete the session passed as a parameter. ' ,
@@ -177,7 +171,7 @@ public function createSession(Request $request): JsonResponse
177171 )
178172 ]
179173 )]
180- public function deleteAction (
174+ public function deleteSession (
181175 Request $ request ,
182176 #[MapEntity(mapping: ['sessionId ' => 'id ' ])] AdministratorToken $ token
183177 ): JsonResponse {
@@ -186,43 +180,8 @@ public function deleteAction(
186180 throw new AccessDeniedHttpException ('You do not have access to this session. ' , null , 1519831644 );
187181 }
188182
189- $ this ->tokenRepository -> remove ($ token );
183+ $ this ->sessionManager -> deleteSession ($ token );
190184
191185 return new JsonResponse (null , Response::HTTP_NO_CONTENT , [], false );
192186 }
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- }
228187}
0 commit comments