Pārlūkot izejas kodu

Mise a plat du code d'inscription. Debut d'écriture de la page d'arrivée après inscriptionb.

bastien 13 gadus atpakaļ
vecāks
revīzija
b4cf01fe46

+ 12 - 0
src/Muzich/CoreBundle/Entity/User.php Parādīt failu

@@ -13,6 +13,7 @@ use Gedmo\Mapping\Annotation as Gedmo;
13 13
  * @ORM\Entity
14 14
  * @ORM\Table(name="m_user")
15 15
  * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\UserRepository")
16
+ * @ORM\HasLifecycleCallbacks()
16 17
  */
17 18
 class User extends BaseUser
18 19
 {
@@ -272,6 +273,17 @@ class User extends BaseUser
272 273
     return $this->getUsername();
273 274
   }
274 275
   
276
+//  /**
277
+//  * @ORM\prePersist
278
+//  */
279
+//  public function setSlug()
280
+//  {
281
+//    if (!$this->slug)
282
+//    {
283
+//      
284
+//    }
285
+//  }
286
+//  
275 287
   /**
276 288
    * Retourn si l'user_id transmis fait partis des enregistrements
277 289
    * followed de l'objet.

+ 17 - 1
src/Muzich/CoreBundle/lib/Controller.php Parādīt failu

@@ -4,11 +4,22 @@ namespace Muzich\CoreBundle\lib;
4 4
 
5 5
 use Symfony\Bundle\FrameworkBundle\Controller\Controller as BaseController;
6 6
 use Muzich\CoreBundle\Searcher\ElementSearcher;
7
+use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
7 8
 
8 9
 class Controller extends BaseController
9 10
 {
10 11
   
11
-  //private $ElementSearcher = null;
12
+  /**
13
+   * Authenticate a user with Symfony Security
14
+   *
15
+   */
16
+  protected function authenticateUser($user)
17
+  {
18
+    $providerKey = $this->container->getParameter('fos_user.firewall_name');
19
+    $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
20
+
21
+    $this->container->get('security.context')->setToken($token);
22
+  }
12 23
     
13 24
   /**
14 25
    * Met a jour l'objet ElementSearcher (en réallité on met a jour les
@@ -89,4 +100,9 @@ class Controller extends BaseController
89 100
     return $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->getTagsArray();
90 101
   }
91 102
   
103
+  protected function setFlash($type, $value)
104
+  {
105
+    $this->container->get('session')->setFlash($type, $value);
106
+  }
107
+  
92 108
 }

+ 10 - 32
src/Muzich/IndexBundle/Controller/IndexController.php Parādīt failu

@@ -3,17 +3,16 @@
3 3
 namespace Muzich\IndexBundle\Controller;
4 4
 
5 5
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
-use FOS\UserBundle\Controller\SecurityController as BaseController;
6
+use Muzich\CoreBundle\lib\Controller;
7 7
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8 8
 use Symfony\Component\Security\Core\SecurityContext;
9 9
 use Symfony\Component\Security\Core\Exception\AuthenticationException;
10
-use Symfony\Component\HttpFoundation\RedirectResponse;
11 10
 
12 11
 use Muzich\CoreBundle\Entity\Tag;
13 12
 use Muzich\CoreBundle\Entity\UsersTagsFavorites;
14 13
 use Muzich\CoreBundle\Entity\User;
15 14
 
16
-class IndexController extends BaseController
15
+class IndexController extends Controller
17 16
 {
18 17
   
19 18
   /**
@@ -23,8 +22,12 @@ class IndexController extends BaseController
23 22
   public function indexAction()
24 23
   {
25 24
     $vars = $this->proceedLogin();
26
-    $vars = array_merge($vars, $this->proceedRegister());
27
-    return $vars;
25
+    
26
+    $form = $this->container->get('fos_user.registration.form');
27
+    
28
+    return array_merge($vars, array(
29
+      'form' => $form->createView()
30
+    ));
28 31
   }
29 32
   
30 33
   /**
@@ -67,34 +70,9 @@ class IndexController extends BaseController
67 70
    * 
68 71
    * @return type array
69 72
    */
