-
Notifications
You must be signed in to change notification settings - Fork 3
/
Plugin.php
286 lines (248 loc) · 11.6 KB
/
Plugin.php
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
<?php
namespace Tohur\SocialConnect;
use App;
use Event;
use URL;
use Illuminate\Foundation\AliasLoader;
use System\Classes\PluginBase;
use System\Classes\SettingsManager;
use RainLab\User\Models\User;
use RainLab\User\Models\UserGroup;
use Carbon\Carbon;
use RainLab\User\Controllers\Users as UsersController;
use Backend\Widgets\Form;
use Tohur\SocialConnect\Classes\Apis\TwitchAPI;
use Tohur\SocialConnect\Classes\ProviderManager;
/**
* SocialConnect Plugin Information File
*
* http://www.mrcasual.com/on/coding/laravel4-package-management-with-composer/
* https://cartalyst.com/manual/sentry-social
*
*/
class Plugin extends PluginBase {
// Make this plugin run on updates page
public $elevated = true;
public $require = ['RainLab.User'];
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails() {
return [
'name' => 'Social Connect',
'description' => 'Allows visitors to register/sign in with their social media accounts',
'author' => 'Joshua Webb',
'icon' => 'icon-users'
];
}
public function registerSettings() {
return [
'settings' => [
'label' => 'Social Connect',
'description' => 'Manage Social Login providers.',
'category' => 'Tohur',
'icon' => 'icon-users',
'class' => 'Tohur\SocialConnect\Models\Settings',
'order' => 600,
'permissions' => ['rainlab.users.access_settings'],
],
[
'label' => 'Api Settings',
'description' => 'Manage Third Party Api settings.',
'category' => 'Tohur',
'icon' => 'icon-cogs',
'class' => 'Tohur\SocialConnect\Models\Apisettings',
'order' => 600,
'permissions' => ['rainlab.users.access_settings'],
]
];
}
public function registerComponents() {
return [
'Tohur\SocialConnect\Components\SocialConnect' => 'socialconnect',
];
}
/**
* Register method, called when the plugin is first registered.
* @return void
*/
public function register() {
}
public function registerSchedule($schedule) {
$schedule->call(function () {
$twitch = new TwitchAPI();
$twitchAPISettings = \Tohur\SocialConnect\Models\Settings::instance()->get('providers', []);
if (!strlen($twitchAPISettings['Twitch']['client_id'])) {
} else {
$client_id = $twitchAPISettings['Twitch']['client_id'];
$client_secret = $twitchAPISettings['Twitch']['client_secret'];
$count = \DB::table('tohur_socialconnect_providers')->count();
if ($count == 0) {
} else {
$Tokens = \DB::table('tohur_socialconnect_providers')->get();
foreach ($Tokens as $Token) {
if ($Token->provider_id == 'Twitch') {
$expiresIn = $Token->provider_expiresIn;
$current = Carbon::now();
if ($Token->updated_at == null) {
$time = $Token->created_at;
} else {
$time = $Token->updated_at;
}
$expired = Carbon::parse($time)->addSeconds($expiresIn);
if ($current > $expired) {
$tokenRequest = json_decode($twitch->helixTokenRequest($twitch->oAuthbaseUrl . "?grant_type=refresh_token&refresh_token=" . $Token->provider_refreshToken . "&client_id=" . $client_id . "&client_secret=" . $client_secret . ""), true);
$accessToken = $tokenRequest['access_token'];
$refreshToken = $tokenRequest['refresh_token'];
$tokenExpires = $expiresIn;
\Db::table('tohur_socialconnect_providers')
->where('provider_id', '=', 'Twitch')
->update(['provider_token' => $accessToken, 'provider_refreshToken' => $refreshToken, 'provider_expiresIn' => $tokenExpires, 'updated_at' => now()]);
}
} else {
}
}
}
}
})->everyMinute();
$schedule->call(function () {
$twitch = new TwitchAPI();
$twitchAPISettings = \Tohur\SocialConnect\Models\Settings::instance()->get('providers', []);
if (!strlen($twitchAPISettings['Twitch']['client_id'])) {
} else {
$client_id = $twitchAPISettings['Twitch']['client_id'];
$client_secret = $twitchAPISettings['Twitch']['client_secret'];
$count = \DB::table('tohur_socialconnect_twitch_apptokens')->count();
if ($count == 0) {
} else {
$tokens = \DB::select('select * from tohur_socialconnect_twitch_apptokens where id = ?', array(1));
$expiresIn = $tokens[0]->expires_in;
$current = Carbon::now();
if ($tokens[0]->updated_at == null) {
$time = $tokens[0]->created_at;
} else {
$time = $tokens[0]->updated_at;
}
$expired = Carbon::parse($time)->addSeconds($expiresIn);
if ($current > $expired) {
$revokeRequest = json_decode($twitch->helixTokenRequest($twitch->oRevokebaseUrl . "?client_id=" . $client_id . "&token=" . $tokens[0]->access_token . ""), true);
$tokenRequest = json_decode($twitch->helixTokenRequest($twitch->oAuthbaseUrl . "?grant_type=client_credentials&client_id=" . $client_id . "&client_secret=" . $client_secret . "&scope=channel:read:hype_train%20channel:read:subscriptions%20bits:read%20user:read:broadcast%20user:read:email"), true);
$accessToken = $tokenRequest['access_token'];
$tokenExpires = $tokenRequest['expires_in'];
\Db::table('tohur_socialconnect_twitch_apptokens')
->where('id', 1)
->update(['access_token' => $accessToken, 'expires_in' => $tokenExpires, 'updated_at' => now()]);
}
}
}
})->daily();
}
public function boot() {
// Load socialite
App::register(\SocialiteProviders\Manager\ServiceProvider::class);
AliasLoader::getInstance()->alias('Socialite', 'Laravel\Socialite\Facades\Socialite');
User::extend(function ($model) {
$model->hasMany['tohur_socialconnect_providers'] = ['Tohur\SocialConnect\Models\Provider'];
});
User::extend(function ($model) {
$model->addDynamicMethod('addUserGroup', function ($group) use ($model) {
if ($group instanceof \October\Rain\Support\Collection) {
return $model->groups()->saveMany($group);
}
if (is_string($group)) {
$group = UserGroup::whereCode($group)->first();
return $model->groups()->save($group);
}
if ($group instanceof \RainLab\User\Models\UserGroup) {
return $model->groups()->save($group);
}
});
});
// Add 'Social Logins' column to users list
UsersController::extendListColumns(function ($widget, $model) {
if (!$model instanceof \RainLab\User\Models\User)
return;
$widget->addColumns([
'tohur_socialconnect_user_providers' => [
'label' => 'Social Logins',
'type' => 'partial',
'path' => '~/plugins/tohur/socialconnect/models/provider/_provider_column.htm',
'searchable' => false
]
]);
});
// Generate Social Login settings form
Event::listen('backend.form.extendFields', function (Form $form) {
if (!$form->getController() instanceof \System\Controllers\Settings)
return;
if (!$form->model instanceof \Tohur\SocialConnect\Models\Settings)
return;
foreach (ProviderManager::instance()->listProviders() as $class => $details) {
$classObj = $class::instance();
$classObj->extendSettingsForm($form);
}
});
// Add 'Social Providers' field to edit users form
Event::listen('backend.form.extendFields', function ($widget) {
if (!$widget->getController() instanceof \RainLab\User\Controllers\Users)
return;
if (!$widget->model instanceof \RainLab\User\Models\User)
return;
if (!in_array($widget->getContext(), ['update', 'preview']))
return;
$widget->addFields([
'tohur_socialconnect_user_providers' => [
'label' => 'Social Providers',
'type' => 'Tohur\SocialConnect\FormWidgets\LoginProviders',
],
], 'secondary');
});
// Add backend login provider integration
Event::listen('backend.auth.extendSigninView', function () {
$providers = ProviderManager::instance()->listProviders();
$social_connect_links = [];
foreach ($providers as $provider_class => $provider_details)
if ($provider_class::instance()->isEnabledForBackend())
$social_connect_links[$provider_details['alias']] = URL::route('tohur_socialconnect_provider', [$provider_details['alias']]) . '?s=' . Backend::url() . '&f=' . Backend::url('backend/auth/signin');
if (!count($social_connect_links))
return;
require __DIR__ . '/partials/backend/_login.htm';
});
}
function register_tohur_socialconnect_providers() {
return [
'\\Tohur\\SocialConnect\\SocialConnectProviders\\Facebook' => [
'label' => 'Facebook',
'alias' => 'Facebook',
'description' => 'Log in with Facebook'
],
'\\Tohur\\SocialConnect\\SocialConnectProviders\\Twitter' => [
'label' => 'Twitter',
'alias' => 'Twitter',
'description' => 'Log in with Twitter'
],
'\\Tohur\\SocialConnect\\SocialConnectProviders\\Google' => [
'label' => 'Google',
'alias' => 'Google',
'description' => 'Log in with Google'
],
'\\Tohur\\SocialConnect\\SocialConnectProviders\\Microsoft' => [
'label' => 'Microsoft',
'alias' => 'Microsoft',
'description' => 'Log in with Microsoft'
],
'\\Tohur\\SocialConnect\\SocialConnectProviders\\Discord' => [
'label' => 'Discord',
'alias' => 'Discord',
'description' => 'Log in with Discord'
],
'\\Tohur\\SocialConnect\\SocialConnectProviders\\Twitch' => [
'label' => 'Twitch',
'alias' => 'Twitch',
'description' => 'Log in with Twitch'
],
];
}
}