|
@@ -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
|
}
|