70
-  protected function proceedRegister()
73
+  public function proceedRegister()
71 74
   {
72
-    $form = $this->container->get('fos_user.registration.form');
73
-    $formHandler = $this->container->get('fos_user.registration.form.handler');
74
-    $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
75
-
76
-    $process = $formHandler->process($confirmationEnabled);
77
-    if ($process) {
78
-        $user = $form->getData();
79
-
80
-        if ($confirmationEnabled) {
81
-            $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
82
-            $route = 'fos_user_registration_check_email';
83
-        } else {
84
-            $this->authenticateUser($user);
85
-            $route = 'fos_user_registration_confirmed';
86
-        }
87
-
88
-        $this->setFlash('fos_user_success', 'registration.flash.user_created');
89
-        $url = $this->container->get('router')->generate($route);
90
-
91
-        return new RedirectResponse($url);
92
-    }
93
-
94
-    return array(
95
-        'form' => $form->createView(),
96
-        'theme' => $this->container->getParameter('fos_user.template.theme'),
97
-    );
75
+    
98 76
   }
99 77
   
100 78
 }

+ 44 - 0
src/Muzich/UserBundle/Controller/UserController.php Parādīt failu

@@ -24,6 +24,40 @@ class UserController extends Controller
24 24
         'form_password' => $form_password->createView()
25 25
       );
26 26
   }
27
+  
28
+  public function registerAction()
29
+  {
30
+    $form = $this->container->get('fos_user.registration.form');
31
+    $formHandler = $this->container->get('fos_user.registration.form.handler');
32
+    $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
33
+    
34
+    $process = $formHandler->process($confirmationEnabled);
35
+    if ($process) {
36
+      $user = $form->getData();
37
+
38
+      if ($confirmationEnabled) {
39
+        $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
40
+        $route = 'fos_user_registration_check_email';
41
+      } else {
42
+        $this->authenticateUser($user);
43
+        $route = 'register_finish';
44
+      }
45
+
46
+      $this->setFlash('success', 'Votre compte a bien été créé');
47
+      $url = $this->generateUrl($route);
48
+
49
+      return new RedirectResponse($url);
50
+    }
51
+
52
+    return $this->container->get('templating')->renderResponse(
53
+      'MuzichIndexBundle:Index:index.html.twig',
54
+      array(
55
+        'form' => $form->createView(),
56
+        'error' => null,
57
+        'last_username' => null
58
+      )
59
+    );
60
+  }
27 61
     
28 62
   public function changePasswordAction()
29 63
   {
@@ -54,5 +88,15 @@ class UserController extends Controller
54 88
 
55 89
     
56 90
   }
91
+  
92
+  /**
93
+   * 
94
+   * @Template()
95
+   */
96
+  public function registerFinishAction()
97
+  {
98
+    
99
+    return array();
100
+  }
57 101
     
58 102
 }

+ 189 - 0
src/Muzich/UserBundle/Entity/UserManager.php Parādīt failu

