7
7
use App \Entity \NewLocation ;
8
8
use App \Entity \Preference ;
9
9
use App \Entity \ProfileVisit ;
10
+ use App \Form \DeleteProfileFormType ;
10
11
use App \Form \ProfileStatusFormType ;
11
12
use App \Form \SearchLocationType ;
12
13
use App \Form \SetLocationType ;
14
+ use App \Model \ProfileModel ;
13
15
use App \Repository \ProfileVisitRepository ;
14
16
use App \Utilities \ChangeProfilePictureGlobals ;
15
17
use App \Utilities \ProfileSubmenu ;
16
18
use Doctrine \ORM \EntityManagerInterface ;
17
19
use Sensio \Bundle \FrameworkExtraBundle \Configuration \ParamConverter ;
18
20
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
21
+ use Symfony \Component \Form \FormError ;
19
22
use Symfony \Component \HttpFoundation \RedirectResponse ;
20
23
use Symfony \Component \HttpFoundation \Request ;
21
24
use Symfony \Component \HttpFoundation \Response ;
25
+ use Symfony \Component \PasswordHasher \Hasher \PasswordHasherFactoryInterface ;
22
26
use Symfony \Component \Routing \Annotation \Route ;
23
27
use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
24
28
use Symfony \Contracts \Translation \TranslatorInterface ;
@@ -27,11 +31,16 @@ class ProfileController extends AbstractController
27
31
{
28
32
private ProfileSubmenu $ profileSubmenu ;
29
33
private ChangeProfilePictureGlobals $ globals ;
34
+ private EntityManagerInterface $ entityManager ;
30
35
31
- public function __construct (ChangeProfilePictureGlobals $ globals , ProfileSubmenu $ profileSubmenu )
32
- {
36
+ public function __construct (
37
+ ChangeProfilePictureGlobals $ globals ,
38
+ ProfileSubmenu $ profileSubmenu ,
39
+ EntityManagerInterface $ entityManager
40
+ ) {
33
41
$ this ->globals = $ globals ;
34
42
$ this ->profileSubmenu = $ profileSubmenu ;
43
+ $ this ->entityManager = $ entityManager ;
35
44
}
36
45
37
46
/**
@@ -90,7 +99,6 @@ public function setMemberStatus(Request $request, EntityManagerInterface $entity
90
99
*/
91
100
public function showMyVisitors (
92
101
Member $ member ,
93
- ProfileSubmenu $ profileSubmenu ,
94
102
EntityManagerInterface $ entityManager ,
95
103
int $ page = 1
96
104
): Response {
@@ -119,7 +127,7 @@ public function showMyVisitors(
119
127
'member ' => $ member ,
120
128
'visits ' => $ visits ,
121
129
'globals_js_json ' => $ this ->globals ->getGlobalsJsAsJson ($ member , $ loggedInMember ),
122
- 'submenu ' => $ profileSubmenu ->getSubmenu ($ member , $ loggedInMember , ['active ' => 'visitors ' ]),
130
+ 'submenu ' => $ this -> profileSubmenu ->getSubmenu ($ member , $ loggedInMember , ['active ' => 'visitors ' ]),
123
131
]);
124
132
}
125
133
@@ -137,7 +145,6 @@ public function redirectToSetLocation(): RedirectResponse
137
145
public function setLocation (
138
146
Request $ request ,
139
147
Member $ member ,
140
- ProfileSubmenu $ profileSubmenu ,
141
148
EntityManagerInterface $ entityManager
142
149
): Response {
143
150
/** @var Member $loggedInMember */
@@ -188,6 +195,90 @@ public function setLocation(
188
195
]);
189
196
}
190
197
198
+ /**
199
+ * @Route("/deleteprofile", name="profile_delete_redirect")
200
+ */
201
+ public function deleteProfileNotLoggedIn (
202
+ Request $ request ,
203
+ ProfileModel $ profileModel ,
204
+ TranslatorInterface $ translator ,
205
+ PasswordHasherFactoryInterface $ passwordHasherFactory
206
+ ): Response {
207
+ /** @var Member $member */
208
+ $ member = $ this ->getUser ();
209
+
210
+ if (null !== $ member ) {
211
+ return $ this ->redirectToRoute ('profile_delete ' , ['username ' => $ member ->getUsername ()]);
212
+ }
213
+
214
+ $ deleteProfileForm = $ this ->createForm (DeleteProfileFormType::class, null , [
215
+ 'loggedIn ' => false ,
216
+ ]);
217
+ $ deleteProfileForm ->handleRequest ($ request );
218
+
219
+ if ($ deleteProfileForm ->isSubmitted () && $ deleteProfileForm ->isValid ()) {
220
+ $ data = $ deleteProfileForm ->getData ();
221
+ $ memberRepository = $ this ->entityManager ->getRepository (Member::class);
222
+ $ member = $ memberRepository ->find ($ data ['username ' ]);
223
+
224
+ $ verified = false ;
225
+ if (null === $ member ) {
226
+ $ deleteProfileForm ->addError (new FormError ($ translator ->trans ('profile.delete.credentials ' )));
227
+ } else {
228
+ $ passwordHasher = $ passwordHasherFactory ->getPasswordHasher ($ member );
229
+ $ verified = $ passwordHasher ->verify ($ member ->getPassword (), $ data ['password ' ]);
230
+
231
+ if (!$ verified ) {
232
+ $ deleteProfileForm ->addError (new FormError ($ translator ->trans ('profile.delete.credentials ' )));
233
+ }
234
+ }
235
+
236
+ $ success = false ;
237
+ if ($ verified ) {
238
+ $ success = $ profileModel ->retireProfile ($ member , $ data );
239
+ }
240
+
241
+ if ($ success ) {
242
+ return $ this ->redirectToRoute ('/logout ' );
243
+ }
244
+ }
245
+
246
+ return $ this ->render ('profile/delete.not.logged.in.html.twig ' , [
247
+ 'form ' => $ deleteProfileForm ->createView ()
248
+ ]);
249
+ }
250
+
251
+ /**
252
+ * @Route("/members/{username}/delete", name="profile_delete")
253
+ */
254
+ public function deleteProfile (Request $ request , Member $ member , ProfileModel $ profileModel ): Response
255
+ {
256
+ $ loggedInMember = $ this ->getUser ();
257
+ if ($ member !== $ loggedInMember ) {
258
+ return $ this ->redirectToRoute ('members_profile ' , ['username ' => $ member ->getUsername ()]);
259
+ }
260
+
261
+ $ deleteProfileForm = $ this ->createForm (DeleteProfileFormType::class, null , [
262
+ 'loggedIn ' => true ,
263
+ ]);
264
+ $ deleteProfileForm ->handleRequest ($ request );
265
+
266
+ if ($ deleteProfileForm ->isSubmitted () && $ deleteProfileForm ->isValid ()) {
267
+ $ success = $ profileModel ->retireProfile ($ member , $ deleteProfileForm ->getData ());
268
+
269
+ if ($ success ) {
270
+ return $ this ->redirectToRoute ('security_logout ' );
271
+ }
272
+ }
273
+
274
+ return $ this ->render ('profile/delete.html.twig ' , [
275
+ 'form ' => $ deleteProfileForm ->createView (),
276
+ 'member ' => $ member ,
277
+ 'globals_js_json ' => $ this ->globals ->getGlobalsJsAsJson ($ member , $ member ),
278
+ 'submenu ' => $ this ->profileSubmenu ->getSubmenu ($ member , $ member , ['active ' => 'profile ' ]),
279
+ ]);
280
+ }
281
+
191
282
private function renderProfile (bool $ ownProfile , Member $ member , Member $ loggedInMember ): Response
192
283
{
193
284
return $ this ->render ('profile/show.html.twig ' , [
@@ -197,4 +288,31 @@ private function renderProfile(bool $ownProfile, Member $member, Member $loggedI
197
288
'submenu ' => $ this ->profileSubmenu ->getSubmenu ($ member , $ loggedInMember ),
198
289
]);
199
290
}
291
+
292
+ private function deleteProfileProcess (Request $ request , bool $ loggedIn ): Response
293
+ {
294
+ $ deleteProfileForm = $ this ->createForm (DeleteProfileFormType::class, null , [
295
+ 'loggedIn ' => $ loggedIn ,
296
+ ]);
297
+ $ deleteProfileForm ->handleRequest ($ request );
298
+
299
+ if ($ deleteProfileForm ->isSubmitted () && $ deleteProfileForm ->isValid ()) {
300
+ $ data = $ deleteProfileForm ->getData ();
301
+ if (false === $ loggedIn ) {
302
+ // Check credentials
303
+ }
304
+
305
+ // handle delete profile form.
306
+
307
+ return $ this ->redirectToRoute ('logout ' );
308
+ }
309
+
310
+ return $ this ->render ('profile/delete.html.twig ' , [
311
+ 'form ' => $ deleteProfileForm ->createView (),
312
+ 'member ' => $ member ,
313
+ 'globals_js_json ' => $ this ->globals ->getGlobalsJsAsJson ($ member , $ member ),
314
+ 'submenu ' => $ profileSubmenu ->getSubmenu ($ member , $ member , ['active ' => 'profile ' ]),
315
+ ]);
316
+
317
+ }
200
318
}
0 commit comments