1010use PhpList \PhpList4 \Domain \Model \Identity \AdministratorToken ;
1111use PhpList \PhpList4 \Domain \Repository \Identity \AdministratorRepository ;
1212use PhpList \PhpList4 \Domain \Repository \Identity \AdministratorTokenRepository ;
13+ use PhpList \PhpList4 \Security \Authentication ;
14+ use PhpList \RestBundle \Controller \Traits \AuthenticationTrait ;
1315use Symfony \Component \HttpFoundation \Request ;
1416use Symfony \Component \HttpFoundation \Response ;
17+ use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
1518use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
1619use Symfony \Component \HttpKernel \Exception \UnauthorizedHttpException ;
1720
2225 */
2326class SessionController extends FOSRestController implements ClassResourceInterface
2427{
28+ use AuthenticationTrait;
29+
2530 /**
2631 * @var AdministratorRepository
2732 */
@@ -33,13 +38,16 @@ class SessionController extends FOSRestController implements ClassResourceInterf
3338 private $ administratorTokenRepository = null ;
3439
3540 /**
41+ * @param Authentication $authentication
3642 * @param AdministratorRepository $administratorRepository
3743 * @param AdministratorTokenRepository $tokenRepository
3844 */
3945 public function __construct (
46+ Authentication $ authentication ,
4047 AdministratorRepository $ administratorRepository ,
4148 AdministratorTokenRepository $ tokenRepository
4249 ) {
50+ $ this ->authentication = $ authentication ;
4351 $ this ->administratorRepository = $ administratorRepository ;
4452 $ this ->administratorTokenRepository = $ tokenRepository ;
4553 }
@@ -69,6 +77,30 @@ public function postAction(Request $request): View
6977 return View::create ()->setStatusCode (Response::HTTP_CREATED )->setData ($ token );
7078 }
7179
80+ /**
81+ * Deletes a session.
82+ *
83+ * This action may only be called for sessions that are owned by the authenticated administrator.
84+ *
85+ * @param Request $request
86+ * @param AdministratorToken $token
87+ *
88+ * @return View
89+ *
90+ * @throws AccessDeniedHttpException
91+ */
92+ public function deleteAction (Request $ request , AdministratorToken $ token ): View
93+ {
94+ $ administrator = $ this ->requireAuthentication ($ request );
95+ if ($ token ->getAdministrator () !== $ administrator ) {
96+ throw new AccessDeniedHttpException ('You do not have access to this session. ' , null , 1519831644 );
97+ }
98+
99+ $ this ->administratorTokenRepository ->remove ($ token );
100+
101+ return View::create ();
102+ }
103+
72104 /**
73105 * Validates the request. If is it not valid, throws an exception.
74106 *
0 commit comments