Browse Source

Evolution #569: token réutilisable avec compteur

Sevajol Bastien 11 years ago
parent
commit
b65a735ef8

+ 1 - 1
app/Resources/translations/flash.fr.yml View File

@@ -5,7 +5,7 @@ group.flash.created: Le groupe a été créé.
5 5
 group.flash.deleted: Le groupe a été supprimé.
6 6
 profile.flash.updated: Le profil a été mis à jour.
7 7
 change_password.flash.success: Le mot de passe a été modifié.
8
-registration.flash.user_created: Un email a été envoyé a l'adresse email saisie.
8
+registration.flash.user_created: Compté créé avec succés !
9 9
 resetting.flash.success: Le mot de passe a été réinitialisé avec succès.
10 10
 
11 11
 # muzich

+ 1 - 1
app/Resources/translations/validators.fr.yml View File

@@ -39,4 +39,4 @@ error:
39 39
       
40 40
 registration:
41 41
   token:
42
-    error:               Le code saisie pour l'inscription n'est pas correct
42
+    error:               Le code saisie pour l'inscription est incorrect ou n'est plus utilisable

+ 4 - 0
src/Muzich/AdminBundle/Admin/RegistrationTokenAdmin.php View File

@@ -16,6 +16,8 @@ class RegistrationTokenAdmin extends Admin
16 16
       ->addIdentifier('id')
17 17
       ->addIdentifier('token')
18 18
       ->add('used')
