|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Socializer plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://enupal.com/ |
|
6
|
|
|
* @copyright Copyright (c) 2019 Enupal LLC |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
namespace enupal\socializer\controllers; |
|
11
|
|
|
|
|
12
|
|
|
use Craft; |
|
13
|
|
|
use craft\helpers\UrlHelper; |
|
14
|
|
|
use enupal\socializer\Socializer; |
|
15
|
|
|
use enupal\socializer\controllers\FrontEndController; |
|
16
|
|
|
|
|
17
|
|
|
class LoginController extends FrontEndController |
|
18
|
|
|
{ |
|
19
|
|
|
const SESSION_REDIRECT_URL = "socializer.redirectUrl"; |
|
20
|
|
|
const SESSION_PROVIDER_HANDLE = "socializer.providerHandle"; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return \yii\web\Response |
|
24
|
|
|
* @throws \Throwable |
|
25
|
|
|
* @throws \craft\errors\MissingComponentException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function actionLogin() |
|
28
|
|
|
{ |
|
29
|
|
|
$providerHandle = Craft::$app->getRequest()->getParam('provider'); |
|
30
|
|
|
$provider = Socializer::$app->providers->getProviderByHandle($providerHandle); |
|
31
|
|
|
$redirect = $this->getRedirectUrl(); |
|
32
|
|
|
|
|
33
|
|
|
if (is_null($provider)) { |
|
34
|
|
|
throw new \Exception(Craft::t('enupal-socializer','Provider not found or disabled')); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$redirectUrl = $redirect ?? Craft::$app->getRequest()->referrer; |
|
38
|
|
|
Craft::$app->getSession()->set(self::SESSION_REDIRECT_URL, $redirectUrl); |
|
39
|
|
|
Craft::$app->getSession()->set(self::SESSION_PROVIDER_HANDLE, $providerHandle); |
|
40
|
|
|
$adapter = $provider->getAdapter(); |
|
41
|
|
|
|
|
42
|
|
|
try { |
|
43
|
|
|
if ($adapter->authenticate()){ |
|
44
|
|
|
if (!Socializer::$app->providers->loginOrRegisterUser($provider)){ |
|
|
|
|
|
|
45
|
|
|
Craft::$app->getSession()->setError(Craft::t('enupal-socializer', "Unable to authenticate user")); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
catch (\Exception $e) { |
|
50
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->handleRedirect(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return mixed|string |
|
58
|
|
|
* @throws \yii\base\Exception |
|
59
|
|
|
*/ |
|
60
|
|
|
private function getRedirectUrl() |
|
61
|
|
|
{ |
|
62
|
|
|
$redirect = Craft::$app->getRequest()->getParam('redirect'); |
|
63
|
|
|
|
|
64
|
|
|
if ($redirect){ |
|
65
|
|
|
$redirect = UrlHelper::siteUrl($redirect); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $redirect; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return \yii\web\Response |
|
73
|
|
|
* @throws \Twig\Error\LoaderError |
|
74
|
|
|
* @throws \Twig\Error\SyntaxError |
|
75
|
|
|
* @throws \craft\errors\MissingComponentException |
|
76
|
|
|
*/ |
|
77
|
|
|
private function handleRedirect() |
|
78
|
|
|
{ |
|
79
|
|
|
$redirectUrl = Craft::$app->getSession()->get(self::SESSION_REDIRECT_URL); |
|
80
|
|
|
$user = Craft::$app->getUser()->getIdentity(); |
|
81
|
|
|
|
|
82
|
|
|
if ($user){ |
|
|
|
|
|
|
83
|
|
|
$variables['user'] = $user; |
|
|
|
|
|
|
84
|
|
|
$redirectUrl = Craft::$app->getView()->renderString($redirectUrl, $variables); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->restoreSession(); |
|
88
|
|
|
|
|
89
|
|
|
return $this->redirect($redirectUrl); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return \yii\web\Response |
|
94
|
|
|
* @throws \Throwable |
|
95
|
|
|
* @throws \craft\errors\ElementNotFoundException |
|
96
|
|
|
* @throws \craft\errors\MissingComponentException |
|
97
|
|
|
* @throws \craft\errors\WrongEditionException |
|
98
|
|
|
* @throws \yii\base\Exception |
|
99
|
|
|
*/ |
|
100
|
|
|
public function actionCallback() |
|
101
|
|
|
{ |
|
102
|
|
|
$providerHandle = Craft::$app->getSession()->get(self::SESSION_PROVIDER_HANDLE); |
|
103
|
|
|
$provider = Socializer::$app->providers->getProviderByHandle($providerHandle); |
|
104
|
|
|
|
|
105
|
|
|
if (is_null($provider)){ |
|
106
|
|
|
throw new \Exception(Craft::t('enupal-socializer','Provider not found or disabled')); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (!Socializer::$app->providers->loginOrRegisterUser($provider)){ |
|
|
|
|
|
|
110
|
|
|
Craft::$app->getSession()->setError(Craft::t('enupal-socializer', "Unable to authenticate user")); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->handleRedirect(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @throws \craft\errors\MissingComponentException |
|
118
|
|
|
*/ |
|
119
|
|
|
private function restoreSession() |
|
120
|
|
|
{ |
|
121
|
|
|
Craft::$app->getSession()->remove(self::SESSION_REDIRECT_URL); |
|
122
|
|
|
Craft::$app->getSession()->remove(self::SESSION_PROVIDER_HANDLE); |
|
123
|
|
|
} |
|
124
|
|
|
} |