Browse Source

Evolution #685: Connexion avec comptes exterieurs

Bastien Sevajol 11 years ago
parent
commit
aaad6a24e8

+ 1 - 3
src/Muzich/CoreBundle/Entity/User.php View File

@@ -1179,9 +1179,7 @@ class User extends BaseUser
1179 1179
   public function setFacebookId($facebook_id)
1180 1180
   {
1181 1181
     $this->facebook_id = $facebook_id;
1182
-    $this->setUsername($facebook_id);
1183
-    $this->setUsernameUpdatable(true);
1184
-    $this->salt = '';
1182
+    $this->password_set = true;
1185 1183
   }
1186 1184
 
1187 1185
   /** @return string */

+ 8 - 0
src/Muzich/CoreBundle/Repository/UserRepository.php View File

@@ -266,4 +266,12 @@ class UserRepository extends EntityRepository
266 266
     ;
267 267
   }
268 268
   
269
+  public function countUsers()
270
+  {
271
+    return $this->getEntityManager()
272
+      ->createQuery("SELECT COUNT(u.id) FROM MuzichCoreBundle:User u")
273
+      ->getSingleScalarResult()
274
+    ;
275
+  }
276
+  
269 277
 }  

+ 53 - 56
src/Muzich/CoreBundle/Security/User/Provider/FacebookProvider.php View File

@@ -12,69 +12,66 @@ use \FacebookApiException;
12 12
 
13 13
 class FacebookProvider implements UserProviderInterface