19
+      ->add('count')
20
+      ->add('count_max')
19 21
       ->add('_action', 'actions', array(
20 22
         'actions' => array(
21 23
           'view' => array(),
@@ -37,6 +39,8 @@ class RegistrationTokenAdmin extends Admin
37 39
   {
38 40
     $formMapper
39 41
       ->add('token')
42
+      ->add('count')
43
+      ->add('count_max')
40 44
       ->add('used')
41 45
     ;
42 46
   }

+ 46 - 1
src/Muzich/CoreBundle/Entity/RegistrationToken.php View File

@@ -38,9 +38,21 @@ class RegistrationToken
38 38
   /**
39 39
    * Si ce boolean est a vrai il n'est plus utilisable
40 40
    * @ORM\Column(type="boolean", nullable=true)
41
-   * @var type boolean
41
+   * @var boolean
42 42
    */
43 43
   protected $used = false;
44
+  
45
+  /**
46
+   * @ORM\Column(type="integer", nullable=true)
47
+   * @var int
48
+   */
49
+  protected $count = 0;
50
+  
51
+  /**
52
+   * @ORM\Column(type="integer", nullable=true)
53
+   * @var int
54
+   */
55
+  protected $count_max = 1;
44 56
     
45 57
   public function __toString()
46 58
   {
@@ -96,5 +108,38 @@ class RegistrationToken
96 108
   {
97 109
     return $this->used;
98 110
   }
111
+  
112
+  public function getCount()
113
+  {
114
+    if (!$this->count)
115
+    {
116
+      return 0;
117
+    }
118
+    return $this->count;
119
+  }
120
+  
121
+  public function setCount($count)
122
+  {
123
+    $this->count = $count;
124
+  }
125
+  
126
+  public function addUseCount()
127
+  {
128
+    $this->setCount($this->getCount()+1);
129
+    if ($this->getCount() >= $this->count_max)
130
+    {
131
+      $this->setUsed(true);
132
+    }
133
+  }
134
+  
135
+  public function getCountMax()
136
+  {
137
+    return $this->count_max;
138
+  }
139
+  
140
+  public function setCountMax($count_max)
141
+  {
142
+    $this->count_max = $count_max;
143
+  }
99 144
 
100 145
 }

+ 77 - 0
src/Muzich/CoreBundle/Tests/Controller/RegistrationTokenTest.php View File

@@ -0,0 +1,77 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+use Muzich\CoreBundle\Entity\RegistrationToken;
7
+
8
+class UserControllerTest extends FunctionalTest
9
+{
10
+  
11
+  public function testRegistrationToken()
12
+  {
13
+    $this->client = self::createClient();
14
+    $token = new RegistrationToken();
15
+    $token_name = 'token_test_3_max_'.time();
16
+    $token->setToken($token_name);
17
+    $token->setCountMax(3);
18
+    $em = $this->getDoctrine()->getEntityManager();
19
+    $em->persist($token);
20
+    $em->flush();
21
+    
22
+    $this->procedure_registration_success(
23
+      'user1', 
24
+      'user1@mail.com', 
25
+      'toor', 
26
+      'toor',
27
+      $token_name
28
+    );
29
+    
30
+    $this->disconnectUser();
31
+    
32
+    $this->procedure_registration_success(
33
+      'user2', 
34
+      'user2@mail.com', 
35
+      'toor', 
36
+      'toor',
37
+      $token_name
38
+    );
39
+    
40
+    $this->disconnectUser();
41
+    
42
+    $this->procedure_registration_success(
43
+      'user3', 
44
+      'user3@mail.com', 
45
+      'toor', 
46
+      'toor',
47
+      $token_name
48
+    );
49
+    
50
+    $this->disconnectUser();
51
+    
52
+    $this->procedure_registration_failure(
53
+      'user4', 
54
+      'user4@mail.com', 
55
+      'toor', 
56
+      'toor',
57
+      $token_name
58
+    );
59
+        
60
+    $this->procedure_registration_failure(
61
+      'user5', 
62
+      'user5@mail.com', 
63
+      'toor', 
64
+      'toor',
65
+      $token_name
66
+    );
67
+        
68
+    $this->procedure_registration_failure(
69
+      'user6', 
70
+      'user6@mail.com', 
71
+      'toor', 
72
+      'toor',
73
+      ''
74
+    );
75
+  }
76
+  
77
+}

+ 0 - 2
src/Muzich/CoreBundle/lib/FunctionalTest.php View File

@@ -145,8 +145,6 @@ class FunctionalTest extends WebTestCase
145 145
       $token
146 146
     );
147 147
     
148
-    
149
-    
150 148
     $this->isResponseRedirection();
151 149
     $this->followRedirection();
152 150
     $this->isResponseSuccess();

+ 37 - 39
src/Muzich/UserBundle/Controller/UserController.php View File

@@ -161,54 +161,52 @@ class UserController extends Controller
161 161
     $form = $this->container->get('fos_user.registration.form');
162 162
     $formHandler = $this->container->get('fos_user.registration.form.handler');
163 163
     $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
164
+    $errors = array();
165
+    
166
+    // On contrôle le token en premier lieu
167
+    $form_values = $this->getRequest()->request->get($form->getName());
168
+    $r_token = $this->getDoctrine()->getRepository('MuzichCoreBundle:RegistrationToken')
169
+      ->findOneBy(array('token' => $form_values["token"], 'used' => false))
170
+    ;
164 171
     
165
-    // Pour palier bug, verif interne
166
-    if (count(($errors = $this->checkRegistrationInformations($form))) < 1)
172
+    if ($r_token)
167 173
     {
168
-      $process = $formHandler->process($confirmationEnabled);
169
-      if ($process) {
170
-        $user = $form->getData();
171
-
172
-        if ($confirmationEnabled) {
173
-          $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
174
-          $route = 'fos_user_registration_check_email';
175
-        } else {
176
-          $this->authenticateUser($user);
177
-          $route = 'start';
178
-        }
179
-
180
-        $this->setFlash('fos_user_success', 'registration.flash.user_created');
181
-        $url = $this->generateUrl($route);
182
-
183
-        
184
-        /**
185
-         * Contrôle du token
186
-         */
187
-        $form_values = $this->getRequest()->request->get($form->getName());
188
-        $r_token = $this->getDoctrine()->getRepository('MuzichCoreBundle:RegistrationToken')
189
-          ->findOneBy(array('token' => $form_values["token"], 'used' => false))
190
-        ;
174
+      if (count(($errors = $this->checkRegistrationInformations($form))) < 1)
175
+      {
176
+        $process = $formHandler->process($confirmationEnabled);
177
+        if ($process) {
178
+          $user = $form->getData();
179
+  
180
+          if ($confirmationEnabled) {
181
+            $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
182
+            $route = 'fos_user_registration_check_email';
183
+          } else {
184
+            $this->authenticateUser($user);
185
+            $route = 'start';
186
+          }
191 187
           
192
-        if (!$r_token)
193
-        {
194
-          $errors[] = $this->get('translator')->trans(
195
-            'registration.token.error', 
196
-            array(),
197
-            'validators'
198
-          );
199
-        }
200
-        else
201
-        {
202
-          $r_token->setUsed(true);
188
+          $this->setFlash('fos_user_success', 'registration.flash.user_created');
189
+          $url = $this->generateUrl($route);
190
+          
191
+          $r_token->addUseCount();
203 192
           $em = $this->getDoctrine()->getEntityManager();
204 193
           $em->persist($r_token);
205 194
           $em->flush();
195
+          
196
+          return new RedirectResponse($url);
206 197
         }
207
-        
208
-        return new RedirectResponse($url);
209 198
       }
210 199
     }
211
-
200
+    else
201
+    {
202
+      $form->bindRequest($this->getRequest());
203
+      $errors[] = $this->get('translator')->trans(
204
+        'registration.token.error', 
205
+        array(),
206
+        'validators'
207
+      );
208
+    }
209
+    
212 210
     return $this->container->get('templating')->renderResponse(
213 211
       'MuzichIndexBundle:Index:index.html.twig',
214 212
       array(