1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://dukt.net/facebook/ |
4
|
|
|
* @copyright Copyright (c) Dukt |
5
|
|
|
* @license https://github.com/dukt/facebook/blob/master/LICENSE.md |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dukt\facebook\controllers; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\web\Controller; |
12
|
|
|
use dukt\facebook\Plugin as Facebook; |
13
|
|
|
use Exception; |
14
|
|
|
use yii\web\Response; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class OauthController |
18
|
|
|
* |
19
|
|
|
* @author Dukt <[email protected]> |
20
|
|
|
* @since 2.0 |
21
|
|
|
*/ |
22
|
|
|
class OauthController extends Controller |
23
|
|
|
{ |
24
|
|
|
// Public Methods |
25
|
|
|
// ========================================================================= |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* OAuth connect. |
29
|
|
|
* |
30
|
|
|
* @return Response |
31
|
|
|
*/ |
32
|
|
|
public function actionConnect(): Response |
33
|
|
|
{ |
34
|
|
|
$provider = Facebook::$plugin->getOauth()->getOauthProvider(); |
35
|
|
|
|
36
|
|
|
Craft::$app->getSession()->set('facebook.oauthState', $provider->getState()); |
37
|
|
|
|
38
|
|
|
$options = Facebook::$plugin->getSettings()->oauthAuthorizationOptions; |
39
|
|
|
$options['scope'] = Facebook::$plugin->getSettings()->oauthScope; |
40
|
|
|
|
41
|
|
|
$authorizationUrl = $provider->getAuthorizationUrl($options); |
42
|
|
|
|
43
|
|
|
return $this->redirect($authorizationUrl); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* OAuth callback. |
48
|
|
|
* |
49
|
|
|
* @return Response |
50
|
|
|
*/ |
51
|
|
|
public function actionCallback(): Response |
52
|
|
|
{ |
53
|
|
|
$provider = Facebook::$plugin->getOauth()->getOauthProvider(); |
54
|
|
|
|
55
|
|
|
$code = Craft::$app->getRequest()->getParam('code'); |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
// Try to get an access token (using the authorization code grant) |
59
|
|
|
$token = $provider->getAccessToken('authorization_code', [ |
60
|
|
|
'code' => $code |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
// Save token |
64
|
|
|
Facebook::$plugin->getOauth()->saveToken($token); |
65
|
|
|
|
66
|
|
|
// Todo: Reset session variables |
67
|
|
|
|
68
|
|
|
// Redirect |
69
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('facebook', "Connected to Facebook.")); |
70
|
|
|
} catch (Exception $exception) { |
71
|
|
|
// Failed to get the token credentials or user details. |
72
|
|
|
Craft::$app->getSession()->setError($exception->getMessage()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->redirect('facebook/settings'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* OAuth disconnect. |
80
|
|
|
* |
81
|
|
|
* @return Response |
82
|
|
|
*/ |
83
|
|
|
public function actionDisconnect(): Response |
84
|
|
|
{ |
85
|
|
|
if (Facebook::$plugin->getOauth()->deleteToken()) { |
86
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('facebook', "Disconnected from Facebook.")); |
87
|
|
|
} else { |
88
|
|
|
Craft::$app->getSession()->setError(Craft::t('facebook', "Couldn’t disconnect from Facebook")); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// redirect |
92
|
|
|
$redirect = Craft::$app->getRequest()->referrer; |
93
|
|
|
|
94
|
|
|
return $this->redirect($redirect); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|