14 14
 {
15
-    /**
16
-     * @var \Facebook
17
-     */
18
-    protected $facebook;
19
-    protected $userManager;
20
-    protected $validator;
21
-
22
-    public function __construct(BaseFacebook $facebook, $userManager, $validator)
23
-    {
24
-        $this->facebook = $facebook;
25
-        $this->userManager = $userManager;
26
-        $this->validator = $validator;
15
+  /**
16
+   * @var \Facebook
17
+   */
18
+  protected $facebook;
19
+  protected $userManager;
20
+  protected $validator;
21
+
22
+  public function __construct(BaseFacebook $facebook, $userManager, $validator)
23
+  {
24
+    $this->facebook = $facebook;
25
+    $this->userManager = $userManager;
26
+    $this->validator = $validator;
27
+  }
28
+
29
+  public function supportsClass($class)
30
+  {
31
+    return $this->userManager->supportsClass($class);
32
+  }
33
+
34
+  public function findUserByFbId($fbId)
35
+  {
36
+    return $this->userManager->findUserBy(array('facebook_id' => $fbId));
37
+  }
38
+
39
+  public function loadUserByUsername($username)
40
+  {
41
+    $user = $this->findUserByFbId($username);
42
+
43
+    try {
44
+      $fbdata = $this->facebook->api('/me');
45
+    } catch (FacebookApiException $e) {
46
+      throw new UsernameNotFoundException('The user is not authenticated on facebook');
47
+      $fbdata = null;
27 48
     }
28 49
 
29
-    public function supportsClass($class)
30
-    {
31
-        return $this->userManager->supportsClass($class);
32
-    }
50
+    if (!empty($fbdata)) {
51
+      if (empty($user)) {
52
+        $user = $this->userManager->getNewReadyUser();
53
+        $user->setFBData($fbdata);
54
+      }
33 55
 
34
-    public function findUserByFbId($fbId)
35
-    {
36
-        return $this->userManager->findUserBy(array('facebook_id' => $fbId));
56
+      if (count($this->validator->validate($user, 'Facebook'))) {
57
+        throw new UsernameNotFoundException('The facebook user could not be stored');
58
+      }
59
+      $this->userManager->updateUser($user);
37 60
     }
38 61
 
39
-    public function loadUserByUsername($username)
40
-    {
41
-        $user = $this->findUserByFbId($username);
42
-
43
-        try {
44
-            $fbdata = $this->facebook->api('/me');
45
-        } catch (FacebookApiException $e) {
46
-            throw new UsernameNotFoundException('The user is not authenticated on facebook');
47
-            $fbdata = null;
48
-        }
49
-
50
-        if (!empty($fbdata)) {
51
-            if (empty($user)) {
52
-                $user = $this->userManager->createUser();
53
-                $user->setEnabled(true);
54
-                $user->setPassword('');
55
-
56
-                $user->setFBData($fbdata); // Ici on passe les données Facebook à notre classe User afin de la mettre à jour
57
-            }
58
-
59
-            if (count($this->validator->validate($user, 'Facebook'))) {
60
-                throw new UsernameNotFoundException('The facebook user could not be stored');
61
-            }
62
-            $this->userManager->updateUser($user);
63
-        }
64
-
65
-        if (empty($user)) {
66
-            throw new UsernameNotFoundException('The user is not authenticated on facebook');
67
-        }
68
-
69
-        return $user;
62
+    if (empty($user)) {
63
+      throw new UsernameNotFoundException('The user is not authenticated on facebook');
70 64
     }
71 65
 
72
-    public function refreshUser(UserInterface $user)
73
-    {
74
-        if (!$this->supportsClass(get_class($user)) || !$user->getFacebookId()) {
75
-            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
76
-        }
66
+    return $user;
67
+  }
77 68
 
78
-        return $this->loadUserByUsername($user->getFacebookId());
69
+  public function refreshUser(UserInterface $user)
70
+  {
71
+    if (!$this->supportsClass(get_class($user)) || !$user->getFacebookId()) {
72
+      throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
79 73
     }
74
+
75
+    return $this->loadUserByUsername($user->getFacebookId());
76
+  }
80 77
 }

+ 1 - 52
src/Muzich/UserBundle/Controller/UserController.php View File

@@ -136,58 +136,7 @@ class UserController extends Controller
136 136
   /** @return User */
137 137
   protected function getNewUser()
138 138
   {
139
-    // Ce serais mieux d'appeler notre user manager et d'utiliser notre createUser
140
-    // avec ce code.
141
-    $userManager = $this->container->get('fos_user.user_manager');
142
-    $user = $userManager->createUser();
143
-    $user->setUsername($this->generateUsername());
144
-    $user->setPlainPassword($this->generatePassword(32));
145
-    $user->setEnabled(true);
146
-    $user->setCguAccepted(true);
147
-    $user->setEmailConfirmed(false);
148
-    $user->setUsernameUpdatable(true);
149
-    $user->setPasswordSet(false);
150
-    return $user;
151
-  }
152
-  
153
-  protected function generateUsername()
154
-  {
155
-    $qb = $this->getEntityManager()->createQueryBuilder();
156
-    $qb->select('count(id)');
157
-    $qb->from('MuzichCoreBundle:User','id');
158
-    $count = $qb->getQuery()->getSingleScalarResult();
159
-    
160
-    while ($this->usernameExist($count))
161
-    {
162
-      $count++;
163
-    }
164
-    
165
-    return 'User'.$count;
166
-  }
167
-  
168
-  protected function usernameExist($count)
169
-  {
170
-    $username = 'User'.$count;
171
-    if ($this->getEntityManager()->getRepository('MuzichCoreBundle:User')
172
-      ->findOneByUsername($username))
173
-    {
174
-      return true;
175
-    }
176
-    
177
-    return false;
178
-  }
179
-  
180
-  protected function generatePassword($length = 8)
181
-  {
182
-    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
183
-    $count = mb_strlen($chars);
184
-    
185
-    for ($i = 0, $result = ''; $i < $length; $i++) {
186
-        $index = rand(0, $count - 1);
187
-        $result .= mb_substr($chars, $index, 1);
188
-    }
189
-    
190
-    return $result;
139
+    return $this->container->get('muzich_user_manager')->getNewReadyUser();
191 140
   }
192 141
   
193 142
   protected function checkRegistrationValues($form)

+ 48 - 0
src/Muzich/UserBundle/Entity/UserManager.php View File

@@ -32,5 +32,53 @@ class UserManager extends UserManagerBase
32 32
 
33 33
     $this->objectManager->getEventManager()->addEventSubscriber($sluggableListener);
34 34
   }
35
+  
36
+  public function getNewReadyUser()
37
+  {
38
+    $user = $this->createUser();
39
+    $user->setUsername($this->generateUsername());
40
+    $user->setPlainPassword($this->generatePassword(32));
41
+    $user->setEnabled(true);
42
+    $user->setCguAccepted(true);
43
+    $user->setEmailConfirmed(false);
44
+    $user->setUsernameUpdatable(true);
45
+    $user->setPasswordSet(false);
46
+    return $user;
47
+  }
48
+  
49
+  protected function generateUsername()
50
+  {
51
+    $count = $this->repository->countUsers();
52
+    while ($this->usernameExist($count))
53
+    {
54
+      $count++;
55
+    }
56
+    
57
+    return 'User'.$count;
58
+  }
59
+  
60
+  protected function usernameExist($count)
61
+  {
62
+    $username = 'User'.$count;
63
+    if ($this->repository->findOneByUsername($username))
64
+    {
65
+      return true;
66
+    }
67
+    
68
+    return false;
69
+  }
70
+  
71
+  protected function generatePassword($length = 8)
72
+  {
73
+    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
74
+    $count = mb_strlen($chars);
75
+    
76
+    for ($i = 0, $result = ''; $i < $length; $i++) {
77
+        $index = rand(0, $count - 1);
78
+        $result .= mb_substr($chars, $index, 1);
79
+    }
80
+    
81
+    return $result;
82
+  }
35 83
 
36 84
 }