Procházet zdrojové kódy

Evolution #743: Suppression de son compte

Sevajol Bastien před 11 roky
rodič
revize
7e74d0d543

+ 3 - 0
app/Resources/translations/flash.fr.yml Zobrazit soubor

@@ -40,6 +40,9 @@ user:
40 40
     sent_recently: Un email de confirmation a déjà été envoyé récemment (pensez à vérifier votre boite SPAM)
41 41
     confirmed:     Votre email est maintenant confirmé
42 42
     failtoken:     La demande de confirmation est trop ancienne, veuillez en demander une nouvelle
43
+  delete:
44
+    fail:           Le mot de passe saisi est incorrect
45
+    success:        Le compte a bien été supprimé
43 46
 
44 47
 presubscription:
45 48
   success:         Un email a été envoyé a votre adresse email pour confirmation (vérifiez aussi dans vos SPAM)

+ 4 - 0
app/Resources/translations/userui.fr.yml Zobrazit soubor

@@ -98,6 +98,10 @@ my_account:
98 98
       playlists:    Les autres utilisateurs peuvent voir mes listes de lecture
99 99
     success:        Vos paramètres de vie privée ont bien été mises à jour
100 100
     error:          Une erreur est survenue lors de la mise à jour de vos paramètres de vie privée
101
+  delete:
102
+    title:          Supprimer mon compte
103
+    text:           Attention: Il vous sera impossible d'annuler cette action après avoir confirmé la suppression de votre compte !
104
+    submit:         Je confirme vouloir supprimer définitivement ce compte
101 105
 
102 106
 change_username:
103 107
   submit:           Changer

+ 26 - 0
src/Muzich/CoreBundle/Form/User/DeleteForm.php Zobrazit soubor

@@ -0,0 +1,26 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Form\User;
4
+
5
+use Symfony\Component\Security\Core\Validator\Constraint\UserPassword;
6
+use Symfony\Component\Form\FormBuilderInterface;
7
+use Symfony\Component\OptionsResolver\OptionsResolverInterface;
8
+use Symfony\Component\Form\AbstractType;
9
+
10
+class DeleteForm extends AbstractType
11
+{
12
+  public function buildForm(FormBuilderInterface $builder, array $options)
13
+  {
14
+    $builder->add('current_password', 'password', array(
15
+      'label' => 'form.current_password',
16
+      'translation_domain' => 'FOSUserBundle',
17
+      'mapped' => false,
18
+      'constraints' => new UserPassword(),
19
+    ));
20
+  }
21
+  
22
+  public function getName()
23
+  {
24
+    return 'delete_user_form';
25
+  }
26
+}

+ 1 - 1
src/Muzich/CoreBundle/Resources/public/css/main.css Zobrazit soubor

@@ -1642,7 +1642,7 @@ form.changeemail input[type="text"]
1642 1642
   width: 388px;
1643 1643
 }
1644 1644
 
1645
-form.changeemail input[type="submit"], form.privacy input[type="submit"]
1645
+form.changeemail input[type="submit"], form.privacy input[type="submit"], form.delete input[type="submit"]
1646 1646
 {
1647 1647
   float: right;
1648 1648
   margin-top: 5px;

+ 30 - 0
src/Muzich/CoreBundle/Tests/Controller/UserControllerTest.php Zobrazit soubor

@@ -588,4 +588,34 @@ class UserControllerTest extends FunctionalTest
588 588
     $this->isResponseSuccess();
589 589
   }
590 590
   
