-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.php
More file actions
320 lines (275 loc) · 9.05 KB
/
plugin.php
File metadata and controls
320 lines (275 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* Plugin.php
*
* This file is part of the Xpressengine package.
*
* PHP version 7
*
* @category SocialLogin
* @package Xpressengine\Plugins\SocialLogin
* @author XE Developers <developers@xpressengine.com>
* @copyright 2019 Copyright XEHub Corp. <https://www.xehub.io>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
* @link https://xpressengine.io
*/
namespace Xpressengine\Plugins\SocialLogin;
use Laravel\Socialite\Contracts\Factory as Socialite;
use Route;
use XeLang;
use Xpressengine\Plugin\AbstractPlugin;
use Xpressengine\Plugins\SocialLogin\Providers\NaverProvider;
use Xpressengine\Plugins\SocialLogin\Providers\KakaoProvider;
use Xpressengine\Plugins\SocialLogin\Providers\AppleProvider;
use Xpressengine\User\UserHandler;
use XeInterception;
use Xpressengine\Plugins\SocialLogin\Providers\SoundcloudProvider;
/**
* Plugin
*
* @category SocialLogin
* @package Xpressengine\Plugins\SocialLogin
* @author XE Developers <developers@xpressengine.com>
* @copyright 2019 Copyright XEHub Corp. <https://www.xehub.io>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
* @link https://xpressengine.io
*/
class Plugin extends AbstractPlugin
{
const REGISTER_TYPE_SIMPLE = 'simple';
const REGISTER_TYPE_STEP = 'step';
/**
* install
*
* @return void
*/
public function install()
{
$this->importConfig();
$this->importLang();
}
/**
* update
*
* @return void
*/
public function update()
{
$this->importLang();
if ($this->checkAllProviderImported() === false) {
$configProviders = app('xe.config')->getVal('social_login.providers', []);
$optionProviders = require __DIR__ . '/option.php';
foreach ($optionProviders as $providerName => $providerConfig) {
if (array_key_exists($providerName, $configProviders) === false) {
$providerConfig['activate'] = !!$providerConfig['client_id'];
$configProviders = array_merge($configProviders, [$providerName => $providerConfig]);
}
}
$socialLoginConfig = app('xe.config')->get('social_login');
$socialLoginConfig->set('providers', $configProviders);
app('xe.config')->modify($socialLoginConfig);
}
if ($this->checkExistRegisterTypeConfig() === false) {
$socialLoginConfig = app('xe.config')->get('social_login');
$socialLoginConfig->set('registerType', self::REGISTER_TYPE_SIMPLE);
app('xe.config')->modify($socialLoginConfig);
}
}
/**
* boot
*
* @return void
*/
public function boot()
{
// register settings menu
$this->registerSettingsMenu();
// register user settings section
$this->registerSection();
// register route
$this->routes();
app('router')->pushMiddlewareToGroup('web', Middleware::class);
}
/**
* register
*
* @return void
*/
public function register()
{
$this->config();
app()->singleton(Handler::class, function ($app) {
$proxyHandler = XeInterception::proxy(Handler::class, 'SocialLoginHandler');
$handler = new $proxyHandler($app[Socialite::class], $app['xe.user'], $app['xe.db'], $app['xe.config']);
$handler->setRequest($app['request']);
$app->refresh('request', $handler, 'setRequest');
return $handler;
});
app()->alias(Handler::class, 'xe.social_login');
app()->resolving('xe.social_login', function ($handler) {
$providers = $handler->getConfig();
if (empty($providers)) {
$this->importConfig();
$providers = $handler->getConfig();
}
// set config for redirect
foreach ($providers as $provider => $info) {
array_set($info, 'redirect', route('social_login::connect', ['provider' => $provider]));
config(['services.' . $provider => $info]);
}
});
app()->resolving(Socialite::class, function ($socialite) {
$socialite->extend('naver', function ($app) {
$config = $app['config']['services.naver'];
return new NaverProvider(
$app['request'],
$config['client_id'],
$config['client_secret'],
$config['redirect']
);
});
});
//02.02 추가
app()->resolving(Socialite::class, function ($socialite) {
$socialite->extend('kakao', function ($app) {
$config = $app['config']['services.kakao'];
return new KakaoProvider(
$app['request'],
$config['client_id'],
$config['client_secret'],
$config['redirect']
);
});
});
// 2020.11.13 추가
app()->resolving(Socialite::class, function ($socialite) {
$socialite->extend('apple', function ($app) {
$config = $app['config']['services.apple'];
return new AppleProvider(
$app['request'],
$config['client_id'],
$config['client_secret'],
$config['redirect']
);
});
});
// 2021.08.12 추가
app()->resolving(Socialite::class, function ($socialite) {
$socialite->extend('soundcloud', function ($app) {
$config = $app['config']['services.soundcloud'];
return new SoundcloudProvider(
$app['request'],
$config['client_id'],
$config['client_secret'],
$config['redirect']
);
});
});
}
public function config()
{
$key = 'social_login';
$config = app('config')->get($key, []);
app('config')->set($key, array_merge(require __DIR__.'/config/config.php', $config));
}
public function checkUpdated()
{
if ($this->checkAllProviderImported() === false) {
\Log::info('a');
return false;
}
if ($this->checkExistRegisterTypeConfig() === false) {
return false;
}
return parent::checkUpdated();
}
private function checkAllProviderImported()
{
$configProviders = app('xe.config')->getVal('social_login.providers', []);
$optionProviders = require __DIR__ . '/option.php';
foreach ($optionProviders as $providerName => $value) {
if (array_key_exists($providerName, $configProviders) === false) {
return false;
}
}
return true;
}
private function checkExistRegisterTypeConfig()
{
return app('xe.config')->getVal('social_login.registerType', false);
}
/**
* import config
*
* @return void
*/
protected function importConfig()
{
$providers = require __DIR__ . '/option.php';
foreach ($providers as $provider => $info) {
$providers[$provider]['activate'] = !!$info['client_id'];
}
app('xe.config')->set('social_login', ['providers' => $providers]);
}
/**
* import lang
*
* @return void
*/
protected function importLang()
{
XeLang::putFromLangDataSource('social_login', $this->path('langs/lang.php'));
}
/**
* register route
*
* @return void
*/
private function routes()
{
Route::group([
'namespace' => 'Xpressengine\\Plugins\\SocialLogin\\Controllers',
'middleware' => ['web']
], function () {
require __DIR__ . '/routes.php';
});
}
/**
* register settings menu
*
* @return void
*/
private function registerSettingsMenu()
{
app('xe.register')->push(
'settings/menu',
'user.social_login@default',
[
'title' => 'social_login::socialLogin',
'description' => 'social_login::descSocialLoginMenu',
'display' => true,
'ordering' => 350
]
);
}
/**
* register section
*
* @return void
*/
private function registerSection()
{
UserHandler::setSettingsSections('social_login@section', [
'title' => 'social_login::socialLoginSetting',
'content' => function ($user) {
$providers = app('xe.social_login')->getConfig();
$accountList = data_get($user, 'accounts', []);
$accounts = [];
foreach ($accountList as $account) {
$accounts[$account->provider] = $account;
}
return view('social_login::tpl.member_setting', compact('accounts', 'providers'));
}
]);
}
}