44
55namespace Frontend \Admin \Controller ;
66
7- use Doctrine \ORM \Exception \ORMException ;
87use Doctrine \ORM \NonUniqueResultException ;
98use Dot \Controller \AbstractActionController ;
109use Dot \DependencyInjection \Attribute \Inject ;
1110use Dot \FlashMessenger \FlashMessengerInterface ;
12- use Exception ;
11+ use Fig \ Http \ Message \ RequestMethodInterface ;
1312use Fig \Http \Message \StatusCodeInterface ;
1413use Frontend \Admin \Adapter \AuthenticationAdapter ;
1514use Frontend \Admin \Entity \Admin ;
1615use Frontend \Admin \Entity \AdminIdentity ;
1716use Frontend \Admin \Entity \AdminLogin ;
1817use Frontend \Admin \Form \AccountForm ;
18+ use Frontend \Admin \Form \AdminDeleteForm ;
1919use Frontend \Admin \Form \AdminForm ;
2020use Frontend \Admin \Form \ChangePasswordForm ;
2121use Frontend \Admin \Form \LoginForm ;
2222use Frontend \Admin \FormData \AdminFormData ;
2323use Frontend \Admin \InputFilter \EditAdminInputFilter ;
2424use Frontend \Admin \Service \AdminServiceInterface ;
2525use Frontend \App \Common \ServerRequestAwareTrait ;
26+ use Frontend \App \Exception \IdentityException ;
2627use Frontend \App \Message ;
2728use Frontend \App \Plugin \FormsPlugin ;
2829use Laminas \Authentication \AuthenticationServiceInterface ;
3738use Psr \Http \Message \ResponseInterface ;
3839use Throwable ;
3940
40- use function password_verify ;
41-
4241class AdminController extends AbstractActionController
4342{
4443 use ServerRequestAwareTrait;
@@ -75,11 +74,11 @@ public function addAction(): ResponseInterface
7574 try {
7675 $ this ->adminService ->createAdmin ($ result );
7776 return new JsonResponse (['message ' => Message::ADMIN_CREATED_SUCCESSFULLY ]);
78- } catch (ORMException $ e ) {
77+ } catch (IdentityException $ e ) {
7978 $ this ->logErrors ($ e , Message::CREATE_ADMIN );
8079 return new JsonResponse (
8180 ['message ' => $ e ->getMessage ()],
82- StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR
81+ StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY
8382 );
8483 } catch (Throwable $ e ) {
8584 $ this ->logErrors ($ e , Message::CREATE_ADMIN );
@@ -102,14 +101,12 @@ public function addAction(): ResponseInterface
102101 [
103102 'form ' => $ this ->adminForm ,
104103 'formAction ' => '/admin/add ' ,
104+ 'method ' => RequestMethodInterface::METHOD_POST ,
105105 ]
106106 ),
107107 ]);
108108 }
109109
110- /**
111- * @throws NonUniqueResultException
112- */
113110 public function editAction (): ResponseInterface
114111 {
115112 $ uuid = $ this ->getAttribute ('uuid ' );
@@ -121,18 +118,18 @@ public function editAction(): ResponseInterface
121118
122119 if ($ this ->isPost ()) {
123120 $ this ->adminForm ->setData ($ this ->getPostParams ());
124- $ this ->adminForm ->setDifferentInputFilter (new EditAdminInputFilter ());
121+ $ this ->adminForm ->setInputFilter (new EditAdminInputFilter ());
125122 if ($ this ->adminForm ->isValid ()) {
126123 /** @var array $result */
127124 $ result = $ this ->adminForm ->getData ();
128125 try {
129126 $ this ->adminService ->updateAdmin ($ admin , $ result );
130127 return new JsonResponse (['message ' => Message::ADMIN_UPDATED_SUCCESSFULLY ]);
131- } catch (ORMException $ e ) {
128+ } catch (IdentityException $ e ) {
132129 $ this ->logErrors ($ e , Message::UPDATE_ADMIN );
133130 return new JsonResponse (
134131 ['message ' => $ e ->getMessage ()],
135- StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR
132+ StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY
136133 );
137134 } catch (Throwable $ e ) {
138135 $ this ->logErrors ($ e , Message::UPDATE_ADMIN );
@@ -157,24 +154,19 @@ public function editAction(): ResponseInterface
157154 [
158155 'form ' => $ this ->adminForm ,
159156 'formAction ' => '/admin/edit/ ' . $ uuid ,
157+ 'method ' => RequestMethodInterface::METHOD_POST ,
160158 ]
161159 ),
162160 ]);
163161 }
164162
165- /**
166- * @throws NonUniqueResultException
167- */
168163 public function deleteAction (): ResponseInterface
169164 {
170- if (! $ this ->isDelete ()) {
171- return new JsonResponse ([
172- 'error ' => [
173- 'messages ' => [
174- [Message::METHOD_NOT_ALLOWED ],
175- ],
176- ],
177- ], StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED );
165+ if (! $ this ->isPost ()) {
166+ return new JsonResponse (
167+ ['message ' => Message::METHOD_NOT_ALLOWED ],
168+ StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED
169+ );
178170 }
179171
180172 $ uuid = $ this ->getAttribute ('uuid ' );
@@ -185,9 +177,17 @@ public function deleteAction(): ResponseInterface
185177 );
186178 }
187179
180+ $ form = new AdminDeleteForm ();
181+ $ form ->setData ($ this ->getPostParams ());
182+ if (! $ form ->isValid ()) {
183+ return new JsonResponse (
184+ ['message ' => $ this ->forms ->getMessages ($ form )],
185+ StatusCodeInterface::STATUS_BAD_REQUEST
186+ );
187+ }
188+
188189 /** @var Admin $admin */
189190 $ admin = $ this ->adminService ->getAdminRepository ()->findOneBy (['uuid ' => $ uuid ]);
190-
191191 try {
192192 $ this ->adminService ->getAdminRepository ()->deleteAdmin ($ admin );
193193 return new JsonResponse (['message ' => Message::ADMIN_DELETED_SUCCESSFULLY ]);
@@ -200,9 +200,6 @@ public function deleteAction(): ResponseInterface
200200 }
201201 }
202202
203- /**
204- * @throws NonUniqueResultException
205- */
206203 public function listAction (): ResponseInterface
207204 {
208205 $ result = $ this ->adminService ->getAdmins (
@@ -219,7 +216,9 @@ public function listAction(): ResponseInterface
219216 public function manageAction (): ResponseInterface
220217 {
221218 return new HtmlResponse (
222- $ this ->template ->render ('admin::list ' )
219+ $ this ->template ->render ('admin::list ' , [
220+ 'form ' => new AdminDeleteForm (),
221+ ])
223222 );
224223 }
225224
@@ -233,8 +232,7 @@ public function loginAction(): ResponseInterface
233232 return new RedirectResponse ($ this ->router ->generateUri ("dashboard " ));
234233 }
235234
236- $ form = new LoginForm ();
237-
235+ $ form = new LoginForm ();
238236 $ shouldRebind = $ this ->messenger ->getData ('shouldRebind ' ) ?? true ;
239237 if ($ shouldRebind ) {
240238 $ this ->forms ->restoreState ($ form );
@@ -297,6 +295,7 @@ public function loginAction(): ResponseInterface
297295 public function logoutAction (): ResponseInterface
298296 {
299297 $ this ->authenticationService ->clearIdentity ();
298+
300299 return new RedirectResponse (
301300 $ this ->router ->generateUri ('admin ' , ['action ' => 'login ' ])
302301 );
@@ -317,7 +316,7 @@ public function accountAction(): ResponseInterface
317316 try {
318317 $ this ->adminService ->updateAdmin ($ admin , $ result );
319318 $ this ->messenger ->addSuccess (Message::ACCOUNT_UPDATE_SUCCESSFULLY );
320- } catch (ORMException $ e ) {
319+ } catch (IdentityException $ e ) {
321320 $ this ->logErrors ($ e , Message::UPDATE_ADMIN );
322321 $ this ->messenger ->addError ($ e ->getMessage ());
323322 } catch (Throwable $ e ) {
@@ -354,11 +353,11 @@ public function changePasswordAction(): ResponseInterface
354353 if ($ changePasswordForm ->isValid ()) {
355354 /** @var array $result */
356355 $ result = $ changePasswordForm ->getData ();
357- if (password_verify ($ result ['currentPassword ' ], $ admin -> getPassword () )) {
356+ if ($ admin -> verifyPassword ($ result ['currentPassword ' ])) {
358357 try {
359358 $ this ->adminService ->updateAdmin ($ admin , $ result );
360359 $ this ->messenger ->addSuccess (Message::ACCOUNT_UPDATE_SUCCESSFULLY );
361- } catch (ORMException $ e ) {
360+ } catch (IdentityException $ e ) {
362361 $ this ->logErrors ($ e , Message::CHANGE_PASSWORD );
363362 $ this ->messenger ->addError ($ e ->getMessage ());
364363 } catch (Throwable $ e ) {
@@ -398,7 +397,7 @@ public function listLoginsAction(): ResponseInterface
398397 return new JsonResponse ($ result );
399398 }
400399
401- public function logErrors (Throwable | Exception $ e , string $ message ): void
400+ private function logErrors (Throwable $ e , string $ message ): void
402401 {
403402 $ this ->logger ->err ($ message , [
404403 'error ' => $ e ->getMessage (),
0 commit comments