591
+  public function testDeleteUser()
592
+  {
593
+    $this->client = self::createClient();
594
+    
595
+    $joelle = $this->findUserByUsername('joelle');
596
+    $this->assertEquals('joelle', $joelle->getUsername());
597
+    $this->assertEquals('joelle@root', $joelle->getEmail());
598
+    $this->connectUser('joelle', 'toor');
599
+    $this->deleteUser('toor');
600
+    $joelle = $this->findOneBy('User', array('id' => $joelle->getId()));
601
+    $this->assertTrue('joelle' != $joelle->getUsername());
602
+    $this->assertTrue('joelle@mail.com' != $joelle->getEmail());
603
+    $this->connectUser('joelle', 'toor', null, false);
604
+  }
605
+  
606
+  protected function deleteUser($password)
607
+  {
608
+    $this->goToPage($this->generateUrl('my_account'));
609
+    $this->exist('form.delete');
610
+    $form = $this->selectForm('form.delete input[type="submit"]');
611
+    $form['delete_user_form[current_password]'] = $password;
612
+    $this->submit($form);
613
+    $this->isResponseRedirection();
614
+    $this->followRedirection();
615
+    $this->isResponseRedirection();
616
+    $this->followRedirection();
617
+    //$this->outputDebug();
618
+    $this->isResponseSuccess();
619
+  }
620
+  
591 621
 }

+ 5 - 0
src/Muzich/CoreBundle/lib/Controller.php Zobrazit soubor

@@ -693,4 +693,9 @@ class Controller extends BaseController
693 693
     return true;
694 694
   }
695 695
   
696
+  protected function getUserManager()
697
+  {
698
+    return $this->container->get('muzich_user_manager');
699
+  }
700
+  
696 701
 }

+ 13 - 6
src/Muzich/CoreBundle/lib/FunctionalTest.php Zobrazit soubor

@@ -88,7 +88,7 @@ class FunctionalTest extends WebTestCase
88 88
     ;
89 89
   }
90 90
   
91
-  protected function connectUser($login, $password, $client = null)
91
+  protected function connectUser($login, $password, $client = null, $success = true)
92 92
   {
93 93
     if (!$client)
94 94
     {
@@ -118,20 +118,27 @@ class FunctionalTest extends WebTestCase
118 118
     $this->isResponseSuccess();
119 119
 
120 120
     $user = $this->getUser();
121
-    if ('anon.' != $user)
121
+    if ($success)
122 122
     {
123
-      if (strpos($login, '@') === false)
123
+      if ('anon.' != $user)
124 124
       {
125
-        $this->assertEquals($login, $user->getUsername());
125
+        if (strpos($login, '@') === false)
126
+        {
127
+          $this->assertEquals($login, $user->getUsername());
128
+        }
129
+        else
130
+        {
131
+          $this->assertEquals($login, $user->getEmail());
132
+        }
126 133
       }
127 134
       else
128 135
       {
129
-        $this->assertEquals($login, $user->getEmail());
136
+        $this->assertTrue(false);
130 137
       }
131 138
     }
132 139
     else
133 140
     {
134
-      $this->assertTrue(false);
141
+      $this->assertEquals('anon.', $user);
135 142
     }
136 143
   }
137 144
   

+ 45 - 5
src/Muzich/UserBundle/Controller/UserController.php Zobrazit soubor

@@ -15,6 +15,7 @@ use Muzich\UserBundle\Form\Type\RegistrationFormType;
15 15
 use Muzich\CoreBundle\Entity\User;
16 16
 use Muzich\CoreBundle\Form\User\PasswordForm;
17 17
 use Muzich\CoreBundle\Form\User\PrivacyForm;
18
+use Muzich\CoreBundle\Form\User\DeleteForm;
18 19
 
19 20
 class UserController extends Controller
20 21
 {
@@ -50,6 +51,11 @@ class UserController extends Controller
50 51
     return $this->createForm(new PrivacyForm(), $this->getUser());
51 52
   }
52 53
   
54
+  protected function getDeleteForm()
55
+  {
56
+    return $this->createForm(new DeleteForm(), $this->getUser());
57
+  }
58
+  
53 59
   protected function getTagsFavoritesForm($user)
54 60
   {
55 61
     $ids = array();
@@ -98,7 +104,8 @@ class UserController extends Controller
98 104
       'change_email_form'        => $change_email_form->createView(),
99 105
       'avatar_form'              => $this->getAvatarForm()->createView(),
100 106
       'preferences_form'         => $this->getPreferencesForm()->createView(),
101
-      'privacy_form'             => $this->getPrivacyForm()->createView()
107
+      'privacy_form'             => $this->getPrivacyForm()->createView(),
108
+      'delete_form'              => $this->getDeleteForm()->createView()
102 109
     );
103 110
   }
