|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Christian Gripp <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Core23\FacebookBundle\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use Core23\FacebookBundle\Connection\FacebookConnection; |
|
13
|
|
|
use Facebook\Authentication\AccessToken; |
|
14
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
|
15
|
|
|
use Facebook\GraphNodes\GraphUser; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
20
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class AuthController extends Controller |
|
23
|
|
|
{ |
|
24
|
|
|
public const SESSION_FB_ID = '_CORE23_FACEBOOK_ID'; |
|
25
|
|
|
public const SESSION_FB_NAME = '_CORE23_FACEBOOK_NAME'; |
|
26
|
|
|
public const SESSION_FB_TOKEN = '_CORE23_FACEBOOK_TOKEN'; |
|
27
|
|
|
public const SESSION_FB_EXPIRES = '_CORE23_FACEBOOK_EXPIRES'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return Response |
|
31
|
|
|
*/ |
|
32
|
|
|
public function authAction(): Response |
|
33
|
|
|
{ |
|
34
|
|
|
$fb = $this->getFacebookConnection(); |
|
35
|
|
|
$helper = $fb->getRedirectLoginHelper(); |
|
36
|
|
|
|
|
37
|
|
|
return $this->redirect($helper->getLoginUrl( |
|
38
|
|
|
$this->generateUrl('core23_facebook_check', array(), UrlGeneratorInterface::ABSOLUTE_URL), |
|
39
|
|
|
$this->getParameter('core23.facebook.api.permissions') |
|
40
|
|
|
)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return Response |
|
45
|
|
|
*/ |
|
46
|
|
|
public function checkAction(): Response |
|
47
|
|
|
{ |
|
48
|
|
|
$fb = $this->getFacebookConnection(); |
|
49
|
|
|
$helper = $fb->getRedirectLoginHelper(); |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
|
|
if ($token = $helper->getAccessToken()) { |
|
53
|
|
|
$response = $fb->get('/me?fields=id,name', $token); |
|
54
|
|
|
|
|
55
|
|
|
$this->storeCredentials($token, $response->getGraphUser()); |
|
56
|
|
|
|
|
57
|
|
|
return $this->redirectToRoute('core23_facebook_success'); |
|
58
|
|
|
} |
|
59
|
|
|
} catch (FacebookSDKException $exception) { |
|
60
|
|
|
$this->getLogger()->warning(sprintf('Facebook SDK Exception: %s', $exception->getMessage())); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->redirectToRoute('core23_facebook_error'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return Response |
|
68
|
|
|
*/ |
|
69
|
|
|
public function errorAction(): Response |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->isAuthenticated()) { |
|
72
|
|
|
return $this->redirectToRoute('core23_facebook_success'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (null !== $this->getParameter('core23.facebook.auth_error.redirect_route')) { |
|
76
|
|
|
return $this->redirectToRoute($this->getParameter('core23.facebook.auth_error.redirect_route'), $this->getParameter('core23.facebook.auth_error.redirect_route_params')); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->render('Core23FacebookBundle:Auth:error.html.twig'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return Response |
|
84
|
|
|
*/ |
|
85
|
|
|
public function successAction(): Response |
|
86
|
|
|
{ |
|
87
|
|
|
if (!$this->isAuthenticated()) { |
|
88
|
|
|
return $this->redirectToRoute('core23_facebook_error'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (null !== $this->getParameter('core23.facebook.auth_success.redirect_route')) { |
|
92
|
|
|
return $this->redirectToRoute($this->getParameter('core23.facebook.auth_success.redirect_route'), $this->getParameter('core23.facebook.auth_success.redirect_route_params')); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$session = $this->getSession(); |
|
96
|
|
|
|
|
97
|
|
|
return $this->render('Core23FacebookBundle:Auth:success.html.twig', array( |
|
98
|
|
|
'name' => $session->get(static::SESSION_FB_NAME), |
|
99
|
|
|
)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param AccessToken $token |
|
104
|
|
|
* @param GraphUser $user |
|
105
|
|
|
*/ |
|
106
|
|
|
private function storeCredentials(AccessToken $token, GraphUser $user): void |
|
107
|
|
|
{ |
|
108
|
|
|
$fbid = $user->getId(); |
|
109
|
|
|
$name = $user->getName(); |
|
110
|
|
|
|
|
111
|
|
|
$session = $this->getSession(); |
|
112
|
|
|
$session->set(static::SESSION_FB_ID, $fbid); |
|
113
|
|
|
$session->set(static::SESSION_FB_NAME, $name); |
|
114
|
|
|
$session->set(static::SESSION_FB_TOKEN, $token); |
|
115
|
|
|
$session->set(static::SESSION_FB_EXPIRES, $token->getExpiresAt()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns the auth status. |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
private function isAuthenticated(): bool |
|
124
|
|
|
{ |
|
125
|
|
|
return (bool) $this->getSession()->get(static::SESSION_FB_TOKEN); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return FacebookConnection |
|
130
|
|
|
*/ |
|
131
|
|
|
private function getFacebookConnection(): FacebookConnection |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->get('core23.facebook.connection'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return LoggerInterface |
|
138
|
|
|
*/ |
|
139
|
|
|
private function getLogger() : LoggerInterface |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->get('logger'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return SessionInterface |
|
146
|
|
|
*/ |
|
147
|
|
|
private function getSession() : SessionInterface |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->get('session'); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|