@@ -0,0 +1,189 @@
1
+<?php
2
+
3
+/*
4
+ * 
5
+ */
6
+
7
+namespace Muzich\UserBundle\Entity;
8
+
9
+use Doctrine\ORM\EntityManager;
10
+use FOS\UserBundle\Model\UserInterface;
11
+use FOS\UserBundle\Model\UserManager as BaseUserManager;
12
+use FOS\UserBundle\Util\CanonicalizerInterface;
13
+use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
14
+use Symfony\Component\Validator\Constraint;
15
+
16
+class UserManager extends BaseUserManager
17
+{
18
+    protected $em;
19
+    protected $class;
20
+    protected $repository;
21
+
22
+    /**
23
+     * Constructor.
24
+     *
25
+     * @param EncoderFactoryInterface $encoderFactory
26
+     * @param string                  $algorithm
27
+     * @param CanonicalizerInterface  $usernameCanonicalizer
28
+     * @param CanonicalizerInterface  $emailCanonicalizer
29
+     * @param EntityManager           $em
30
+     * @param string                  $class
31
+     */
32
+    public function __construct(EncoderFactoryInterface $encoderFactory, $algorithm, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, EntityManager $em, $class)
33
+    {
34
+        parent::__construct($encoderFactory, $algorithm, $usernameCanonicalizer, $emailCanonicalizer);
35
+        
36
+        $this->em = $em;
37
+        $this->repository = $em->getRepository($class);
38
+
39
+        $metadata = $em->getClassMetadata($class);
40
+        $this->class = $metadata->name;
41
+
42
+        // Slug stuff
43
+        $evm = new \Doctrine\Common\EventManager();
44
+        // ORM and ODM
45
+        $sluggableListener = new \Gedmo\Sluggable\SluggableListener();
46
+        $evm->addEventSubscriber($sluggableListener);
47
+        // now this event manager should be passed to entity manager constructor
48
+        $this->em->getEventManager()->addEventSubscriber($sluggableListener);
49
+    }
50
+
51
+    /**
52
+     * {@inheritDoc}
53
+     */
54
+    public function deleteUser(UserInterface $user)
55
+    {
56
+        $this->em->remove($user);
57
+        $this->em->flush();
58
+    }
59
+
60
+    /**
61
+     * {@inheritDoc}
62
+     */
63
+    public function getClass()
64
+    {
65
+        return $this->class;
66
+    }
67
+
68
+    /**
69
+     * {@inheritDoc}
70
+     */
71
+    public function findUserBy(array $criteria)
72
+    {
73
+        return $this->repository->findOneBy($criteria);
74
+    }
75
+
76
+    /**
77
+     * {@inheritDoc}
78
+     */
79
+    public function findUsers()
80
+    {
81
+        return $this->repository->findAll();
82
+    }
83
+
84
+    /**
85
+     * {@inheritDoc}
86
+     */
87
+    public function reloadUser(UserInterface $user)
88
+    {
89
+        $this->em->refresh($user);
90
+    }
91
+
92
+    /**
93
+     * Updates a user.
94
+     *
95
+     * @param UserInterface $user
96
+     * @param Boolean $andFlush Whether to flush the changes (default true)
97
+     */
98
+    public function updateUser(UserInterface $user, $andFlush = true)
99
+    {
100
+        $this->updateCanonicalFields($user);
101
+        $this->updatePassword($user);
102
+
103
+        $this->em->persist($user);
104
+        if ($andFlush) {
105
+            $this->em->flush();
106
+        }
107
+    }
108
+
109
+    /**
110
+     * {@inheritDoc}
111
+     */
112
+    public function validateUnique(UserInterface $value, Constraint $constraint)
113
+    {
114
+        // Since we probably want to validate the canonical fields,
115
+        // we'd better make sure we have them.
116
+        $this->updateCanonicalFields($value);
117
+
118
+        $fields = array_map('trim', explode(',', $constraint->property));
119
+        $users = $this->findConflictualUsers($value, $fields);
120
+
121
+        // there is no conflictual user
122
+        if (empty($users)) {
123
+            return true;
124
+        }
125
+
126
+        // there is no conflictual user which is not the same as the value
127
+        if ($this->anyIsUser($value, $users)) {
128
+            return true;
129
+        }
130
+
131
+        return false;
132
+    }
133
+
134
+    /**
135
+     * Indicates whether the given user and all compared objects correspond to the same record.
136
+     *
137
+     * @param UserInterface $user
138
+     * @param array $comparisons
139
+     * @return Boolean
140
+     */
141
+    protected function anyIsUser($user, array $comparisons)
142
+    {
143
+        foreach ($comparisons as $comparison) {
144
+            if (!$user->isUser($comparison)) {
145
+                return false;
146
+            }
147
+        }
148
+
149
+        return true;
150
+    }
151
+
152
+    /**
153
+     * Gets conflictual users for the given user and constraint.
154
+     *
155
+     * @param UserInterface $value
156
+     * @param array $fields
157
+     * @return array
158
+     */
159
+    protected function findConflictualUsers($value, array $fields)
160
+    {
161
+        return $this->repository->findBy($this->getCriteria($value, $fields));
162
+    }
163
+
164
+    /**
165
+     * Gets the criteria used to find conflictual entities.
166
+     *
167
+     * @param UserInterface $value
168
+     * @param array $constraint
169
+     * @return array
170
+     */
171
+    protected function getCriteria($value, array $fields)
172
+    {
173
+        $classMetadata = $this->em->getClassMetadata($this->class);
174
+
175
+        $criteria = array();
176
+        foreach ($fields as $field) {
177
+            if (!$classMetadata->hasField($field)) {
178
+                throw new \InvalidArgumentException(sprintf('The "%s" class metadata does not have any "%s" field or association mapping.', $this->class, $field));
179
+            }
180
+
181
+            $criteria[$field] = $classMetadata->getFieldValue($value, $field);
182
+        }
183
+
184
+        return $criteria;
185
+    }
186
+}
187
+
188
+
189
+?>

