2020use Shopsys \FrameworkBundle \Model \Administrator \Exception \DeletingSelfException ;
2121use Shopsys \FrameworkBundle \Model \Administrator \Security \AdministratorRolesChangedFacade ;
2222use Shopsys \FrameworkBundle \Model \AdminNavigation \BreadcrumbOverrider ;
23- use Shopsys \FrameworkBundle \Model \Security \Authenticator ;
2423use Shopsys \FrameworkBundle \Model \Security \Roles ;
24+ use Symfony \Bundle \SecurityBundle \Security ;
2525use Symfony \Component \Form \Extension \Core \Type \FormType ;
2626use Symfony \Component \Form \Extension \Core \Type \SubmitType ;
2727use Symfony \Component \Form \Extension \Core \Type \TextType ;
3737
3838class AdministratorController extends AdminBaseController
3939{
40- protected const MAX_ADMINISTRATOR_ACTIVITIES_COUNT = 10 ;
40+ protected const int MAX_ADMINISTRATOR_ACTIVITIES_COUNT = 10 ;
4141
4242 /**
4343 * @param \Shopsys\FrameworkBundle\Model\Administrator\AdministratorFacade $administratorFacade
@@ -48,7 +48,7 @@ class AdministratorController extends AdminBaseController
4848 * @param \Shopsys\FrameworkBundle\Model\Administrator\Security\AdministratorRolesChangedFacade $administratorRolesChangedFacade
4949 * @param \Shopsys\FrameworkBundle\Model\Administrator\AdministratorTwoFactorAuthenticationFacade $administratorTwoFactorAuthenticationFacade
5050 * @param \Shopsys\FrameworkBundle\Model\Administrator\AdministratorPasswordFacade $administratorPasswordFacade
51- * @param \Shopsys\FrameworkBundle\Model \Security\Authenticator $authenticator
51+ * @param \Symfony\Bundle\SecurityBundle \Security $security
5252 */
5353 public function __construct (
5454 protected readonly AdministratorFacade $ administratorFacade ,
@@ -59,12 +59,15 @@ public function __construct(
5959 protected readonly AdministratorRolesChangedFacade $ administratorRolesChangedFacade ,
6060 protected readonly AdministratorTwoFactorAuthenticationFacade $ administratorTwoFactorAuthenticationFacade ,
6161 protected readonly AdministratorPasswordFacade $ administratorPasswordFacade ,
62- protected readonly Authenticator $ authenticator ,
62+ protected readonly Security $ security ,
6363 ) {
6464 }
6565
66+ /**
67+ * @return \Symfony\Component\HttpFoundation\Response
68+ */
6669 #[Route(path: '/administrator/list/ ' )]
67- public function listAction ()
70+ public function listAction (): Response
6871 {
6972 $ queryBuilder = $ this ->administratorFacade ->getAllListableQueryBuilder ();
7073 $ dataSource = new QueryBuilderDataSource ($ queryBuilder , 'a.id ' );
@@ -90,10 +93,13 @@ public function listAction()
9093 /**
9194 * @param \Symfony\Component\HttpFoundation\Request $request
9295 * @param int $id
96+ * @return \Symfony\Component\HttpFoundation\Response
9397 */
9498 #[Route(path: '/administrator/edit/{id} ' , requirements: ['id ' => '\d+ ' ])]
95- public function editAction (Request $ request , int $ id )
99+ public function editAction (Request $ request , int $ id ): Response
96100 {
101+ $ this ->denyAccessUnlessHimselfOrGranted ($ request , $ id );
102+
97103 $ administrator = $ this ->administratorFacade ->getById ($ id );
98104
99105 $ loggedUser = $ this ->getUser ();
@@ -159,8 +165,31 @@ public function editAction(Request $request, int $id)
159165 ]);
160166 }
161167
168+ /**
169+ * @param \Symfony\Component\HttpFoundation\Request $request
170+ * @param int $administratorId
171+ */
172+ protected function denyAccessUnlessHimselfOrGranted (Request $ request , int $ administratorId ): void
173+ {
174+ $ currentAdministrator = $ this ->getCurrentAdministrator ();
175+
176+ // always allow admin to edit himself
177+ if ($ currentAdministrator ->getId () === $ administratorId ) {
178+ return ;
179+ }
180+
181+ if ($ request ->getMethod () === Request::METHOD_GET ) {
182+ $ this ->denyAccessUnlessGranted (Roles::ROLE_ADMINISTRATOR_VIEW );
183+ } else {
184+ $ this ->denyAccessUnlessGranted (Roles::ROLE_ADMINISTRATOR_FULL );
185+ }
186+ }
187+
188+ /**
189+ * @return \Symfony\Component\HttpFoundation\Response
190+ */
162191 #[Route(path: '/administrator/my-account/ ' )]
163- public function myAccountAction ()
192+ public function myAccountAction (): Response
164193 {
165194 /** @var \Shopsys\FrameworkBundle\Model\Administrator\Administrator $loggedUser */
166195 $ loggedUser = $ this ->getUser ();
@@ -172,9 +201,10 @@ public function myAccountAction()
172201
173202 /**
174203 * @param \Symfony\Component\HttpFoundation\Request $request
204+ * @return \Symfony\Component\HttpFoundation\Response
175205 */
176206 #[Route(path: '/administrator/new/ ' )]
177- public function newAction (Request $ request )
207+ public function newAction (Request $ request ): Response
178208 {
179209 $ form = $ this ->createForm (AdministratorFormType::class, $ this ->administratorDataFactory ->create (), [
180210 'scenario ' => AdministratorFormType::SCENARIO_CREATE ,
@@ -211,9 +241,10 @@ public function newAction(Request $request)
211241 /**
212242 * @CsrfProtection
213243 * @param int $id
244+ * @return \Symfony\Component\HttpFoundation\Response
214245 */
215246 #[Route(path: '/administrator/delete/{id} ' , requirements: ['id ' => '\d+ ' ])]
216- public function deleteAction (int $ id )
247+ public function deleteAction (int $ id ): Response
217248 {
218249 try {
219250 $ realName = $ this ->administratorFacade ->getById ($ id )->getRealName ();
@@ -225,16 +256,16 @@ public function deleteAction(int $id)
225256 'name ' => $ realName ,
226257 ],
227258 );
228- } catch (DeletingSelfException $ ex ) {
259+ } catch (DeletingSelfException ) {
229260 $ this ->addErrorFlash (t ('You can \'t delete yourself. ' ));
230- } catch (DeletingLastAdministratorException $ ex ) {
261+ } catch (DeletingLastAdministratorException ) {
231262 $ this ->addErrorFlashTwig (
232263 t ('Administrator <strong>{{ name }}</strong> is the only one and can \'t be deleted. ' ),
233264 [
234265 'name ' => $ this ->administratorFacade ->getById ($ id )->getRealName (),
235266 ],
236267 );
237- } catch (AdministratorNotFoundException $ ex ) {
268+ } catch (AdministratorNotFoundException ) {
238269 $ this ->addErrorFlash (t ('Selected administrated doesn \'t exist. ' ));
239270 }
240271
@@ -269,7 +300,7 @@ public function enableTwoFactorAuthenticationAction(
269300 $ loggedUser = $ this ->getUser ();
270301 $ this ->securitySafeCheck ($ loggedUser );
271302
272- if ($ administrator ->getUsername () !== $ loggedUser ->getUserIdentifier ()) {
303+ if ($ administrator ->getUsername () !== $ loggedUser? ->getUserIdentifier()) {
273304 $ this ->addErrorFlash (t ('You are allowed to set up two factor authentication only to yourself. ' ));
274305
275306 return $ this ->redirectToRoute ('admin_administrator_edit ' , ['id ' => $ id ]);
@@ -386,7 +417,7 @@ public function disableTwoFactorAuthenticationAction(Request $request, int $id):
386417 $ loggedUser = $ this ->getUser ();
387418 $ this ->securitySafeCheck ($ loggedUser );
388419
389- if ($ administrator ->getUsername () !== $ loggedUser ->getUserIdentifier ()) {
420+ if ($ administrator ->getUsername () !== $ loggedUser? ->getUserIdentifier()) {
390421 $ this ->addErrorFlash (t ('You are allowed to disable two factor authentication only to yourself. ' ));
391422
392423 return $ this ->redirectToRoute ('admin_administrator_edit ' , ['id ' => $ id ]);
@@ -531,7 +562,8 @@ public function setNewPasswordAction(Request $request): Response
531562 );
532563
533564 if (!$ this ->isGranted (Roles::ROLE_ADMIN )) {
534- $ this ->authenticator ->loginAdministrator ($ administrator );
565+ $ this ->security ->login ($ administrator , 'security.authenticator.form_login.administration ' );
566+ $ request ->getSession ()->migrate ();
535567 }
536568
537569 $ this ->addSuccessFlash (t ('Password has been successfully set. ' ));
0 commit comments