104 111
   
@@ -257,7 +264,6 @@ class UserController extends Controller
257 264
     }
258 265
     
259 266
     $form_tags_favorites = $this->getTagsFavoritesForm($user);
260
-    $change_email_form = $this->getChangeEmailForm();
261 267
 
262 268
     return $this->container->get('templating')->renderResponse(
263 269
       'MuzichUserBundle:User:account.html.twig',
@@ -268,10 +274,11 @@ class UserController extends Controller
268 274
         'form_tags_favorites'      => $form_tags_favorites->createView(),
269 275
         'form_tags_favorites_name' => $form_tags_favorites->getName(),
270 276
         'favorite_tags_id'         => $this->getTagsFavorites(),
271
-        'change_email_form'        => $change_email_form->createView(),
277
+        'change_email_form'        => $this->getChangeEmailForm()->createView(),
272 278
         'avatar_form'              => $this->getAvatarForm()->createView(),
273 279
         'preferences_form'         => $this->getPreferencesForm()->createView(),
274
-        'privacy_form'             => $this->getPrivacyForm()->createView()
280
+        'privacy_form'             => $this->getPrivacyForm()->createView(),
281
+        'delete_form'              => $this->getDeleteForm()->createView()
275 282
       )
276 283
     );
277 284
   }
@@ -450,7 +457,8 @@ class UserController extends Controller
450 457
         'change_email_form'        => $change_email_form->createView(),
451 458
         'avatar_form'              => $this->getAvatarForm()->createView(),
452 459
         'preferences_form'         => $this->getPreferencesForm()->createView(),
453
-        'privacy_form'             => $this->getPrivacyForm()->createView()
460
+        'privacy_form'             => $this->getPrivacyForm()->createView(),
461
+        'delete_form'              => $this->getDeleteForm()->createView()
454 462
       )
455 463
     );
456 464
   }
@@ -783,4 +791,36 @@ class UserController extends Controller
783 791
     ));
784 792
   }
785 793
   
794
+  public function deleteAction(Request $request)
795
+  {
796
+    $form = $this->getDeleteForm();
797
+    $form->bind($request);
798
+    
799
+    if ($form->isValid())
800
+    {
801
+      $this->getUserManager()->disableUser($form->getData());
802
+      $this->setFlash('success', 'user.delete.success');
803
+      return $this->redirect($this->generateUrl('fos_user_security_logout'));
804
+    }
805
+    
806
+    $this->setFlash('error', 'user.delete.fail');
807
+    $form_tags_favorites = $this->getTagsFavoritesForm($form->getData());
808
+    return $this->container->get('templating')->renderResponse(
809
+      'MuzichUserBundle:User:account.html.twig',
810
+      array(
811
+        'form_password'            => $this->getChangePasswordForm($form->getData())->createView(),
812
+        'errors_pers'              => array(),
813
+        'user'                     => $form->getData(),
814
+        'form_tags_favorites'      => $form_tags_favorites->createView(),
815
+        'form_tags_favorites_name' => $form_tags_favorites->getName(),
816
+        'favorite_tags_id'         => $this->getTagsFavorites(),
817
+        'change_email_form'        => $this->getChangeEmailForm()->createView(),
818
+        'avatar_form'              => $this->getAvatarForm()->createView(),
819
+        'preferences_form'         => $this->getPreferencesForm()->createView(),
820
+        'privacy_form'             => $this->getPrivacyForm()->createView(),
821
+        'delete_form'              => $form->createView()
822
+      )
823
+    );
824
+  }
825
+  
786 826
 }