+ 8 - 0
src/Muzich/UserBundle/Resources/config/routing.yml Parādīt failu

@@ -7,4 +7,12 @@ my_account:
7 7
 change_password:
8 8
   pattern: /account/change-password
9 9
   defaults: { _controller: MuzichUserBundle:User:changePassword }
10
+  
11
+register:
12
+  pattern: /register/do
13
+  defaults: { _controller: MuzichUserBundle:User:register }
14
+  
15
+register_finish:
16
+  pattern: /register/finish
17
+  defaults: { _controller: MuzichUserBundle:User:registerFinish }
10 18
   

+ 15 - 1
src/Muzich/UserBundle/Resources/views/Registration/register.html.twig Parādīt failu

@@ -1,9 +1,23 @@
1 1
 <div class="register">
2
-  <form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} 
2
+  <form action="{{ path('register') }}" {{ form_enctype(form) }} 
3 3
     method="POST" 
4 4
     class="fos_user_registration_register"
5 5
   >
6
+    
6 7
     {{ form_widget(form) }}
8
+    
9
+    {{ form_errors(form) }}
10
+
11
+    {{ form_row(form.username) }}
12
+    
13
+    {{ form_row(form.email) }}
14
+    
15
+    {{ form_row(form.plainPassword.first) }}
16
+    
17
+    {{ form_row(form.plainPassword.second) }}
18
+
19
+    {{ form_rest(form) }}
20
+    
7 21
     <div>
8 22
         <input type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" />
9 23
     </div>

+ 9 - 0
src/Muzich/UserBundle/Resources/views/User/registerFinish.html.twig Parādīt failu

@@ -0,0 +1,9 @@
1
+{% extends "MuzichUserBundle::layout.html.twig" %}
2
+
3
+{% block title %}Inscription terminé{% endblock %}
4
+
5
+{% block content %}
6
+
7
+  <h2>Inscription terminé !</h2>
8
+      
9
+{% endblock %}

+ 1 - 0
web/bundles/muzichcore/js/muzich.js Parādīt failu

@@ -24,6 +24,7 @@ $(document).ready(function(){
24 24
   // When message is clicked, hide it
25 25
   $('.message a.message-close').click(function(){			  
26 26
     $(this).parent('.message').animate({top: -$(this).outerHeight()-50}, 700);
27
+    return false;
27 28
   });		 
28 29
 		 
29 30
 });