+ 9 - 0
src/Muzich/UserBundle/Entity/UserManager.php Zobrazit soubor

@@ -11,6 +11,7 @@ use FOS\UserBundle\Util\CanonicalizerInterface;
11 11
 use Doctrine\ORM\EntityManager;
12 12
 //use FOS\UserBundle\Entity\UserManager as UserManagerBase; UPGRADE FOSUserBundle 1.3
13 13
 use FOS\UserBundle\Doctrine\UserManager as UserManagerBase;
14
+use Muzich\CoreBundle\Entity\User;
14 15
 
15 16
 /**
16 17
  */
@@ -85,5 +86,13 @@ class UserManager extends UserManagerBase
85 86
   {
86 87
     return $this->emailCanonicalizer->canonicalize($email);
87 88
   }
89
+  
90
+  public function disableUser(User $user)
91
+  {
92
+    $user->setEnabled(false);
93
+    $user->setUsername(time());
94
+    $user->setEmail('deleted_'.time().'@mail.com');
95
+    $this->updateUser($user);
96
+  }
88 97
 
89 98
 }

+ 6 - 1
src/Muzich/UserBundle/Resources/config/routing.yml Zobrazit soubor

@@ -94,4 +94,9 @@ send_email_confirmation:
94 94
 helpbox_tags_favorites:
95 95
   pattern: /account/favorites-tags
96 96
   defaults: { _controller: MuzichUserBundle:User:favoriteTagsHelpbox }
97
-    
97
+
98
+delete_user:
99
+  pattern: /account/delete
100
+  defaults: { _controller: MuzichUserBundle:User:delete }
101
+  requirements:
102
+    _method:  POST

+ 31 - 1
src/Muzich/UserBundle/Resources/views/User/account.html.twig Zobrazit soubor

@@ -57,7 +57,7 @@
57 57
     <h2 data-open="myaccount_favorites_password">{{ 'my_account.change_password'|trans({}, 'userui') }}</h2>
58 58
 
59 59
     <div id="myaccount_favorites_password" class="myaccount_part {% if app.request.get('open') == 'change_password' %}flashing{% endif %}" 
60
-      {% if not form_password|form_has_errors and not app.request.get('open') == 'change_password' %}
60
+      {% if not app.request.get('open') == 'change_password' %}
61 61
        style="display: none;"
62 62
       {% endif %}
63 63
      >
@@ -150,5 +150,35 @@
150 150
     </div>
151 151
     <div style="clear:both;"></div>
152 152
     
153
+    
154
+    <h2 data-open="myaccount_delete">{{ 'my_account.delete.title'|trans({}, 'userui') }}</h2>
155
+  
156
+    <div id="myaccount_delete" class="myaccount_part"
157
+      {% if not delete_form|form_has_errors and not app.request.get('open') == 'delete' %}
158
+        style="display: none;"
159
+      {% endif %}
160
+    >
161
+      
162
+      <p class="warning info">{{ 'my_account.delete.text'|trans({}, 'userui') }}
163
+      </p>
164
+      
165
+      <form
166
+        class="delete"
167
+        action="{{ path('delete_user', {'open':'delete'}) }}"
168
+        method="post"
169
+      >
170
+        
171
+        <div class="field">
172
+          {{ form_label(delete_form.current_password) }}
173
+          {{ form_widget(delete_form.current_password) }}
174
+        </div>
175
+        
176
+        {{ form_rest(delete_form) }}
177
+      
178
+          <input type="submit" class="button" value="{{ 'my_account.delete.submit'|trans({}, 'userui') }}" />
179
+      </form>
180
+    </div>
181
+    <div style="clear:both;"></div>
182
+    
153 183
   </div>
154 184
 {% endblock %}